var map;
var gdir;
var geocoder = null;
var addressMarker;
var toAddress
var locale

var localSearch = new Array();
var callBack = new Array();

function changeValue (x, s, d) {
	if (x.value == d) {
		x.value = s;
	}
}

function printpage() { 
	window.print(); 
}

function usePointFromPostcode(postcode, addID, callbackFunction) {
	localSearch[addID] = new GlocalSearch();
	callBack[addID] = function() {
		if (localSearch[addID].results[0]) {		
			var resultLat = localSearch[addID].results[0].lat;
			var resultLng = localSearch[addID].results[0].lng;
			var point = new GLatLng(resultLat,resultLng);
			callbackFunction(point);
		} else{
			// Postcode not found!
		}
	}
	localSearch[addID].setSearchCompleteCallback(null, callBack[addID]);
	localSearch[addID].execute(postcode)// + ", UK");
}

function loadMe() {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("map"));

		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);

		// setDirections("Birmingham", "Stafford", "en_UK");
		
		var iwebicon = new GIcon();
		iwebicon.image = "/google-maps/pngs/kings-park-logo.png";
  		iwebicon.shadow = "/google-maps/pngs/shadow.png";
		iwebicon.iconSize = new GSize(78, 51);
  		iwebicon.shadowSize = new GSize(78, 51);
		iwebicon.iconAnchor = new GPoint(38, 51);
 		iwebicon.infoWindowAnchor = new GPoint(38, 51);
		//
		var point = new GLatLng(52.274877,-0.878037);
		var marker = new GMarker(point, iwebicon);
		//
		// Add the icon as a marker		
		map.setCenter(new GLatLng(52.274877,-0.878037), 9);
		map.addOverlay(marker);
		map.addControl(new GLargeMapControl());

		//
		var titleInfo = "<strong>Kings Park Conference & Sports Centre</strong><br />"
		var addinfo = titleInfo
		addinfo += "Kings Park Road<br />"
		addinfo += "Moulton Park Industrial Estate<br />"
		addinfo += "Northampton<br />"
		addinfo += "NN3 6LL<br /><br />";
		var moreInfo = "Tel : 01604 493111<br />"
		moreInfo += "Fax : 01604 493559<br />"
		//
		var infoHTML = addinfo+moreInfo;
		GEvent.addListener(marker, "click", function() {
			myMarker = marker;
			myMarker.openInfoWindowHtml(infoHTML);
			// Centre the map on iWeb
			map.setCenter(point);
  		});
	
	}
}
   
function setDirections(fromAddress, toAddress1, locale1) {
	toAddress = toAddress1;
	locale = locale1;
	usePointFromPostcode(fromAddress, 0, getDirections);
}

function getDirections(myPoint) {
	myPoint = String(myPoint);
	myPoint = myPoint.substr(1, myPoint.length-2);
	//
	if (document.layers) {
		document.all.directionsCont.display = "block";
	} else 	if (document.all) {
		document.all.directionsCont.style.display = "block";
	} else 	if (!document.all && document.getElementById) {
		document.getElementById('directionsCont').style.display = "block";
	}
	//
	gdir.load("from: " + myPoint + " to: " + toAddress,
                { "locale": locale });
}

function handleErrors(){
	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. Please try another address.");
}

function onGDirectionsLoad(){ 

}

window.onload = loadMe;
window.onunload = GUnload;
