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{
			alert("No such postcode location exsists");
		}
	}
	localSearch[addID].setSearchCompleteCallback(null, callBack[addID]);
	localSearch[addID].execute(postcode)// + ", UK");
}

function loadMe() {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("map"));

		
		document.getElementById('objDirTools').style.visibility='hidden';
		$("#directionsCont").slideUp("slow");	
		$("#directions").html('');	
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	
		var icon = new GIcon();
		icon.image = "images/googlemaps/logo.png";
  		icon.shadow = "images/googlemaps/shadow.png";
		icon.iconSize = new GSize(78, 51);
  		icon.shadowSize = new GSize(78, 51);
		icon.iconAnchor = new GPoint(38, 51);
 		icon.infoWindowAnchor = new GPoint(38, 51);
		
		
		var point = new GLatLng(52.688744,-2.023281);
		var marker = new GMarker(point, icon);
		//
		// Add the icon as a marker		
		map.setCenter(new GLatLng(52.688744,-2.023281), 8);
		map.addOverlay(marker);
		
		//
		var titleInfo = "<strong>Premier Software Solutions</strong><br />"
		var addinfo = titleInfo
		addinfo += "Premier House<br />"
		addinfo += "3 Hollies Court<br />"
		addinfo += "Cannock<br />"
		addinfo += "Staffordshire, UK<br />"
		addinfo += "WS11 1DB<br /><br />";
		var moreInfo = "Tel : +44 (0) 1543 466 580<br />"
		moreInfo += "Fax : +44 (0) 1543 466 579<br />"
		moreInfo += "Email : <a href='mailto:info@premiersoftware.co.uk'>info@premiersoftware.co.uk</a>";
		//
		
		var infoHTML = addinfo+moreInfo;
		GEvent.addListener(marker, "click", function() {
			myMarker = marker;
			myMarker.openInfoWindowHtml(infoHTML);
			//centre us on the map
			map.setCenter(point);
  		});
	}
}
   
function setDirections(fromAddress, locale1) {
	toAddress = "52.688744,-2.023281";
	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";
	}*/
	$("#directionsCont").slideDown("slow");	
	//
	gdir.load("from: " + myPoint + " to: " + toAddress,
                { "locale": locale });
		// Show User Options
	document.getElementById('objDirTools').style.visibility='visible';
	//
}

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;