var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("mapa_ruta"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());


		map.setCenter(new GLatLng(40.446456,-3.48418));
		map.setZoom(16);
		
		//iconito rojo
		var icon = new GIcon();
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
	
		//marker
		var point = new GLatLng(40.446456,-3.48418);
		var marker = new GMarker(point,icon);
		texto_html = '<div style="font-size:11px;width:200px"> Micoinsa <br/>C/ Primavera 15, Torrej&oacute;n de ardoz, Madrid </div>';
		
		map.openInfoWindowHtml(point,texto_html);
	    
		//dibujar
		map.addOverlay(marker);
		
		
		gdir = new GDirections(map, document.getElementById("direcciones"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);



		//setDirections("Madrid", "Primavera 15, Torrejón de ardoz, Madrid", "es");
	}
}

function setDirections( fromAddress, toAddress, locale ) {
	gdir.load("from: " + fromAddress + " to: " + toAddress,	{ "locale": locale });
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Dirección no disponible.\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(){
}

Event.observe(window, 'load', function() {
  initialize();
});