// set the 2 location
var locationOssendrecht = new GLatLng(51.391584, 4.351717);
var locationAlmere = new GLatLng(52.408789, 5.248989);
var locationAntwerpen = new GLatLng(51.252024, 4.394498);
var locationRotterdam = new GLatLng(51.868037, 4.259967);
var locationVlissingen = new GLatLng(51.466713, 3.683764);

// set the 2 markers for the 2 locations
var markerOssendrecht = new GMarker(locationOssendrecht);
var markerAlmere = new GMarker(locationAlmere);
var markerAntwerpen = new GMarker(locationAntwerpen);
var markerRotterdam = new GMarker(locationRotterdam);
var markerVlissingen = new GMarker(locationVlissingen);

function setUp() 
{
	// if the browser is google maps compatible
	if (GBrowserIsCompatible()) {
							
	// make a new map, and set a center point variable
	var map = new GMap2(document.getElementById("map"));
	var center = new GLatLng(52.033062969886096, 5.1);

	// actually center the map to the center point and set the zoom level
	map.setCenter(center, 7);
	map.addControl(new GLargeMapControl());

	// add markers to the map
	map.addOverlay(markerOssendrecht);
	map.addOverlay(markerAlmere);
	map.addOverlay(markerAntwerpen);
	map.addOverlay(markerRotterdam);
	map.addOverlay(markerVlissingen);

	GEvent.addListener(markerOssendrecht, "click", function(){
		markerOssendrecht.openInfoWindowHtml("Hoofdkantoor<br />Putsmolentje 8<br />4641 SH Ossendrecht<br /><br />T +31 (0)164-674591<br />F +31 (0)164-673892");
	});

	GEvent.addListener(markerAlmere, "click", function(){
		markerAlmere.openInfoWindowHtml("Afhaal- en distributiecentrum<br />Damsluisweg 15<br />1332 EA Almere<br /><br />T +31 (0)36-5490999<br />F +31 (0)36-5499939");
	});

	GEvent.addListener(markerAntwerpen, "click", function(){
		markerAntwerpen.openInfoWindowHtml("Opslag haven Antwerpen<br />Haven 73 - Ouland 23<br />B-2030 Antwerpen");
	});

	GEvent.addListener(markerRotterdam, "click", function(){
		markerRotterdam.openInfoWindowHtml("Opslag haven Rotterdam<br />Nieuwesluisweg 170<br />Haven no. 5028<br />3197KV Rotterdam");
	});

	GEvent.addListener(markerVlissingen, "click", function(){
		markerVlissingen.openInfoWindowHtml("Opslag haven Vlissingen<br />Engelandweg 12<br />4389PC Ritthem");
	});

	// set up directions object and actions for it
	gdir = new GDirections(map, document.getElementById("directions"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
  }
}

function route(from, to, lang)
{
	// if the location is location 1
	if (to == 1)
	{
		setDirections(from, "@" + markerOssendrecht.getPoint().lat() + ", " + markerOssendrecht.getPoint().lng(), lang);
	}
	else if (to == 2)
	{
		setDirections(from, "@" + markerAlmere.getPoint().lat() + ", " + markerAlmere.getPoint().lng(), lang);
	}
	else if (to == 3)
	{
		setDirections(from, "@" + markerAntwerpen.getPoint().lat() + ", " + markerAntwerpen.getPoint().lng(), lang);
	}
	else if (to == 4)
	{
		setDirections(from, "@" + markerRotterdam.getPoint().lat() + ", " + markerRotterdam.getPoint().lng(), lang);
	}
	else if (to == 5)
	{
		setDirections(from, "@" + markerVlissingen.getPoint().lat() + ", " + markerVlissingen.getPoint().lng(), lang);
	}
}
   
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 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.");
}