	var maxExtent = new OpenLayers.Bounds(463548.807345,4702464.900336,603188.481532,4811786.924463);
	
	var clay = false;
	var cslay = false;
	var tmz = 0;
	var options = {
			minResolution: "auto",
            minExtent: new OpenLayers.Bounds(-1, -1, 1, 1), 
            maxResolution: "auto",
			maxExtent: maxExtent, 
          	
          	numZoomLevels: 1,
          	
            controls: [new OpenLayers.Control.MouseDefaults()]
			};
	
	
	
	
	var map = new OpenLayers.Map('map',options);
    map.addControl(new OpenLayers.Control.PanZoomBar());
            map.events.register("moveend", null, displayZoom);

         
        function displayZoom() {
        
            if (clay!=false) {
            	if (map.getZoom()!=tmz)clay.redraw();
            	tmz = map.getZoom();
            }
        }


	
	
	
	var filemap = '/var/www/mendinet/mapa/mapa.map';

	layer = new OpenLayers.Layer.MapServer("base_layer","/cgi-bin/mapserv",{map:filemap,layer:"base_layer2",layers:"basic"});
										
	map.addLayer(layer);										
	/*ex = new OpenLayers.Layer.MapServer("excluidas",
										"/cgi-bin/mapserv",
										{map:filemap,layer:"excluidas_pdr"}
										);*/										
	var layers = {};
	var layersCom = {};
	
	
	
	
	balayer = new OpenLayers.Layer.MapServer("base_layer",
										"/cgi-bin/mapserv",
										{map:filemap,layer:"base_layer"}
										);
	map.addLayer(balayer);
	
	
	trans = new OpenLayers.Layer.MapServer("trans",
										"/cgi-bin/mapserv",
										{map:filemap,layer:"trans"}
										);
	map.addLayer(trans);
	
	goierri = new OpenLayers.Layer.MapServer("goierri",
										"/cgi-bin/mapserv",
										{map:filemap,layer:"goierri"}
										);
	map.addLayer(goierri);
	for (var a in comarcas){
		//alert(comarcas[a]);
		layersCom[comarcas[a]] = new OpenLayers.Layer.MapServer("solocolors",
											"/cgi-bin/mapserv",
											{map:filemap,layer:"solocolors",com:comarcas[a]}
											);
		map.addLayer(layersCom[comarcas[a]]);
	}	
	
	//map.addLayer(ex);
	for (i=1;i<=9;i++){
		layers[i] = new OpenLayers.Layer.MapServer("G"+i,
										"/cgi-bin/mapserv",
										{map:filemap,layer:"G"+i}
										);
		map.addLayer(layers[i]);
		layers[i].setOpacity(0.1);
	}
	
	//map.addControl(new OpenLayers.Control.PanZoomBar());
	
	
	map.setOptions({restrictedExtent: maxExtent});
	map.zoomToMaxExtent();	
	 
	 
	 
	var markers = new OpenLayers.Layer.Markers("Loc");
	map.addLayer(markers);
	
	var xx;
	var yy;
	
	
	var mendiFrame =  OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
	    'autoSize': false,
	    'minSize': new OpenLayers.Size(50,50)
	});


	function addMarker(ll, popupClass, popupContentHTML, closeBox, overflow,id) {
	    var feature = new OpenLayers.Feature(markers, ll); 
	    feature.closeBox = closeBox;
	    feature.closeBox = true;
	    feature.popupClass = popupClass;
	    feature.data.popupContentHTML = "<div class=\"cloudInfo\" id=\"cloud_"+id+"\"></div>";
	    feature.data.overflow = (overflow) ? "auto" : "hidden";
	    var marker = feature.createMarker();
	    var markerClick = function (evt) {
	    
			    jQuery.getJSON('zonas_rurales.html',{info:id},function(data){ 
					if (data.res){
					
						feature.data.popupContentHTML= jQuery("#cloud_"+id).html(data.res);
						
					}
				});
	        if (this.popup == null) {
	            this.popup = this.createPopup(this.closeBox);
	            map.addPopup(this.popup);
	            this.popup.show();
	        } else {
	            this.popup.toggle();
	        }
	        currentPopup = this.popup;
	        OpenLayers.Event.stop(evt);
	    };
	    marker.events.register("mousedown", feature, markerClick);
	    markers.addMarker(marker);
	}
     jQuery.noConflict();
     
     jQuery(document).ready(function(){


	

	function findValue(li) {
		if( li == null ) return alert("No match!");
		if( !!li.extra ) var sValue = li.extra[0];
		else var sValue = li.selectValue;
		d = sValue.split('*');
		var lonlat = new OpenLayers.LonLat(d[1],d[0]);
		markers.clearMarkers();

	    addMarker(lonlat, mendiFrame, 'hola', false, true,d[2]);
		window.setTimeout(function(){ map.panTo(lonlat);},700);
	}
	
	function selectItem(li) {
		findValue(li);
	}
	
	function formatItem(row) {
		return row[0]+" ("+row[2]+")";
	}

	jQuery("#busca").autocomplete("localidades.html",{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:false
		}
	);
	
	jQuery("#busca1").autocomplete("municipios.html",{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:false
		}
	);	

	jQuery("#busca2").autocomplete("comarcas.html",{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:false
		}
	);

	jQuery('#selComarca').bind('change',function(){
		if (jQuery(this).val()=="__NULL"||1==1){ markers.clearMarkers(); }
		if (jQuery(this).val()=="__NULL"){
			balayer.setVisibility(false);
			layer.setOpacity(0);
			trans.setVisibility(false);
				for (var a in comarcas){
				
					layersCom[comarcas[a]].setVisibility(false);
				}
		 
			layer.setVisibility(true);
				var i=0;
				var vis = window.setInterval(function(){
						if (i == 99 ){clearInterval(vis);}
						x= i;
						if (x<10) x= '0'+i;
						layer.setOpacity('0.'+x); i++; 
						},20);
			
			return false;
		}
		var coma  =  jQuery(this).val();
		jQuery.getJSON('zonas_rurales.html',{c:jQuery(this).val()},function(data){ 
			if (data.res){
			
		layer.setVisibility(false);
		//balayer.setVisibility(true);
		
		for (i=1;i<=9;i++){
			layers[i].setVisibility(false);
		}
		for (var a in comarcas){
		
			layersCom[comarcas[a]].setVisibility(false);
		}
		
	
		layersCom[coma].setVisibility(true);
		layersCom[coma].setOpacity(0);
		var i=0;

		clay = layersCom[coma];
		cslay = "COMARCAS";

		var vis = window.setInterval(function(){
				if (i == 99 ){clearInterval(vis);}
				x= i;
				if (x<10) x= '0'+i;
				layersCom[coma].setOpacity('0.'+x); i++; 
				},10);
		
		//trans.setVisibility(true);
		//layersCom[coma].setOpacity(0.75);


				
		return;	
				markers.clearMarkers();
				arr = data.res;
				
				for(var a in arr){
					var lonlat = new OpenLayers.LonLat(arr[a].y,arr[a].x);
					 
				    addMarker(lonlat, mendiFrame, 'hola', false, true,arr[a].idpol);
				}
				map.zoomTo('2');
				var lonlat = new OpenLayers.LonLat(data.Y,data.X);
				window.setTimeout(function(){ map.panTo(lonlat);},500);
			}
		});
		return false;
	});
	
	jQuery('#RESET').bind('click',function(){
		markers.clearMarkers();
		map.zoomToMaxExtent();	
		for (var a in comarcas){
			layersCom[comarcas[a]].setVisibility(false);
		}
		for (i=1;i<=9;i++){
			layers[i].setVisibility(false);
		}
		balayer.setVisibility(false);
		layer.setOpacity(0); 
		layer.setVisibility(true);
			var i=0;
			var vis = window.setInterval(function(){
					if (i == 99 ){clearInterval(vis);}
					x= i;
					if (x<10) x= '0'+i;
					layer.setOpacity('0.'+x); i++; 
					},20);

		return false;
	});
	
	jQuery('#selGrupo').bind('change',function(){
		markers.clearMarkers();
		layer.setVisibility(false);
		trans.setVisibility(false);
		
		for (var a in comarcas){
			layersCom[comarcas[a]].setVisibility(false);
		}
		for (i=1;i<=9;i++){
			layers[i].setVisibility(false);
		}

		if (jQuery(this).val()=="__NULL"){
			balayer.setVisibility(false);
			layer.setOpacity(0); 
			layer.setVisibility(true);
				var i=0;
				var vis = window.setInterval(function(){
						if (i == 99 ){clearInterval(vis);}
						x= i;
						if (x<10) x= '0'+i;
						layer.setOpacity('0.'+x); i++; 
						},20);
			
			return false;
		}
		
		balayer.setVisibility(true);
		
		v = jQuery(this).val().split('G');
		layers[v[1]].setVisibility(true);
		
		clay = layers[v[1]];
		cslay = "GRUPOS";
		
		//layers[v[1]].setOpacity(0);
		var i=0;
		var vis = window.setInterval(function(){
				if (i == 99 ){clearInterval(vis);}
				x= i;
				if (x<10) x= '0'+i;
				layers[v[1]].setOpacity('0.'+x); i++; 
				},20);

		return false;
	});

});
