// JavaScript Document
/*     GOOGLE MAP     */

function googlekey(keyabajo,kayarriba,domabajo,domarriba){
	var documento = self.location.href.match( /\/([^/]+)$/ )[1];
	if (document.location.href == 'http://servidor.'+ domabajo +'/'+ documento)
	{
		document.write("<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key="+ keyabajo +"' type='text/javascript'></script>");
	}
	else
	{
		if (document.location.href == 'http://'+ domarriba +'/'+ documento);
		{
			document.write("<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key="+ kayarriba +"' type='text/javascript'></script>");
		}
	}
}

function mostrar_mapa(){
	var capa_imprimir = document.getElementById("capa_imprimir_mapa");
	capa_imprimir.style.display="";
	capa_imprimir = document.getElementById("capa_imprimir_mapa2");
	capa_imprimir.style.display="";
}

	var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function onLoad() {
      if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setUIToDefault();
		map.setCenter(new GLatLng(36.814232,-2.639465), 10);
		
		var icon = new GIcon();
		icon.image = "../imagenes/mapas/aeropuerto.png";
		icon.iconSize = new GSize(32, 32);
		icon.iconAnchor = new GPoint(16, 16);
		icon.infoWindowAnchor = new GPoint(16, 16);
		
		var icon2 = new GIcon();
		icon2.image = "../imagenes/mapas/estacion.png";
		icon2.iconSize = new GSize(26, 28);
		icon2.iconAnchor = new GPoint(12, 14);
		icon2.infoWindowAnchor = new GPoint(12, 14);
		
		function createMarker(point,html,icono) {
			html = "<div style='text-align:left;'>" + html + "<\/div>";
			var marker = new GMarker(point,icono);
		
			GEvent.addListener(marker, "click", function() {
				if (map.getZoom()<17){
				map.setCenter(point, 17);
				}
			});
		
			GEvent.addListener(marker, "mouseover", function() {
				marker.openInfoWindow(html);
			});
		
			GEvent.addListener(marker, "dblclick", function() {
				if (map.getZoom()<17){
					map.setCenter(point, 17);
				}
			});

		return marker;
		}
		
		var punto = new GLatLng(36.844186,-2.370815);
		var marca = createMarker(punto,"<p><strong>Aeropuerto de Alemr&iacute;a<\/strong><br />Carretera de N&iacute;jar Km 9, 04006 Almer&iacute;<br />Telf.: <strong>902 404 704<\/strong> <br /><br />&iquest;Vienes desde aqu&iacute;?<\/p> <img src='../imagenes/mapas/boton_mostrar.gif' style='cursor:pointer;' onclick='setDirections(\"36.844186,-2.370815\", \"El Ejido\", \"es\");' \/>",icon);
		map.addOverlay(marca);
		
		var punto2 = new GLatLng(36.835215,-2.455772);
		var marca2 = createMarker(punto2,"<p><strong>Estaci&oacute;n intermodal<\/strong><br />Estaci&oacute;n S/N, 04006 Almer&iacute;a<br />Telf.: <strong>902 432 343<\/strong> <br /><br />&iquest;Vienes desde aqu&iacute;?<\/p> <img src='../imagenes/mapas/boton_mostrar.gif' style='cursor:pointer;' onclick='setDirections(\"36.83525,-2.455782\", \"El Ejido\", \"es\");' \/>",icon2);
		map.addOverlay(marca2);
		
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
      }
    }
	
    function setDirections(fromAddress, toAddress, locale) {
		gdir.load("from: " + fromAddress + " to: " + toAddress,
		{ "locale": locale });
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponde con ninguna posicion geografica o no se puede realizar en coche.");
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("No corresponde con ninguna posicion geografica o no se puede realizar en coche.");
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("No corresponde con ninguna posicion geografica o no se puede realizar en coche.");

		//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
		//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("No corresponde con ninguna posicion geografica o no se puede realizar en coche.");

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("No corresponde con ninguna posicion geografica o no se puede realizar en coche.");
	    
	   else alert("No corresponde con ninguna posicion geografica o no se puede realizar en coche.");
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      //document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
