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();
alert(localSearch[addID]);
	
	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() {
	clearInterval(googmap);	
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("map_canvas"));
		
		
//		directionsPanel = document.getElementById("directions");
//		map.setCenter(new GLatLng(lngitude, latitude), 3);
//		directions = new GDirections(map, directionsPanel);
//		directions.load("from: 500 Memorial Drive, Cambridge, MA to: 4 Yawkey Way, Boston, MA 02215 (Fenway Park)");
		

		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	
		var iwebicon = new GIcon();
		iwebicon.image = "/website.data/ntcg.directions.picture.png";
  		iwebicon.shadow = "/website.data/ntcg.directions.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(lngitude, latitude); //52.23734, -0.88882);
		var marker = new GMarker(point, iwebicon);
		//
		// Add the icon as a marker		
		map.setCenter(new GLatLng(lngitude, latitude), 15);
		map.addOverlay(marker);
		map.addControl(new GLargeMapControl());
		//
		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) {
//alert(toAddress1);
//alert(ecms('from').value);
	toAddress = toAddress1;
	locale = locale1;
	usePointFromPostcode(fromAddress, 0, getDirections);
}

function getDirections(myPoint) {
alert('steve');
	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("I'm sorry to inform you that the postcode provided could not be located. This may be due to a number of reasons.");
}

function onGDirectionsLoad(){ 

}

var googmap = setInterval("loadMe()",1000)

//window.onload = loadMe;
window.onunload = GUnload;
