// JavaScript Document
var geocoder;
var map; 
var gdirections;
var romaLL;

//carica la mappa di google
function load() {
  if (GBrowserIsCompatible()) {

	map = new GMap2(document.getElementById("map"));
	geocoder = new GClientGeocoder();
	gdirections = new GDirections(map, document.getElementById("directions"));
	
	var latlng = geocoder.getLatLng(			
			"Roma, Italia",
			function(point) {
			  if (!point) {
				alert(address + " not found");
			  } else {
				map.setCenter(point, 2);				
				romaLL = point;				
			  }
			}
		  )
		  
		  GEvent.addListener(map, "click", onMapClick);
	
	//aggiunge i controlli alla mappa
	map.addControl(new GScaleControl());
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	
  }

}

//handler del click sulla mappa
function onMapClick(mymarker,point) {												
	if(mymarker){												
		//seleziona il partner nella select
		SelectPartner(mymarker.partnerid);		
		//visualizza il baloon sul marker
		GetPartnerDetails(mymarker.partnerid);
		
	}	
}

//costruisce la mappa nel div
function getMap() {
	return new GMap2(document.getElementById("map"));
}

function getDirections() {
	return new GDirections(getMap(),document.getElementById("directions"));
}

function getDirectionString(sFrom, sTo) {
	return sFrom + " to " + sTo;
}

function getDirectionOptions() {
	var opt = {
		'locale' : 'it_IT',
		'getPolyline' : true,
		'getSteps' : true,
		'preserveViewport' : false
  }
	return opt;
}

function onGDirectionsLoad(){ 
	  // Use this function to access information about the latest load()
	  // results.

	  // e.g.
	// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	alert(gdirections.getStatus().code);
	// and yada yada yada...
	}

function handleErrors(){
   if (gdirections.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdirections.getStatus().code);
   else if (gdirections.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: " + gdirections.getStatus().code);
   
   else if (gdirections.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: " + gdirections.getStatus().code);

//   else if (gdirections.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: " + gdirections.getStatus().code);
	 
   else if (gdirections.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: " + gdirections.getStatus().code);

   else if (gdirections.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdirections.getStatus().code);
	
   else alert("An unknown error occurred.");
   
}