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/pioneer-centre-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.389848, -2.463276);
		var marker = new GMarker(point, iwebicon);
		//
		// Add the icon as a marker		
		map.setCenter(new GLatLng(52.389848, -2.463276), 9);
		map.addOverlay(marker);
		map.addControl(new GLargeMapControl());

		//
		var titleInfo = "<strong>Pioneer Centre</strong><br />"
		var addinfo = titleInfo
		addinfo += "Cleobury Mortimer<br />"
		addinfo += "Shropshire<br />"
		addinfo += "DY14 8JG<br /><br />";
		var moreInfo = "Tel : 01299 271217<br />"
		moreInfo += "Fax : 01299 270948<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;
