var geocoder;
var map;
var zoom = 14;
var addresses = new Array();

// On page load, call this function
function loadgmaps()
{
  // Create new map object
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GMapTypeControl());
  map.addControl(new GSmallMapControl());

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


  // Create new geocoding object
  geocoder = new GClientGeocoder();

  // Retrieve location information, pass it to addToMap()
  geocoder.getLocations(address, addToMap);
}

// For small map objects with multiple addresses
function loadgmaps2()
{
  // Create new map object
  map = new GMap2(document.getElementById("map"));

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


  // Create new geocoding object
  geocoder = new GClientGeocoder();

  // Retrieve location information, pass it to addToMap()
  geocoder.getLocations(address, addToMap);

  if(addresses.length > 0)
  {
    for(i=0;i<addresses.length;i++)
    {
      showAddress(addresses[i]);
    }
  }
}

// This function adds the point to the map and centers the map
function addToMap(response)
{
  // Retrieve the object
  place = response.Placemark[0];

  // Retrieve the latitude and longitude
  point = new GLatLng(place.Point.coordinates[1],
					  place.Point.coordinates[0]);

  // Center the map on this point
  map.setCenter(point, zoom);

  // Create a marker
  if(addresses.length == 0) {
      marker = new GMarker(point);

      // Add the marker to map
      map.addOverlay(marker);

      map.addControl(new GSmallMapControl());
  }


  //Add address information to marker
  //marker.openInfoWindowHtml(place.address);
}


// This function adds multiple html infowindows to the map
function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
		  address[1],
		  function(point) {
			if (point) {
			  var marker = new GMarker(point);
			  GEvent.addListener(marker,"click", function() {
				var myHtml = '<a href="'+address[2]+'">'+address[0]+'</a> <br />'+address[3]+' <br />'+address[4];
				map.openInfoWindowHtml(point, myHtml);
			  });
			  map.addOverlay(marker);
			}
		  }
		);
	}
}


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

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("Het opgegeven adres is niet bekend bij de routeplanner. Probeer het adres anders te omschrijven.");
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("Door een onbekende oorzaak kan het verzoek niet worden verwerkt.");
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("Door een onbekende oorzaak kan het verzoek niet worden verwerkt.");

//   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("Door een onbekende oorzaak kan het verzoek niet worden verwerkt.");

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("Door een onbekende oorzaak kan het verzoek niet worden verwerkt.");
	
   else alert("Vul een correct adres in.");
   
}

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...
}

