var map;
var gdir;
var geocoder = null;
var addressMarker;
var pointMarker;

function load() {
  
  
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);

    map.enableScrollWheelZoom();
    map.setCenter(new GLatLng(52.39988558107205, 4.876556396484375), 14	);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
			map.removeMapType(G_HYBRID_MAP);
			map.removeMapType(G_SATELLITE_MAP);

    var pointIcon = new GIcon();
    pointIcon.image = "http://www.hotitem.nl/favicon32.ico";
    pointIcon.iconAnchor = new GPoint(32, 16);
    pointIcon.infoWindowAnchor = new GPoint(34, 2);
    pointIcon.iconSize = new GSize(32, 32);

    markerOptions = { icon:pointIcon, title:"Hot ITem"};
    var latlng = new GLatLng(52.39988558107205, 4.876556396484375);
    //var pointMarker = new GMarker(latlng, markerOptions);
	pointMarker = new GMarker(latlng, markerOptions);
    pointMarker.bindInfoWindowHtml("<h4>Hot ITem</h4><img src='http://www.hotitem.nl/images/pand.jpg' width='220px' height='165px'><br><br>Danzigerkade 19, 1013 AP Amsterdam");
    map.addOverlay(pointMarker);
    pointMarker.openInfoWindowHtml("<h4>Hot ITem</h4><img src='http://www.hotitem.nl/images/pand.jpg' width='220px' height='165px'><br><br>Danzigerkade 19, 1013 AP Amsterdam");

  }
}


function setDirections(fromAddress, toAddress, locale) {
	if (pointMarker != null) {
		pointMarker.closeInfoWindow();
	}

  gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": locale });
}


function handleErrors(){
  if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("Geen route kunnen bepalen omdat het vertrekpunt niet gevonden kon worden.\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_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\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_UNKNOWN_DIRECTIONS)
     alert("Er kan geen route worden bepaald, vul een ander vertrekpunt in.\n Foutcode: " + 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("Onbekende fout. " + gdir.getStatus().code);
   
}

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...
}
	
function unloadMap() {
	GUnload();
}
	
if (window.addEventListener) {
	window.addEventListener("load", load, false);
	window.addEventListener("unload", unloadMap, false);
}
else if (window.attachEvent) {
	window.attachEvent("onload", load);
	window.attachEvent("onunload", unloadMap);
}

