$(function() {
	var marsden = "Ralph Trimmer Dr, New Zealand";
	var auckland = "Auckland, New Zealand";
	
	$('#aucklandLink').click(function() { newDirection('auckland'); });
	$('#airportLink').click(function() { newDirection('airport'); });

	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2(document.getElementById("mapview"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		var gdir = new GDirections(map);
		
		GEvent.addListener(gdir, "error", handleErrors);
		
		setDirections(auckland, marsden, "en_US");
		
	}

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

	function newDirection(from)
	{
		var marsden = "Ralph Trimmer Dr, New Zealand";
		var auckland = "Auckland, New Zealand";
		
		if(from == "airport")
		{
			setDirections("Onerahi, New Zealand" , marsden, "en_US");
			var directions = $('directions');
			directions.innerHTML = "<h3>Approx Time Required:</h3><p>30mins South of Whangarei</p><h3>Directions</h3><h4>From Whangarei Airport in Onerahi:</h4><ol><li>Follow the main road towards Whangarei City</li><li>Once at State Highway 1, head South towards Ruakaka (20-30mins)</li><li>Turn left at the BP Petrol station onto the Port Marsden Highway</li><li>Follow this road straight through until you get to Ralph Trimmer Drive at the end.</li><li>Take the 2nd right into the parking lot.</li></ol>";	
		}
		if(from == "auckland")
		{
			setDirections(auckland, marsden, "en_US");
			var directions = $('directions');
			directions.innerHTML = '<h3>Approx Time Required:</h3><p>1.5 Hours North of Auckland</p><h3>Directions</h3><h4>From Auckland:</h4><ol><li>Follow State Highway 1, North for about 80 minutes</li><li>Go straight past the Ruakaka turn off and turn right at the Petrol Station onto the Port Marsden Highway</li><li>Follow this road straight through until you get to Ralph Trimmer Drive at the end.</li><li>Take the 2nd right into the parking lot.</li></ol>';
		}
	}
	
	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.");
	   
	}
});

