var nFrom=0;
var nTo=0;
var gdir, fromAddress, toAddress;

$(document).ready(function() {

	initialize();
	$(".partenze").click(onClickPartenza);
	$(".manifestazioni").click(onClickManifestazioni);
	

});

function onClickPartenza()
{
	nFrom=$(this).attr("value");
	showPercorso();
}

function onClickManifestazioni()
{
	nTo=$(this).attr("value");
	showPercorso();
}

function showPercorso()
{
	percorso="from: " + arrFromAddress[nFrom] + " to: " + arrToAddress[nTo];
	gdir.load(percorso, { "locale": "en" });
}




var arrFromAddress=Array("Milano Italy","Torino Italy","Locarno Switzerland","Briga Switzerland");
var arrToAddress=Array("Cascata del Toce Formazza VB Italy","Mergozzo VB Italy","Arona NO italy","Santa Maria Maggiore VB italy","Verbania VB italy","omegna VB italy","omegna VB italy");

var arrLocalita= Array(

	Array(46.4088,8.4133,"Cascata del Toce - Formazza"),
	Array(45.9624,8.4506,"Lake Mergozzo"),
	Array(45.7570,8.5600,"Arona - Lake Maggiore"),
	Array(46.1361,8.4634,"Santa Maria Maggiore - Valley Vigezzo"),
	Array(45.9215,8.5516,"Verbania - Lake Maggiore"),
	Array(45.8761,8.4088,"Omegna - Lake Orta")
	
	);


	


/*
**
* Bootstrap function to setup map and apply
* custom company marker
*/
function initialize() {
	if (GBrowserIsCompatible()) {
	//settings
	
	
	var defaultZoomLevel  = 9;
	//end settings
	
	//setup elements
	map   = new GMap2(document.getElementById("map_canvas"));
	gdir  = new GDirections(map, document.getElementById("directions"));
	
	//error handler
	GEvent.addListener(gdir, "error", handleErrors);
	
	map.setCenter(new GLatLng(45.9676,8.6533 ), defaultZoomLevel);
	map.addControl( new GLargeMapControl3D ());
	
	//mettiamo i markers
	for (var i in arrLocalita) {
	
		var ThisMarker=new GMarker(new GLatLng(arrLocalita[i][0],arrLocalita[i][1]));
		map.addOverlay(ThisMarker);
		ThisMarker.bindInfoWindowHtml(arrLocalita[i][2]);
	
	}
	

  }
}

  function handleErrors(){
     if (gdir.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: " + 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.");
  }
