var map;
var gdir;
var geocoder;
var casa;
var address;
var lugar = "Santander";

function mapLoadInit(direccion)
{
	if (GBrowserIsCompatible())
	{		
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			direccion,
			function(point)
			{
				if (point)
				{
					lng = point.lng();
					lat = point.lat();
					mapLoad(lat, lng);
				} 
				else {	
					<!--alert(direccion + " lugar no encontrado");-->
			   }
			}
		);	
	}
}

function mapLoad(latitud,longitud)
{
	if (GBrowserIsCompatible())
	{
		var icono = new GIcon();
		icono.image = 'images/icono_google.png';
		icono.iconSize = new GSize(26,20);
		icono.iconAnchor = new GPoint(26,20);
	
		map = new GMap2(document.getElementById("mapa_ruta"));
		/*map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());*/
		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, 14, G_SATELLITE_MAP);
		var marker = new GMarker(point, icono);
		map.addOverlay(marker);		
	}
}

function mapLoadDirections(from,latitud,longitud)
{
	document.getElementById('direcciones').innerHTML = '';
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("mapa_ruta"));
		/*map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());*/
		map.addMapType(G_PHYSICAL_MAP);
		casa = new GLatLng(latitud, longitud);
		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()
{
	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 comoLlegar(desde, hasta, valLT, lt , lg)
{
	if (GBrowserIsCompatible())
	{
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			hasta,
			function(point)
			{
				if (point)
				{
					if (valLT == 1) {
						lng = lg;
						lat = lt;
					} else {
						lng = point.lng();
						lat = point.lat();
					}
					direcciones_submit(desde, lat, lng);	
				}
				else {
					<!--alert(hasta + " lugar no encontrado");-->	
			   }
			}
		);	
	}	
}

function direcciones_submit(from,latitud,longitud)
{
	window.location='#verruta'
	document.getElementById('direcciones').style.display = 'block';
	mapLoadDirections(from,latitud,longitud);
}
