var icon = new GIcon();

var map;
var geocoder = new GClientGeocoder();

var gdir;
var addressMarker;

//http://www.google.es/maps?f=q&source=s_q&hl=es&geocode=&q=El+Plant%C3%ADo,+Burgos&sll=42.344514,-3.680731&sspn=0.002875,0.004565&ie=UTF8&hq=El+Plant%C3%ADo,&hnear=Burgos,+Castilla+y+Le%C3%B3n&ll=42.344867,-3.681149&spn=0.002875,0.004565&t=h&z=18&iwloc=A

var latPunto = 42.344867; /*+ arriba/- abajo*/
var longPunto = -3.681149; /*+ izquierda / - derecha*/

var punto = new GLatLng(latPunto,longPunto);

function load(){
	if (GBrowserIsCompatible()){

		//Icono personalizado
		//icon.image = "imgs/g_maps_icon.png";
		//icon.shadow = "imgs/g_maps_shadow.png";
		//icon.iconSize = new GSize(36, 36);
		//icon.shadowSize = new GSize(90, 37);
		//icon.iconAnchor = new GPoint(16, 35);
		//icon.infoWindowAnchor = new GPoint(23, 8);

		// create the map object
		map = new GMap2(document.getElementById("div_mapa"));
		map.setCenter(new GLatLng(latPunto, longPunto), 17);
		
		/*map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());*/
		
		map.setUIToDefault();
		map.setMapType(G_HYBRID_MAP);
		// G_NORMAL_MAP Mapa normal
		// G_SATELLITE_MAP Mapa de satélite
		// G_HYBRID_MAP Combina el mapa normal y de satélite
		// G_PHYSICAL_MAP Mapa de relieve

		var marker = new GMarker(punto);
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml("<div style='font-size: 0.80em;color:#000;font-family:Arial;'><strong>Estadio Municipal de El Plantío</strong><br /><span style='font-size: 0.85em;color: #666;'>C/ Cruz Roja s/n<br />09006 Burgos, España.</span></div>");
		});

		gdir = new GDirections(map, document.getElementById("div_direcciones"));
		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 se ha encontrado correspondencia geográfica con la dirección especificada. Esto puede ocurrir si la dirección es relativamente nueva o es incorrecta.\nError code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("No se ha podido procesar la solicitud con éxito por una razón desconocida.\n Error: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("Falta un parámetro HTTP.\n Error: " + gdir.getStatus().code);

	//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 se puede conectar con Google Maps porque la clave introducida no es correcta. \n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("La petición no ha podido ser analizada con éxito.\n Error code: " + gdir.getStatus().code);

	else alert("Se ha producido un error desconocido");
}

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...
}

// GEOCODER
function searchAddress(sPoblacion){
	geocoder.getLatLng(sPoblacion,function(point){
	if (!point) {
	alert("Población desconocida");      
	}else{
	alert(point);
	map.setCenter(point, 16);        
	var marker = new GMarker(point);        
	map.addOverlay(marker);
	//alert(point);
	//marker.openInfoWindowHtml("CÃ³digo HTML");      
	}
	});
}

function cargarRuta(){
	document.getElementById('div_direcciones').style.display = 'block';
	setDirections("'" + document.getElementById('origen').value + "'", 'Estadio Municipal de El Plantío@'+latPunto+','+longPunto+'', 'es_ES');
}
