var map;
var gdir;
var geocoder;
var casa;
var address;
var lugar = "Aranda de Duero";

function mapLoadInit(direccion,zoom,tipo,latitud,longitud)
{
	if (GBrowserIsCompatible())
	{		
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			direccion,
			function(point)
			{
				if (latitud != '' && longitud != '') {
					mapLoad(latitud, longitud, zoom, tipo);
				} else {
					if (point) {
						lng = point.lng();
						lat = point.lat();
						mapLoad(lat, lng, zoom, tipo);
					} 
				}
			}
		);	
	}
}

function mapLoad(latitud,longitud,zoom,tipo)
{
	if(zoom == undefined)
	{
		zoom = 10;
	}
	// G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, and G_PHYSICAL_MAP
	if(tipo == undefined)
	{
		tipo = G_PHYSICAL_MAP;
	}
	
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("mapa_ruta"));
		map.addControl(new GLargeMapControl());
		var position = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
		map.addControl(new GMenuMapTypeControl(), position);	
		map.addMapType(G_PHYSICAL_MAP);
		var point = new GLatLng(latitud, longitud);
		map.setCenter(point, zoom, tipo);
		var marker = new GMarker(point);
		map.addOverlay(marker);		
	}
}

function mapLoadDirections(from)
{
	document.getElementById('direcciones').innerHTML = '';
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("mapa_ruta"));
		map.addMapType(G_PHYSICAL_MAP);
		casa = 'Aranda de Duero';
		if (from != "") 
		{
			lugar = from;
		}
		geocoder = new GClientGeocoder();
		geocoder.getLocations(casa, showAddress);
		map.addControl(new GLargeMapControl());
		var position = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
		map.addControl(new GMenuMapTypeControl(), position);			
		gdir = new GDirections(map, document.getElementById("direcciones"));
		GEvent.addListener(gdir, "error", handleErrors);	
	}
}

function showAddress(response)
{
	  if (!response || response.Status.code != 200)
	  {
	    alert("Status Code:" + response.Status.code);
	  } 
	  else 
	  {
	    place = response.Placemark[0];
	    address = place.address;
		setDirections(lugar, "es");
	  }
}

function setDirections(fromAddress) 
{
	gdir.load("from: " + fromAddress + " to: " + address,
	{ "locale": "es_ES" });
}

function handleErrors()
{
	document.getElementById('direcciones').style.display = 'none';
	
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Por favor revisa la localidad o introduce la busqueda de la siguiente forma: 'Localidad, Provincia'");
	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("La direccion requerida no ha sido procesada correctamente.\n Error code: " + gdir.getStatus().code);
	else alert("Ha ocurrido un error desconocido por google.");
}

function direcciones_submit(from)
{
	document.getElementById('direcciones').style.display = 'block';
	mapLoadDirections(from);
}
