var reasons=[];
reasons[G_GEO_SUCCESS]            = "Success";
reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";                            
reasons[G_GEO_UNKNOWN_ADDRESS]    = "Indirizzo sconosciuto: Controllare i dati immessi.";
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";

 // This function adds the point to the map
function addToMap(response){
	if (response.Status.code == G_GEO_SUCCESS){
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		map.setCenter(point, 13);
		map.setZoom(16);
		map.setMapType(G_HYBRID_MAP);
		marktemp = createMarker(point, eval("infoTabs" + contatoreMarker))
		map.addOverlay(marktemp);
		gmarkers[contatoreMarker] = marktemp;
	} else {
		var reason = "Codice " + response.Status.code;
		if (reasons[response.Status.code]) {
			reason = reasons[response.Status.code]
		} 
		alert('Errore! ' + eval("restaurant" + contatoreMarker) + ' ' + reason);
	}
	contatoreMarker = contatoreMarker + 1;
}

function createMarker(point, number) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowTabsHtml(number)
	});
	return marker;
}

function myclick(i) {
	gmarkers[i].openInfoWindowTabsHtml(eval("infoTabs" + i))
}

function loadMaps(webSiteRoot)
{
	map = new GMap2(document.getElementById("map"));
	geocoder = new GClientGeocoder();
	map.setCenter(new GLatLng(0,0),0);
	icon = new GIcon();
	icon.image = webSiteRoot + "/images/start.png";
	icon.shadow = webSiteRoot + "/images/start-shadow.png";
	icon.iconSize = new GSize(20, 35);
	icon.shadowSize = new GSize(20, 35);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);	
	geocoder.getLocations(address1, addToMap);
	map.addControl(new GSmallMapControl());
}

function addp(csurl) {
	try {
		window.external.AddSearchProvider(csurl);
	} catch (e) {
		alert("You need to use Internet Explorer (7.0 or later) or Firefox (2.0 or later) to install the OpenSearch plug-in.");
	}
}

function showAddress(address) {
 	geocoder = new GClientGeocoder();
	geocoder.getLatLng(
    address + ' livigno, Sondrio, Lombardia, Italia',
    function(point) {
      if (!point) {
        alert(address + " non trovato");
      } else {
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml("<strong>" + address + "</strong>");
      }
    }
  );
}
