window.addEvent('load', load);
window.addEvent('unload', GUnload);

var map;
var gdir;
var geocoder = null;
var addressMarker;

function load() {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("map_canvas"));

		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	}
}

function setDirections(fromAddress, toAddress, locale) {
	// hide previous errors
	hideErrors();
	
	if (fromAddress != '') {
		gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
	}
}

function handleErrors() {

	var error_code = gdir.getStatus().code;

	if (error_code == G_GEO_MISSING_QUERY) {
		// 601 - 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.
		showError('Het is helaas niet gelukt om een route te plannen vanuit het door u opgegeven adres. Error code: ' + error_code);
	} else if (error_code == G_GEO_UNKNOWN_ADDRESS) {
		// 602 - 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.
		showError('Het door u ingevoerde adres kon niet worden gevonden. Dit kan komen doordat het een relatief nieuw adres is, of doordat het adres onjuist is ingevoerd.<br />Voert u het adres alstublieft als volgt in: "Straatnaam Huisnummer, Plaatsnaam".');
	} else if (error_code == G_GEO_UNAVAILABLE_ADDRESS) {
		// 603 - The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.
		showError('Het is helaas niet gelukt om een route te plannen vanuit het door u opgegeven adres. Error code: ' + error_code);	
	} else if (error_code == G_GEO_UNKNOWN_DIRECTIONS) {
		// 604 - Could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region.
		showError('Het is helaas niet gelukt om een route te plannen vanuit het door u opgegeven adres. Error code: ' + error_code);
	} else if (error_code == G_GEO_BAD_KEY) {
		// 610 - The given key is either invalid or does not match the domain for which it was given.
		showError('The given key is either invalid or does not match the domain for which it was given. Error code: ' + error_code);
	} else if (error_code == G_GEO_BAD_REQUEST) {
		// 400 - A directions request could not be successfully parsed.
		showError('Het is helaas niet gelukt om een route te plannen vanuit het door u opgegeven adres. Error code: ' + error_code);
	} else if (error_code == G_GEO_SERVER_ERROR) {
		// 500 - A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.
		showError('Het is helaas niet gelukt om een route te plannen vanuit het door u opgegeven adres. Error code: ' + error_code);
	} else {
		// An unknown error occurred.
		showError('Het is helaas niet gelukt om een route te plannen vanuit het door u opgegeven adres. Error code: ' + error_code);
	}
}

function onGDirectionsLoad(){
	var route_result = document.getElementById('route_result');
	if (route_result.className == 'hidden') {
		route_result.className = ''; // show result
	}
}

function showError(error_text) {
	var route_result = document.getElementById('route_result');
	var route_error = document.getElementById('route_error');

	if (route_result.className == '') {
		route_result.className = 'hidden'; // hide previous results
	}

	route_error.className = ''; // show error
	route_error.innerHTML = '<h3>Er is een fout opgetreden</h3>' + "\n" + '<p>' + error_text + '</p>';
}

function hideErrors() {
	document.getElementById('route_error').className = 'hidden'; // hide errors
}
