window.onload = function() { load(); }
window.onunload = function() { GUnload(); }

if (GBrowserIsCompatible()) {
	var marker;
	var gmarkers = [];
	var gmaps_i = 0;
	var to_html = '';
	var taal = "nl_nl";
}

//<![CDATA[
function load() {
	if (GBrowserIsCompatible()) {
		// Create new map
		var map = new GMap2(document.getElementById("map"));
		
		// Add map controls
		//map.addControl(new GSmallMapControl());
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMapTypeControl());
		//map.addControl(new GOverviewMapControl());
		//map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
	
		// Set map screen position
		map.setCenter(new GLatLng(51.6266, 4.835583), 15);
		map.setMapType(G_HYBRID_MAP);
		// Create marker @ point
		
		// Set marker
		//<a href='http://maps.google.nl/?daddr=Straat+Huisnummer%2COosterhout%2CNederland&z=16'>Klik hier om uw route te bepalen.</a>
		marker = createMarker(new GLatLng(51.6266, 4.835583), 'Akkermans Boomkwekerijen B.V.', "<font face='verdana' size='2pt'><p><b><font color='#436019'>Akkermans Boomkwekerijen B.V.</font></b><br/><font color='#555555'>Bredaseweg 150<br/>4904SC Oosterhout (N.Br.)<br/>Nederland</font></p><p><img src='http://www.page-online.nl/po/client/5/content/images/akkermans_bedrijf_klein.jpg' title='Boomkwekerij Akkermans B.V. - Bedrijf' alt='Boomkwekerij Akkermans B.V. - Bedrijf'></p>");
		map.addOverlay(marker);
        gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "error", handleErrors);
		GEvent.trigger(marker, "click");
	}
}

// A function to create the marker and set up the event window
function createMarker(point,name, html) {
    var marker = new GMarker(point);
    // The info window version with the "to here" form open
    to_html = html + 'Ik vertrek vanaf: <form action="javascript:getDirections()">' +
			         '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br />' +
					 '<input value="Routebeschrijving" TYPE="submit">' +
					 '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + '"/>';
					 
    // The inactive version of the direction info
    
	var infoTabs = [  
		new GInfoWindowTab("Adres", html + "<a href='javascript:tohere("+ gmaps_i + ")'>Plan route...</a>"),  
		new GInfoWindowTab("Contact", "<font face='verdana' size='2pt'><b><font color='#436019'>Contact</font></b><p><font color='#555555'><b>Telefoon-en Faxnummers</b><br/>t. +31 (0)162-453204<br/>f. +31 (0)162-454358<br/><br/></font>")
	];
	
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowTabsHtml(infoTabs); });
    gmaps_i++;
    gmarkers[gmaps_i] = marker;
	return marker;
}

function tohere(c) { 
	marker.openInfoWindowHtml(to_html);   
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	alert("Het door u ingevoerde adres is niet bekendnProbeer de volgende notatie: Dorpstraat 18 Brunssum.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.");
}

// ===== request the directions =====
function getDirections() {
	var saddr = document.getElementById("saddr").value
	var daddr = document.getElementById("daddr").value
	gdir.load("from: " + saddr + " to: " + daddr, { "locale": taal }); 
	document.getElementById('print').style.visibility = 'visible';
}
//]]>
