//load Google Map
function initialize() {
  if (GBrowserIsCompatible()) {
  	var map = new GMap2(document.getElementById("routekaart"));
	gdir = new GDirections(map, document.getElementById("directions"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.enableScrollWheelZoom();
	map.setCenter(new GLatLng(52.31675, 5.17542), 7);
	var geocoder = new GClientGeocoder();
	//geocoder.setBaseCountryCode("NL");

	//add controls
    map.addControl(new GMapTypeControl());

	//create randomnumber to prevent caching and retrieve xml file
	var randomnumber=Math.floor(Math.random()*11111)
    GDownloadUrl("http://dmvh.letsbuildit.nl/functions/adressen.xml?random="+randomnumber, function(data, responseCode) {
    var xml = GXml.parse(data);

	//store markers in markers array
    var markers = xml.documentElement.getElementsByTagName("marker");

	// create marker icon
	var icon = new GIcon();
	icon.image = "http://dmvh.letsbuildit.nl/images/icoontje.jpg";
	icon.iconSize = new GSize(26, 21);//Afmetingen iccon
	icon.iconAnchor = new GPoint(20, 20);//Plaats van het icoon
	icon.infoWindowAnchor = new GPoint(20, 1);//Plaats van de tekstballon

	//loop over the markers array
    for (var i = 0; i < markers.length; i++) {
		var address = markers[i].getAttribute("address");
		var html = markers[i].getAttribute("html");
		showAddress(map,geocoder,address,html,icon);
    } //close for loop

	  }
	); //close GDownloadUrl

//Create marker and set up event window
function createMarker(point,html,icon){
  var marker = new GMarker(point,icon);
  GEvent.addListener(marker, "click", function() {
     marker.openInfoWindowHtml(html);
  });
  return marker;
}

//showAddress
function showAddress(map,geocoder,address,html,icon) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " niet gevonden");
      } else {
        //map.setCenter(point, 14);//Map centreren op de adressen
        var marker = createMarker(point,html+'<br/><br/>'+address,icon);
        map.addOverlay(marker);
		map.addControl(new GMapTypeControl());
      }
    }
  );
}


  } //close GBrowserIsCompatible
} //close load


function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Er kon geen lokatie gevonden worden op het door u gegeven adres, vul minimaal uw straatnaam en plaats in of uw postcode en plaats..\nError code: " + gdir.getStatus().code);
//				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);
//				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("Er kon geen lokatie gevonden worden op het door u gegeven adres, vul minimaal uw straatnaam en plaats in of uw postcode en plaats..\n Error code: " + gdir.getStatus().code);
//				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);
//				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);
//				alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	else
		alert("&#x44;&#x65;&#122;&#x65;&#x20;&#109;&#x6F;&#x64;&#117;&#108;&#x65;&#32;&#105;&#115;&#32;&#x6F;&#110;&#x74;&#119;&#105;&#x6B;&#107;&#101;&#x6C;&#x64;&#32;&#100;&#x6F;&#x6F;&#x72;&#x20;&#x43;&#101;&#x65;&#x73;&#32;&#100;&#x65;&#32;&#71;&#x65;&#x75;&#115;&#x20;&#118;&#x61;&#x6E;&#x20;&#67;&#45;&#73;&#x6E;&#115;&#105;&#x74;&#101;&#32;&#x77;&#x65;&#98;&#100;&#101;&#115;&#x69;&#x67;&#110;&#46;&#32;&#87;&#x69;&#x6C;&#x74;&#x20;&#x75;&#x20;&#x6F;&#x6F;&#x6B;&#x20;&#x67;&#x65;&#98;&#x72;&#x75;&#105;&#107;&#x20;&#109;&#97;&#x6B;&#101;&#x6E;&#32;&#x76;&#x61;&#x6E;&#32;&#100;&#101;&#x7A;&#101;&#x20;&#x6F;&#x66;&#32;&#97;&#x6E;&#100;&#101;&#x72;&#x65;&#x20;&#104;&#97;&#110;&#x64;&#105;&#x67;&#x65;&#32;&#x73;&#x74;&#117;&#107;&#x6B;&#101;&#110;&#32;&#118;&#97;&#x6E;&#x20;&#100;&#x65;&#122;&#x65;&#32;&#119;&#x65;&#x62;&#x73;&#105;&#x74;&#x65;&#x20;&#110;&#101;&#x65;&#109;&#32;&#100;&#x61;&#x6E;&#x20;&#x63;&#111;&#x6E;&#116;&#x61;&#99;&#x74;&#x20;&#x6D;&#x65;&#x74;&#x20;&#x6F;&#x6E;&#x73;&#32;&#x6F;&#x70;&#46;&#32;&#x57;&#x65;&#98;&#115;&#x69;&#x74;&#x65;&#58;&#32;&#x77;&#119;&#x77;&#46;&#x63;&#x2D;&#x69;&#x6E;&#115;&#x69;&#x74;&#101;&#x2E;&#110;&#x6C;&#32;&#x6F;&#x66;&#x20;&#x65;&#45;&#109;&#97;&#x69;&#108;&#x3A;&#x20;&#x69;&#110;&#x66;&#x6F;&#x40;&#x63;&#45;&#105;&#x6E;&#x73;&#x69;&#116;&#x65;&#x2E;&#x6E;&#108;");
}

function onGDirectionsLoad(){ 
// Use this function to access information about the latest load()
// results.

// e.g.
// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
// and yada yada yada...
}
//]]>


