var map;
var gdir;
var geocoder = null;
var addressMarker;
var address;
var data = new Array();
var localSearch = new GlocalSearch();

function load() {
	 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);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom(true);
		map.setCenter(new GLatLng(54, -4.1), 5);
		map.setMapType(G_NORMAL_MAP);
		usePointToPostcode(postcode);
	}
}

function dummyError() {
	return false;
}

function usePointToPostcode(postcode, callbackFunction) {
	localSearch.setSearchCompleteCallback(null, function() {
		if (localSearch.results[0]) {
			var resultLat = localSearch.results[0].lat;
			var resultLng = localSearch.results[0].lng;
			var point = new GLatLng(resultLat,resultLng);
			data[0] = localSearch.results[0].lat;
			data[1] = localSearch.results[0].lng;
			eventCords(localSearch.results[0].lat, localSearch.results[0].lng);
		} else {
			alert("Postcode not found!");
		}
	});

	localSearch.execute(postcode + ", UK");
}

function usePointFromPostcode(postcode, callbackFunction) {
	localSearch.setSearchCompleteCallback(null, function() {
		if (localSearch.results[0]) {
			var resultLat = localSearch.results[0].lat;
			var resultLng = localSearch.results[0].lng;
			var point = new GLatLng(resultLat,resultLng);
			loadDirections(localSearch.results[0].lat, localSearch.results[0].lng);
		} else {
			alert("Postcode not found!");
		}
	});

	localSearch.execute(postcode + ", UK");
}

function eventCords(gLat, gLng) {
	map.setCenter(new GLatLng(gLat,gLng), 15);
	var marker = new GMarker(new GLatLng(gLat,gLng));
	map.addOverlay(marker);
}

function loadDirections (geoData1,geoData2) {
	gdir.load("from: " + geoData1 + "," + geoData2 + " to: " + data[0]+','+data[1], { "locale": "en_GB" });
}

function setDirections(fromAddress) {
	usePointFromPostcode(fromAddress+', UK');
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	alert("Please check you have entered a valid UK postcode in the form of 'AB1 12C'");
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	alert("A geocoding or directions request could not be successfully processed yet, the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	else alert("Please ensure you have filled in your postcode.");
}

function onGDirectionsLoad(){}