// Maps functions

var map, gdir, fromAddress, toAddress;


function loadMap() {
	if (GBrowserIsCompatible()) {
	  
		// Setup our maps and directions
		map = new GMap2(document.getElementById("map"));
		gdir  = new GDirections(map, document.getElementById("dNavDirections"));
		map.setCenter(new GLatLng(53.862246,-1.378784), 3);
		
		GEvent.addListener(gdir, "error", handleErrors);

		
		var bounds = new GLatLngBounds();
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		GDownloadUrl("locationData.xml", function(data) {
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");
				for (var i = 0; i < markers.length; i++) {
				var name = markers[i].getAttribute("name");
				var address = markers[i].getAttribute("address");
				var url = markers[i].getAttribute("url");
				var type = markers[i].getAttribute("type");
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
										parseFloat(markers[i].getAttribute("lng")));
				var number = markers[i].getAttribute("number");
				var marker = createMarker(point, name, address, type, number, url);
				map.addOverlay(marker);
				bounds.extend(point);
			}
			map.setZoom(map.getBoundsZoomLevel(bounds));
			map.setCenter(bounds.getCenter());
		});
	}
}


function unloadMap() {
	GUnload();
}

function createMarker(point, name, address, type, number, url) {
	var iconCol = new GIcon(G_DEFAULT_ICON); 
	iconCol.image = 'images/mapmarkers/' + type + number + '.gif';
	var marker = new GMarker(point, iconCol);
	var html = "<b>" + name + "</b> <br/>" + address + '<br/><a href="' + url + '">' + url + '</a>';
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	GEvent.addListener(marker, 'click', function() {
		document.getElementById("sSelProp").innerHTML = address;
	});
  	return marker;
}

function overlayDirections()  
{  
	if ((document.getElementById("sSelProp").innerHTML.length == 0) && (document.getElementById("postcode").value.length == 0)) 
	{
		alert("You must click a property and enter your postcode on the map before we can give you directions.");
	} else {
		fromAddress =  document.getElementById("postcode").value;
		toAddress = document.getElementById("sSelProp").innerHTML;
		gdir.load("from: " + fromAddress + " to: " + toAddress); 
		document.getElementById("pFlash").innerHTML = "Displaying Directions " + "from: " + fromAddress + " to: " + toAddress;
	}
}  

function handleErrors()
{  
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)  
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the postcode is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);  
	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("An unknown error occurred.");  
}