﻿// Copyright (c) Commonwealth of Australia 2001-2009
// This is the source of the locator script; it should be minimised using the YUI compressor before distribution

var iterations;
var geoThreshold;
var currentPosition;

var $ = function(id) { return document.getElementById(id); };
var getPosition;

function usePosition(position) {
	if (position.coords.accuracy > 1000) {
		if (confirm("Unable to pinpoint your location precisely (within 1km), continue?")) {
			$("btnLocate").value = "Locate";
			window.location = "?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude;
		}
		else {
			var locateButton = $("btnLocate");
			locateButton.value = "Locate";
			locateButton.disabled = false;
		}
	}
	else {
		$("btnLocate").value = "Locate";
		window.location = "?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude;
	}
}

function fail() {
	var locateButton = $("btnLocate");
	locateButton.value = "Locate";
	locateButton.disabled = false;
	$("error").innerHTML = "<ol><li class=\"error\">An error occurred while trying to automatically find your current location.</li></ol>";
}

function successCallback(position) {
	iterations++;

	if (currentPosition === null || position.coords.accuracy < currentPosition.coords.accuracy) {
		currentPosition = position;
	}

	if (currentPosition.coords.accuracy <= geoThreshold || iterations >= 5) {
		usePosition(currentPosition);
	}
	else if (iterations == 1) {
		setTimeout(getPosition, 3000);
	}
	else {
		setTimeout(getPosition, 2000);
	}
}

function errorCallback(error) {
	if (error.code == error.TIMEOUT) {
		if (currentPosition !== null) {
			usePosition(currentPosition);
		}
		else {
			navigator.geolocation.getCurrentPosition(successCallback, errorCallback, { enableHighAccuracy: true, timeout: 30000, maximumAge: 5000 });
		}
	}
	else {
		if (currentPosition !== null) {
			usePosition(currentPosition);
		}
		else {
			fail();
		}
	}
}

function getPosition() {
	navigator.geolocation.getCurrentPosition(successCallback, errorCallback, { enableHighAccuracy: true, timeout: 30000, maximumAge: 5000 });
}

function enableGeolocation() {
	if (document.getElementById) {

		var locate = $("locate");

		if (locate && navigator && navigator.geolocation) {
			locate.innerHTML = "<div id='geolocation'><label for='btnLocate'>Search using your current location</label><input type='button' class='button' id='btnLocate' value='Locate' /></div>";

			var locateButton = $("btnLocate");

			if (locateButton) {
				locateButton.onclick = function() {
					locateButton.value = "Locating...";
					locateButton.disabled = true;

					currentPosition = null;
					geoThreshold = 200;
					iterations = 0;

					$("locateStatus").innerHTML = "";
					$("error").innerHTML = "";

					navigator.geolocation.getCurrentPosition(successCallback, errorCallback, { enableHighAccuracy: true, timeout: 0, maximumAge: 5000 });
				};
			}
		}
	}
}
