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

// Locale
var locale = "en_UK";

// The geolocations are used to centre the marker on first page load
// We will set this in HTML pages

// Westover Wandsworth geoloc
//var WGEOwandsworth = new GLatLng(51.538052,-0.085794);

// Westover Brompton Cross geoloc
//var WGEObromptoncross = new GLatLng(51.538052,-0.085794);

// Westover Notting Hill geoloc
//var WGEOnottinghill = new GLatLng(51.538052,-0.085794);

// The postcodes are used to get directions
// We will set this in HTML pages

// Westover Wandsworth postcode
//var WPCwandsworth = "N1 3LY";

// Westover Brompton Cross postcode
//var WPCbromptoncross = "N1 3LY";

// Westover Notting Hill postcode
//var WPCnottinghill = "N1 3LY";

function initialize(mapArray)
{

  //alert("maparray= "+mapArray);
  
  if (GBrowserIsCompatible())
  {
	
	// Create our "tiny" marker icon
	var westoverIcon = new GIcon();
	westoverIcon.image = "http://www.thewestover.com/support/img/googlemaps/westover-icon.png";
	westoverIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	westoverIcon.iconSize = new GSize(20, 27);
	westoverIcon.shadowSize = new GSize(37, 27);
	westoverIcon.iconAnchor = new GPoint(10, 27);
	westoverIcon.infoWindowAnchor = new GPoint(10, 1);

	// Set up our GMarkerOptions object
	markerOptions = { icon:westoverIcon };
	
	var a=0;
	
	for (a=0;a<mapArray.length;a++)
	{
	  
	  // Define the map div
	  map = new GMap2(document.getElementById("mapcanvas" + a));
	  
	  // Center the map
	  map.setCenter(mapArray[a][0], 15);
	  
	  // Add a control
	  map.addControl(new GSmallMapControl());
	  
	  // Load the directions function
	  gdir = new GDirections(map, document.getElementById("directions"));
	  
	  // Error handler
	  GEvent.addListener(gdir, "error", handleErrors);
	  
	  // Add custom marker
	  map.addOverlay(marker = new GMarker(mapArray[a][0], markerOptions));
	  
	  //alert("map= "+map);
	  
	}
	 
  }

}

// We will set this in HTML pages
function setDirections(fromAddress,toAddress,themap)
{
	
	//alert(fromAddress +"\n"+ toAddress +"\n"+ themap);
	
	// Define the map div from the name we pass
	map = new GMap2(document.getElementById(themap));
	
	// New directions function
	gdir = new GDirections(map, null);
	
	// Directions refers to the directions panel - not used here
	//gdir = new GDirections(map, document.getElementById("directions"));
	
	//GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
	
	// fromAddress and toAddress are passed to the directions function
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
	
	// Remove the centered marker as we now have direction markers
	map.removeOverlay(marker);
	
	// Error handler
	GEvent.addListener(gdir, "error", handleErrors);
	
	// Add a control
	map.addControl(new GSmallMapControl());
  
}

function handleErrors(gdir)
{

  if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)

  alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\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_BAD_REQUEST)

  alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

  else alert("An unknown error occurred.");

}

function onGDirectionsLoad()
{
  
  // Do something once the map has loaded
  //alert(fromAddress + ", " + toAddress);

}

// Check if enter has been pressed
function onenter(fromAddress,toAddress,themap)
{
  
  if (window.event && window.event.keyCode==13)
  {
	
	  alert("from "+fromAddress+"\nto "+toAddress+"\nmap "+themap);
	  
	  setDirections(fromAddress,toAddress,themap);
	  
  }
  else
  {
	
	return true;
  }
  
}