  
  countCache = {};
  
  function applyFilterToTiled(){
    
    scaleParam = jQuery('#search_scale').val();
    formatParam = jQuery('#search_format').val();
    typeParam = jQuery('#search_type').val();
    
    filterTag(
      scaleParam,
      formatParam,
      typeParam
    );
    
    if (marker){
      //console.log(marker);
      var lonLat = new OpenLayers.LonLat(marker.data.lon, marker.data.lat).transform(map.displayProjection, map.projection);
      dataSend( lonLat.lon, lonLat.lat );
    } 
    
    
  }
  
  function doCount(){
   
      scaleParam = jQuery('#search_scale').val();
      formatParam = jQuery('#search_format').val();
      typeParam = jQuery('#search_type').val();
      console.log(countCache);
      var cacheKey = scaleParam + '_' + formatParam + '_' + typeParam;
      
      if (countCache[cacheKey]) {
      
        jQuery('#preview-count').text(countCache[cacheKey]);
        return ;
      } 
      
      jQuery.ajax({
        url: "/maps/count",
        type: 'GET',
        global: false,
        data:{
          scale: scaleParam,
          format: formatParam,
          type: typeParam  
        }, 
        success: function(data){
          jQuery('#preview-count').text(data);
          countCache[cacheKey] = data;
        }
      });
      
      
  }
  
  function filterTag(scale, format, type){
    
    var title;
    
    cqlString = '';
    if (scale) {
    
      cqlString += "poehali:scale=\'" + scale + "\'";
    }
    
    if (format) {
      if (cqlString.length) cqlString += '&&'; 
      cqlString += "poehali:format=\'" + format + "\'";
    }
    
    if (type) {
      if (cqlString.length) cqlString += '&&';
      cqlString += "poehali:type=\'" + type + "\'";
    }
    
    if (cqlString.length > 0) {
      
      //title = 'Карты ' + tag;
      
      updateFilter("cql", cqlString);
      
      //tagsList = jQuery('.list-tags').eq(0);
      
      
    } else {
      //title = 'Карты';
      resetFilter();
    }
    
    //jQuery('#title-header').text(title);
  }
  
  
  function updateFilter(type, value){
      // merge the new filter definitions
      var filterParams = getFilterParams(type, value);
      mergeNewParams(filterParams);
  }
  
  function getFilterParams(type, value) {
      
      var filterType = type;
      var filter = value;
      
      // by default, reset all filters                
      var filterParams = {
          filter: null,
          cql_filter: null,
          featureId: null
      };
      

      if (filter && OpenLayers.String.trim(filter) != "") {
          if (filterType == "cql") 
              filterParams["cql_filter"] = filter;
          if (filterType == "ogc") 
              filterParams["filter"] = filter;
          if (filterType == "fid") 
              filterParams["featureId"] = filter;
      }
      
      return filterParams;
  }



  function updateFeatureInfoFilters(featureInfoParams){
      var filterParams = getFilterParams();
      if(!filterParams)
        return;
        
      featureInfoParams["cql_filter"] = filterParams["cql_filter"];
      featureInfoParams["filter"] = filterParams["filter"];
      featureInfoParams["featureId"] = filterParams["featureId"];
  }
  
  function resetFilter() {
  
      updateFilter();
  }
  
  function mergeNewParams(params){
      
      //пока без фильтра
      //tiledLayer.mergeNewParams(params);
  }
  
  
  var drawPolygon =  function(data, title, link) {
      
      
      try {
        data = eval('(' + data + ')');
      } catch (e) {
      
        data = null;
      }
      
      
      if (data) {
      
        var coords = [];
      

        
        coords[3] = data.ne.lat;
        coords[2] = data.ne.lng;
        coords[1] = data.sw.lat;
        coords[0] = data.sw.lng;
        
        
        
        ext = coords;
        var bounds = new OpenLayers.Bounds();
        bounds.extend(new OpenLayers.LonLat(data.ne.lng, data.ne.lat).transform( map.displayProjection, map.projection));
        bounds.extend(new OpenLayers.LonLat(data.sw.lng, data.sw.lat).transform( map.displayProjection, map.projection));
        
        
        
        
        var box = new OpenLayers.Feature.Vector(bounds.toGeometry(), {
          url: link,
          title: title
        }, st);
        
        
        
        
        vectorLayer.addFeatures([box]);
        
        return box;
        
       
      }
    }  

  //во внутр представлении
  function dataSend(lon, lat){
    
    var lonLat = new OpenLayers.LonLat(lon, lat).transform(map.projection, map.displayProjection);
  
    jQuery.ajax({
        type: 'POST',
        url: '/maps/get',
        global: false,
        data: {
          lng: lonLat.lon,
          lat: lonLat.lat,
                
          format: jQuery('#search_format').val(),
          type: jQuery('#search_type').val(),
          scale: jQuery('#search_scale').val(),
          sf_format: 'js'
        },
        success: function(data){
          
          var infoDiv = jQuery('#info'); 
          infoDiv.html(data);
          jQuery('a', infoDiv).each(function(){
            
            if (jQuery(this).attr('data')) {
              var box = drawPolygon(jQuery(this).attr('data'));
              
              jQuery(this).mouseover(function(){
                st.display = 'block';
                vectorLayer.drawFeature(box, st);
              });
              
              jQuery(this).mouseout(function(){
                st.display = 'none';
                vectorLayer.drawFeature(box, st);
              });         
                            
            }
          });
        },
        
        beforeSend: function(){
          jQuery('#info').html('<div style="display: block;" id="loadIndicatorInfo"><div style="border: 2px solid rgb(232, 232, 232); height: 60px; text-align: left; background-color: white;"><img src="/images/loading.gif" class="fL"/><div style="margin: 10px 0px 0px 65px;">Загрузка</div><div style="margin: 10px 0px 0px 60px;"><small>Пожалуйста подождите</small></div></div></div>');
        }
        /*
        complete: function(){
          jQuery('#info').empty();
        } 
        */ 
      });
  }