
function createMarker(point,html) {
	// A function to create the marker and set up the event window
    // Dont try to unroll this function. It has to be here for the function closure
    // Each instance of the function preserves the contends of a different instance
    // of the "marker" and "html" variables which will be needed later when the event triggers.    
		
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
	marker.openInfoWindowHtml(html);
	});
	return marker;
}
		
function load(mapID, locatie, streetcitycountry) 
{
	//alert("load!");
	
	if (GBrowserIsCompatible()) 
	{
		// initiate map
		var map = new GMap2(document.getElementById(mapID));
		
		// Display the map, with some controls and set the initial location 
      	map.addControl(new GLargeMapControl());
      	map.addControl(new GMapTypeControl());
      	map.setCenter(new GLatLng(20,0),2);
	  
	    // ====== Create a Client Geocoder ======
		var geo = new GClientGeocoder(new GGeocodeCache()); 
		
		// ====== Array for decoding the failure codes ======
		var reasons=[];
		reasons[G_GEO_SUCCESS]            = "Success";
		reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
		reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";

	  	var search = streetcitycountry;
      	// ====== Perform the Geocoding ======        
      geo.getLatLng(search, function (point)
        { 
          // ===== If that was successful, plot the point and centre the map ======
          if (point) {
		  
		  	var i = 0;
		  	markerHTML = locatie;
		  
            var marker = createMarker(point,markerHTML)
            map.addOverlay(marker);
            map.setCenter(point,15);
			
			
			
          }
          // ====== Decode the error status ======
          else {
            // ==Look to see if the query was cached ==
            var result=geo.getCache().get(search);
            if (result) {
              var reason="Code "+result.Status.code;
              if (reasons[result.Status.code]) {
                reason = reasons[result.Status.code]
              }
            } else {
              var reason = "";
            } 
            alert('Could not find "'+search+ '" ' + reason);
          }
        }
      );
	  
      	
	}
}


// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
var htmls = [];
var i = 0;
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];

// A function to create the marker and set up the event window
function createMarker(point,html) {
  var marker = new GMarker(point);

  // The info window version with the "to here" form open
  to_htmls[i] = html + '<br />Vertrekpunt:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
     '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br />' +
     '<INPUT value="Toon routebeschrijving" TYPE="SUBMIT">' +
     '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
            // "(" + name + ")" + 
     '"/>';
 
  // The inactive version of the direction info
  html = html + '<br>Routebeschrijving: <a href="javascript:tohere('+i+')">Naar hier</a>';

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  // save the info we need to use later for the side_bar
  gmarkers[i] = marker;
  htmls[i] = html;


  i++;
  return marker;
}


// This function picks up the click and opens the corresponding info window
function myclick(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}

// functions that open the directions forms
function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}