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

function MakeMap() {
  if (GBrowserIsCompatible()) { 
  	document.getElementById("fromAddress").selectedIndex="0";
  	     
    map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());

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

    setDirections();
  }
}

function setDirections() {
	var fromObject = document.getElementById("fromAddress");
	var fromAddress = fromObject[fromObject.selectedIndex].value;
  var toAddress = "Sint Maartenslaan 1, Maastricht, NL";
	var locale = "EN";
	
	gdir.load("from: " + fromAddress + " to: " + toAddress, {"locale": locale});
}

function handleErrors(){
 if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
   alert("Het adres bestaat niet. Probeer bijvoorbeeld ', NL' achter de plaatsnaam te zetten." + gdir.getStatus().code);
 } else {
 	 alert("De route kan niet getoond worden, probeer het nog eens.");
 }
}

function onGDirectionsLoad(){
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().image = "/pics/logo.png"; 
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().iconSize = new GSize(140, 45); 
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().iconAnchor = new GPoint(50, 45);
	
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().printImage = "/pics/logo.png"; 
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().mozPrintImage = "/pics/logo.png"; 
}

if (typeof window.onload == "function") {
	var FnOnload = window.onload;
	window.onload = function() {
	  FnOnload();
	  MakeMap();
	}
} else {
	window.onload = function() {
		MakeMap();
	}
}



