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


function loadMap(){

  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
   
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(51.36322, 6.134877), 8);
   
    var point = new GLatLng(50.972495, 5.825071); //Geleen
    var point_venray = new GLatLng(51.541456, 5.994466); //Venray
    var point_weert = new GLatLng(51.273173, 5.732034); //Weert
    				    
    var icon = new GIcon();
    icon.image = "/pics/logo.png";
    icon.iconSize = new GSize(125,50);
    icon.iconAnchor = new GPoint(45,48);				    
    
    
    			    
    var marker_venray = new GMarker(point_venray,icon);
    map.addOverlay(marker_venray); 
    
    var marker_weert = new GMarker(point_weert,icon);
    map.addOverlay(marker_weert); 
    
    var marker = new GMarker(point,icon);
    map.addOverlay(marker); 
    }
}

function MakeMap(fromAddress, toAddress) {
	
	document.getElementById("directions").innerHTML = "";
	
  if (GBrowserIsCompatible()) { 
  	     
    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);

		var fromAddress = document.getElementById("fromAddress").value;
  	var toAddress = document.getElementById("toAddress").value;

    setDirections(fromAddress,toAddress);
  }
}

function setDirections(fromAddress, toAddress) {
  gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": "NL" });
}

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(125,50); 
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().iconAnchor = new GPoint(45,48);
	
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().printImage = "/pics/logo.png"; 
	gdir.getMarker(gdir.getNumGeocodes()-1).getIcon().mozPrintImage = "/pics/logo.png"; 
}
