
var geocoder;
var map;
var pueblo = "Instituto Javier de Benito";

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 s'ha trobat cap localització geogràfica que correspongui a alguna de les adreces indicades. Pot ser que alguna de les adreces sigui massa nova o pot ser que sigui incorrecta.\nCodi d'error: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("La sol·licitud de geocidificació o alguna de les adreces no es pot processar correctament. La raó exacta de l'error no es coneix.\nCodi d'error: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("El paràmetre HTTP q no existeix o no té assignat cap valor. Per a sol·licituds de geocodificació, això vol dir que no s'ha especificat cap consulta a l'entrada.\nCodi d'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("La clau proporcionada no és vàlida o no correspon al domini per al qual es va concedir.\nCodi d'error: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("La sol·licitud d'adreces no s'ha pogut processar adequadament.\nCodi d'error: " + gdir.getStatus().code);
	
   else alert("Ha succeït un error desconegut.");
   
}

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

// This function adds the point to the map
function addToMap(response)
{
	// Retrieve the object
	place = response.Placemark[0];
	
	// Retrieve the latitude and longitude
	point = new GLatLng(place.Point.coordinates[1],
	place.Point.coordinates[0]);
	
	// Center the map on this point
	map.setCenter(point, 14);
	  
	// Create a marker
	marker = new GMarker(point);
	
	
	// Add the marker to map
	map.addOverlay(marker);
	
	//map.setUIToDefault();
	map.removeMapType(G_PHYSICAL_MAP);
	//var mapControl = new GMapTypeControl();
	//map.addControl(mapControl);
	map.enableScrollWheelZoom();



	// Add address information to marker
	/*marker.openInfoWindowHtml('<strong>' + pueblo + '</strong><br />' +
		'Gran Vía Carles III, 71-75<br/>08028 Barcelona');*/
}

function crea_mapa(capa, direccion) {
	if (GBrowserIsCompatible()) {
	  
	
      // Create new map object
      map = new GMap2(capa, {size:new GSize($(capa).width(),350)});
	
	gdir = new GDirections(map, document.getElementById("directions"));

	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
	
	//gdir.load(direccion, { "locale": "ca_ES" } );

      // Create new geocoding object
      geocoder = new GClientGeocoder();

      // Retrieve location information, pass it to addToMap()
      geocoder.getLocations(direccion, addToMap);

	}//fin if (GBrowserIsCompatible())
}// function load()
	

//GEvent.addDomListener(window, "load", load);
GEvent.addDomListener(window, "unload", GUnload);