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);

	
		var customIcon = new GIcon();
		customIcon.image = "icon/marker.png";
  		customIcon.shadow = "icon/markershadow.png";
      	customIcon.transparent = "icon/iconTrans.png";
      	customIcon.printImage = "icon/markerie.gif";
      	customIcon.mozPrintImage = "icon/markerie.gif";
    	customIcon.iconSize = new GSize(200, 104);
      	customIcon.shadowSize = new GSize(266, 104);
      	customIcon.iconAnchor = new GPoint(1, 104);
      	customIcon.infoWindowAnchor = new GPoint(1, 104);
      	customIcon.infoShadowAnchor = new GPoint(1, 1);      	
		
		
		//
		var point = new GLatLng(52.8845,-1.8575);
		var marker = new GMarker(point, customIcon);
		//
		// Add the icon as a marker		
		//http://www.satsig.net/maps/lat-long-finder.htm
		map.setCenter(new GLatLng(52.8884,-1.8503), 13);
		map.addOverlay(marker);
     	map.addControl(new GLargeMapControl());
      	map.addControl(new GMapTypeControl());
		
		//
		var titleInfo = "<strong>High View Cottage</strong><br />"
		var addinfo = titleInfo
		addinfo += "<strong>4 Star Bed and Breakfast</strong><br />"
		addinfo += "Toothill Road<br />"
		addinfo += "Uttoxeter<br />"
		addinfo += "Stafford<br />"
		addinfo += "ST14 8JU<br /><br />";
		var moreInfo = "Tel : 01889 568 183<br />"
		moreInfo += "Mob : 07980 041670<br />"
		moreInfo += "Email : <a href='mailto:info@highviewcottage.com'>info@highviewcottage.com</a>";
		//
		var infoHTML = addinfo+moreInfo;
		/*GEvent.addListener(marker, "click", function() {
			myMarker = marker;
			myMarker.openInfoWindowHtml(infoHTML);
			// Centre the map on HVC
			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;
