// JavaScript Document

var custPostcode;
var custLat;
var custLong;

function GetMap() {
	
	custPostcode = document.getElementById("uclFindYourLocalDepot_txtPostcode").value;
	
	if (custPostcode == '' || custPostcode == 'Enter first part of your postcode') {
		alert('Please enter the first part of your postcode.');
		return false;
	}
	
	var mapOptions = {
	credentials: "AmX4pRCsfzRQ6dzASmnIkIGPaHgpCY_-1YL5PAwLTaC_Yb-jxNb80zsWEx5GPuVH",
	//center: new Microsoft.Maps.Location(54.0710002184, -2.7839995176),
	mapTypeId: Microsoft.Maps.MapTypeId.automatic,
	zoom:2
	}

	map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), mapOptions);
	ClickGeocode();
}

function FindNearest() {
	
	var depotLat;
	var depotLong;
	
	var d
	
	var ClosestMarker;
	var ClosestDistance = 0;
	
	for(z=0; z<arrDepots.length; z++) {
		
		depotLat = arrDepots[z].split("|")[2];
		depotLong = arrDepots[z].split("|")[3];
		
		var d = distHaversine((custLat + '').parseDeg(), (custLong + '').parseDeg(), (depotLat + '').parseDeg(), (depotLong + '').parseDeg());		
	
		arrDepots[z] = arrDepots[z] + '|' + d;
		
		if ((ClosestDistance == 0) || (d < ClosestDistance)) {
			ClosestDistance = d;
			ClosestMarker = z;
		}
	}
	
//	alert('Closest depot is ' + arrDepots[ClosestMarker].split("|")[0] + ' at ' + ClosestDistance.toPrecision(4) + ' km');
	window.location.href = arrDepots[ClosestMarker].split("|")[4] + '?PC=' + custPostcode;
}

function ClickGeocode(credentials)
{
	map.getCredentials(MakeGeocodeRequest);
}

function MakeGeocodeRequest(credentials)
{
	var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + custPostcode  + ', UK' + "?output=json&jsonp=GeocodeCallback&key=" + credentials;	
	CallRestService(geocodeRequest);
}

function CallRestService(request) 
{
	var script = document.createElement("script");
	script.setAttribute("type", "text/javascript");
	script.setAttribute("src", request);
	document.body.appendChild(script);
}

function GeocodeCallback(result) 
{	
	if (result && result.resourceSets && result.resourceSets.length > 0 && result.resourceSets[0].resources && result.resourceSets[0].resources.length > 0) 
	{	
		// Set the map view using the returned bounding box
		var bbox = result.resourceSets[0].resources[0].bbox;
		var name = result.resourceSets[0].resources[0].name;
		var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
		map.setView({ bounds: viewBoundaries});
		
		// Add a pushpin at the found location
		var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);

		var pushpin = new Microsoft.Maps.Pushpin(location, { text: '' + (0) + '', draggable: true });		
		
		var loc = pushpin.getLocation();
		
		custLat = loc.latitude.toFixed(10);
		custLong = loc.longitude.toFixed(10);
		
		// The co-ordinates below come back when postcode is not recognised. Poss bug.
		if (custLat == 54.0709991455 && custLong == -2.7839999199) {
			alert('Your postcode is not recognised. Please check and try again.');
		}
		else {
			FindNearest();	
		}		
	}
	else
	{
		alert('Your postcode is not recognised. Please check and try again.');
	}
}
