// <![CDATA[

var map;
var gdir;
var geocoder = null;
var address;
var business_address;
var myLng;
var myLat;

// Nine Club Coordinates are:
myLng = 51.747813;
myLat = -2.303224;
business_address = 'Nine Club<br />' +
					'Pegasus House<br />' +
					'Stonehouse Park<br />' +
					'Stonehouse<br />' +
					'Gloucestershire<br />'+
					'GL10 3UT';
	function initialize() {
	  if (GBrowserIsCompatible()) {
		  map = new GMap2(document.getElementById("map_canvas"));
		  gdir = new GDirections(map, document.getElementById("directions"));
		  
		  GEvent.addListener(gdir, "error", handleErrors);
	
		  map.setCenter(new GLatLng(myLng,myLat), 15);
		  map.addControl(new GLargeMapControl);
		  map.addControl(new GMapTypeControl());
		 
		  var trafficOptions = {incidents:true};
		  trafficInfo = new GTrafficOverlay(trafficOptions);
		  map.addOverlay(trafficInfo);
		 
		  point = new GLatLng(myLng, myLat);
		  marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml(business_address);
		
		  GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(business_address);
		  });
		  
		  return marker;
	  }
	}

    function setDirections(fromAddress, toAddress, locale) {
	  if ( fromAddress.length > 0 ) {
	  	  document.getElementById('directions').innerHTML = '';
	      gdir.load("from: " + fromAddress + " to:51.746924, -2.301832", { "locale": locale });
		  document.getElementById('directions').style.display = 'block';
		  document.getElementById('directions_heading').style.display = 'block';
	  } else {
	  	  alert('Please enter an address and try again!');
	  }
    }


    function handleErrors(){
	   var bIsOK = true;
	   var response = '';
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
	     response = "No corresponding geographic location could be found for the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.<br /><br />If you entered just a post code, make sure you add a space and then the letters 'uk' as in the example above.<br /><br />Error code: " + gdir.getStatus().code;
 		 document.getElementById('directions').innerHTML = response;
		 bIsOK = false;
	   } else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
	     response = "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;
		 document.getElementById('directions').innerHTML = response;
		 bIsOK = false;
	   
	   } else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
	     response = "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;
		 document.getElementById('directions').innerHTML = response;
		 bIsOK = false;

	//   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) {
	     response = "The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code;
		 document.getElementById('directions').innerHTML = response;
		 bIsOK = false;

	   } else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
	     response = "A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code;
		 document.getElementById('directions').innerHTML = response;
		 bIsOK = false;
	    
	   } else {
	   	response = "An unknown error occurred.";
		document.getElementById('directions').innerHTML = response;
		bIsOK = false;
	   }
	   
	}
// ]]>