var map,point,marker;
var gdir;
var geocoder = null;
var addressMarker;

var icon = new GIcon();
icon.image = "/imagenes/icon.png";
icon.shadow = "/imagenes/icon_s.png";
icon.iconSize = new GSize(56, 32);
icon.shadowSize = new GSize(80, 32);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(12, 20);


	var Mapa = new Object();
		 Mapa.MId="map";
		 Mapa.MLat = 42.813515;
 		 Mapa.MLng = -1.63685;	 
		 Mapa.MZoom = 17;

	var Punto = new Object();
		 Punto.PLat = -1.63685;
		 Punto.PLng = 42.81370;
		 Punto.PIcon = icon;
		 Punto.PTab1="rtvena";
		 Punto.PTab2="Como llegar";
		 Punto.PTab1Info="<p class='pGoogle'><strong>Centro de encuentros y Servicios Profesionales</strong><br />Avda Baja Navarra 57 - 31002 Pamplona<br /><span>Tel&eacute;fono:</span> 948 22 60 93<br /></p>";
		 Punto.PTab2Info="<p class='pGoogle'>Ej.:  Calle las delicias, Zaragoza </p><form id='frmGoogle' action='http://maps.google.es/maps' method='get' target='_target'><input type='text' name='saddr' id='saddr'/><input type='submit' class='btn' value='Enviar' /><input type='hidden' name='daddr' id='daddr' value='42.81352,-1.63685' class='oculto' /><br class='clear' /></form>";		 

function crear_mapa(Mapa){
	//Map
	map = new GMap2(document.getElementById(Mapa.MId));
	map.setCenter(new GLatLng(Mapa.MLat,Mapa.MLng), Mapa.MZoom);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl(1));
	gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
}

function crear_punto(Punto){
	// Emplazar marcadores
	point = new GPoint(Punto.PLat,Punto.PLng);
	marker = new GMarker(point, Punto.PIcon);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
                       var html, html2
						map.savePosition();
						html = Punto.PTab1Info;
							html2 =	Punto.PTab2Info;
			
						var infoTabs = [
						new GInfoWindowTab(Punto.PTab1, html),
						new GInfoWindowTab(Punto.PTab2,html2)
						];
						marker.openInfoWindowTabsHtml(infoTabs);
                     });
}


function mostrar_mapa(Mapa){
			map.panTo(new GLatLng(Mapa.MLat, Mapa.MLng));
}
function mostrar_punto(Punto){
	marker.setPoint(new GLatLng(Punto.PLat,Punto.PLng));
	GEvent.addListener(marker, "click", function() {
                       var html, html2
						map.savePosition();
						html = Punto.PTab1Info;
							html2 =	Punto.PTab2Info;
			
						var infoTabs = [
						new GInfoWindowTab(Punto.PTab1, html),
						new GInfoWindowTab(Punto.PTab2,html2)
						];
						marker.openInfoWindowTabsHtml(infoTabs);
                     });
	crear_punto(Punto);
}

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 corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	 
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
   else alert("An unknown error occurred.");
   
}

function onGDirectionsLoad(){ 
	map.clearOverlays();
}