//<![CDATA[

	/****************** VARIABLES GLOBALES *************************************/


	var map;
	var geocoder;
	
	var centerLatitude=39.472643;
	var centerLongitude=-0.376797;
	
	var startZoom=15;
	
	var zoom_img_on=4;
	
	var refresh=true;
	
	

	//var addres=document.frm.q.value;
	//alert(addres);
	/***********************************************************************************/
	
	/*********************************** GEOCODIFICACION *******************************/
	
	function showAddress(address,descripcion) {
  		geocoder.getLatLng(address,function(point) {
      		if (!point || address=='') {
        		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
      		} else {
        		//map.setCenter(point, startZoom);
        		
        		centerLatitude=point.lat();
        		centerLongitude=point.lng();
        		
        		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        		if(descripcion!=""){
				addMarker(centerLatitude,centerLongitude,descripcion,1);
				}
        		//updateMarkers(new GLatLng(centerLatitude,centerLongitude));
        		//var marker = new GMarker(point);
        		//map.addOverlay(marker);
        		//marker.openInfoWindowHtml(address);
      			}
    		}
  		);
	}
	
	function showAddressZoom(address,descripcion,zoom) {
  		geocoder.getLatLng(address,function(point) {
      		if (!point || address=='') {
        		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
      		} else {
        		//map.setCenter(point, startZoom);
        		
        		centerLatitude=point.lat();
        		centerLongitude=point.lng();
        		
        		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        		addMarker(centerLatitude,centerLongitude,descripcion,1);
        		//updateMarkers(new GLatLng(centerLatitude,centerLongitude));
        		//var marker = new GMarker(point);
        		//map.addOverlay(marker);
        		//marker.openInfoWindowHtml(address);
      			}
    		}
  		);
	}
	
	/***********************************************************************************/
	
	
	/******************************* FUNCIONES DE ZOOM *********************************/
	
	function zoomIn(){
		
		var zoomActual=map.getZoom();
		if(zoomActual<17){
			map.zoomIn(false,true);
			ChangeZoomImage(zoom_img_on+1);
		}
	
	}
	
	function zoomOut(){
		var zoomActual=map.getZoom();
		if(zoomActual>12){
			map.zoomOut(false,true);
			ChangeZoomImage(zoom_img_on-1);
		}
	}
	
	function zoomLevel(level){
	
		if(level>=12 && level<=17){
			map.setZoom(level);	
			ChangeZoomImage(level-11);
		}
			
	}
	
	/********************************************************************************/
	
	/********************************* FUNCIONES DE DESPLAZAMIENTO **************************/
	
	
	function moveNorth(){
		map.panDirection(0,+3);
	}
	
	function moveSouth(){
		map.panDirection(0,-3);
	}
	
	function moveEast(){
		map.panDirection(-3,0);
	}
	
	function moveWest(){
		map.panDirection(+3,0);
	}
	
	function moveNorthEast(){
		map.panDirection(-3,+3);
	}
	
	function moveNorthWest(){
		map.panDirection(+3,+3);
	}
	
	function moveSouthEast(){
		map.panDirection(-3,-3);
	}
	
	function moveSouthWest(){
		map.panDirection(+3,-3);
	}
	
	/****************************************************************************/

	/********************** FUNCIONES DE MARCADORES ****************************/
	
	function addMarker(latitude,longitude,description,tipo){
	
		var point=new GLatLng(latitude,longitude);
		//var icon=new GIcon();
		
		/*switch(tipo){
		
			case 1:
				icon.image="http://www.distritomaritimo.com/mapas/images/Arrow-48.png";
				icon.shadow="http://www.distritomaritimo.com/mapas/images/sh.png";
				icon.iconSize=new GSize(50,40);
				icon.shadowSize=new GSize(78,50);
				icon.iconAnchor=new GPoint(0,0);
				icon.infoWindowAnchor=new GPoint(5,1);
			break;
			case 2:
				icon.image="http://www.distritomaritimo.com/mapas/images/pin_A_0_on.png";
				icon.shadow="http://www.distritomaritimo.com/mapas/images/shadow_chincheta.png";
				icon.iconSize=new GSize(37,49);
				icon.shadowSize=new GSize(57,49);
				icon.iconAnchor=new GPoint(17,50);
				icon.infoWindowAnchor=new GPoint(5,1);
			break;
		
		}*/
		
		var marker=new GMarker(point);
		
		
				
			
			
		map.addOverlay(marker);
		
		marker.openInfoWindowHtml(description);	
		
	}//function addMarker(latitude,longitude,description)
	
	/***************************************************************************/
	
	
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapa_zoom_area"));
        geocoder = new GClientGeocoder();

        showAddress(document.getElementById("q").value,document.getElementById("d").value);
        
        
        //map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        
        
        
        
        
        //Activamos el zoom suave
        map.enableContinuousZoom();
        map.disableDoubleClickZoom();
        //map.enableScrollWheelZoom();
        
        
        var toActiveIni=document.getElementById("zoom_img"+zoom_img_on);
        toActiveIni.src='http://www.distritomaritimo.com/mapas/images/zoom'+zoom_img_on+'on.gif';

        GEvent.addListener(map, "zoomend", function() {
  			var newcenter = map.getCenter();
  			updateMarkers(newcenter);
		});
        
        GEvent.addListener(map, "moveend", function() {
  			var newcenter = map.getCenter();
  			if(refresh==true){
  				updateMarkers(newcenter);
			}
			refresh=true;
		});
        
      }//if (GBrowserIsCompatible())
    }//function load()
	
    //Esta función actualiza los marcadores con ajax en funcion de la cercanía a un punto central que se le pasa como parámetro
    
    function updateMarkers(point){
	 
	    //remove the existing points
	    //map.clearOverlays();
	    //showAddressZoom(document.getElementById("q").value,document.getElementById("d").value,map.getZoom());
	    //create the boundary for the data to provide
	    //initial filtering
	    /*var bounds=map.getBounds();
	    var southWest=bounds.getSouthWest();
	    var northEast=bounds.getNorthEast();
	    var getVars='ne='+northEast.toUrlValue()+'&sw='+southWest.toUrlValue()+'&known='+point.toUrlValue();
	    
	    //retrieve the points
	    var request = GXmlHttp.create();
	    
	    request.open('GET','http://www.distritomaritimo.com/mapas/includes/ws_call.php?'+getVars,true);
	    request.onreadystatechange=function(){
		    if(request.readyState == 4){
			    
			 	var jscript=request.responseText;
			 	var points;
			 	eval(jscript);
			 	//create each point from the list
			 	
			 	for(i in myJSON.points){
					if(i%2==0){
				 	addMarker(parseFloat(myJSON.points[i].lat),parseFloat(myJSON.points[i].lng),myJSON.points[i].name,2);
			 		}else{
				 		addMarker(parseFloat(myJSON.points[i].lat),parseFloat(myJSON.points[i].lng),myJSON.points[i].name,1);
			 		}
				 	
			 	}   
		    }else{
			    alert("estoy aqui");
		    }
	    }
	    
	    request.send(null);*/
	       
    }
    
    
	
    window.onload=load;
	window.onunload=GUnload;
	
	
    //]]>