	var map = null;
    var geocoder = null;
	var puntoMovil = null;
	
	var latitudInicial = 39.977120098439634;
	var longitudInicial = -4.658203125;
	var altura = 4;
	var nAltura = 0;
	var direccion = '';
	

	function initialize(x, y, h, d) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        //map.removeMapType(G_HYBRID_MAP);  
		geocoder = new GClientGeocoder();  
		//alert(centros.length);
		map.setCenter(new GLatLng(latitudInicial, longitudInicial), altura);
		mapas1.latitud.value = "39.977120098439634";
		mapas1.longitud.value ="-4.658203125";
		map.enableScrollWheelZoom(); 
		GEvent.addListener(map, "zoomend", function (antiguoZoom, nuevoZoom){
   			//alert("Has cambiado el zoom del mapa.\nAntiguo Zoom: " + antiguoZoom + "\nNuevo Zoom: " + nuevoZoom);
			altura = nuevoZoom;
			//alert(altura);
			var links = '';
			var direccion = mapas1.direccion.value;
			if(direccion != '')
				links = '<a href="http://mapas.calvogar.com/index.php?d='+direccion+'&h='+altura+'">http://mapas.calvogar.com/index.php?d='+direccion+'&h='+altura+'</a><br>';
			var y = mapas1.latitud.value;
			var x = mapas1.longitud.value;
			if(y!='' && x!=''){			
				links += '<a href="http://mapas.calvogar.com/index.php?x='+x+'&y='+y+'">http://mapas.calvogar.com/index.php?x='+x+'&y='+y+'&h='+altura+'</a>';
			}
			
			document.getElementById('urlWeb').innerHTML=links;
		});
		
		document.getElementById('urlWeb').innerHTML='<a href="http://mapas.calvogar.com/index.php?x='+longitudInicial+'&y='+latitudInicial+'&h='+altura+'">http://mapas.calvogar.com/index.php?x='+longitudInicial+'&y='+latitudInicial+'&h='+altura+'</a>';
			 
		var mapControl = new GMapTypeControl();
		var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0,0));
		map.addControl(mapControl, topRight); 	
		map.addControl(new GLargeMapControl());			
		var overviewmap = new GOverviewMapControl(new GSize(100,100)) 
      	map.addControl(overviewmap); 
      	overviewmap.hide(true); 	
		ponerPunto(map.getCenter().y,map.getCenter().x);
		
		if(direccion!=''){
			buscarDireccion();			
		}
      }
    }
	
	function buscarDireccion(){
		var direccion = mapas1.direccion.value;
		geocoder.getLatLng(direccion, function(point){
			if (!point) {
				alert(direccion + " no encontrada.");
			} 
			else {
				if(nAltura!=0){
					map.setCenter(point, nAltura);
					nAltura = 0;
				}
				else{					
					map.setCenter(point, 13);
				}
				mapas1.latitud.value = point.y;
				mapas1.longitud.value = point.x;
				puntoMovil.setPoint(point);
				document.getElementById('urlWeb').innerHTML='<a href="http://mapas.calvogar.com/index.php?d='+direccion+'&h='+altura+'">http://mapas.calvogar.com/index.php?d='+direccion+'&h='+altura+'</a><br><a href="http://mapas.calvogar.com/index.php?x='+point.x+'&y='+point.y+'&h='+altura+'">http://mapas.calvogar.com/index.php?x='+point.x+'&y='+point.y+'&h='+altura+'</a>';
				}
			}  
		);
	}
	
	function buscarPunto(){
		var latitud = mapas1.latitud.value;
		var longitud = mapas1.longitud.value;
		var point = new GLatLng(latitud, longitud);
		map.setCenter(point, 10); 
		puntoMovil.setPoint(point);
		document.getElementById('urlWeb').innerHTML='<a href="http://mapas.calvogar.com/index.php?x='+longitud+'&y='+latitud+'&h='+altura+'">http://mapas.calvogar.com/index.php?x='+longitud+'&y='+latitud+'&h='+altura+'</a>';
	}
	
	function obtenerDireccion(point){
		geocoder.getLocations(point, function(addresses) {
		 if(addresses.Status.code != 200) {
		   alert("Ninguna dirección en " + point.toUrlValue());
		   document.getElementById('urlWeb').innerHTML='<a href="http://mapas.calvogar.com/index.php?x='+point.x+'&y='+point.y+'&h='+altura+'">http://mapas.calvogar.com/index.php?x='+point.x+'&y='+point.y+'&h='+altura+'</a>';
		   mapas1.direccion.value = '';
		 } else {
		   mapas1.direccion.value = addresses.Placemark[0].address;		   
		   //map.openInfoWindow(point, result.address);
		   document.getElementById('urlWeb').innerHTML='<a href="http://mapas.calvogar.com/index.php?d='+addresses.Placemark[0].address+'&h='+altura+'">http://mapas.calvogar.com/index.php?d='+addresses.Placemark[0].address+'&h='+altura+'</a><br><a href="http://mapas.calvogar.com/index.php?x='+point.x+'&y='+point.y+'&h='+altura+'">http://mapas.calvogar.com/index.php?x='+point.x+'&y='+point.y+'&h='+altura+'</a>';
		 }
		});
	}
	
	function centrarMapa(latitud, longitud) {
		var point = new GLatLng(latitud, longitud);
    	map.setCenter(point, 10);  
        map.addOverlay(marker);      
    }
	
	function centrarPoint() {
		var punto = puntoMovil.getPoint();
    	map.setCenter(punto, altura);     
    }

    function ponerPunto(latitud, longitud) {
		var point = new GLatLng(latitud, longitud);
    	puntoMovil = new GMarker(point, {draggable: true});
		GEvent.addListener(puntoMovil, "dragend", function() {
			var punto = puntoMovil.getPoint();
			mapas1.latitud.value = punto.y;
			mapas1.longitud.value = punto.x;	
			obtenerDireccion(punto);
		});
		GEvent.addListener(puntoMovil, "drag", function() {
			var punto = puntoMovil.getPoint();
			mapas1.latitud.value = punto.y;
			mapas1.longitud.value = punto.x;
		});
        map.addOverlay(puntoMovil);
    }
