var debugMode = false;

function initApp() {
	var divElm = document.getElementById("test");
	var useW3C   = typeof(navigator.geolocation) != 'undefined';
	var useGears = window.google && google.gears;
	var geolocationSupported = useW3C || useGears;

	var debugDiv  = document.getElementById("debug");
	var debugDist = null;

	var currPos;
	var itCount = 0;
	
	// Detect browsing with desktop
	if (typeof(window.screen) != "undefined" && typeof(window.screen.width) != "undefined" && window.screen.width > 600) {
		document.body.className = "desktop";
	}
	
	var updateDisplay = function() {
		try {
			var MIN_ACCURACY = 50; // minimum allowable accuracy (actually a max error in meters)
			var MIN_DISTANCE = 100; // how far away from the program until we display the Star of Bethlehem note
			itCount++;
			if (currPos && currPos.coords && currPos.coords.accuracy < MIN_ACCURACY && currPos.coords.accuracy > 0) {
				if (debugMode) {
					debugDiv.style.display = "block";
					debugDiv.innerHTML =
							 "&nbsp;&nbsp;#: "+itCount+"<br>"
							+"Lat: "+currPos.coords.latitude+"<br>"
							+"Lng: "+currPos.coords.longitude+"<br>"
							+"Acc: "+currPos.coords.accuracy+ " m<br>";
					debugDist = new Array();
				}
				var nearestZone;
				var foundZone = null;
				for (i = 0; i < BethMapZones.length; i++) {
					var c = {latitude:BethMapZones[i].lat,longitude:BethMapZones[i].lng};
					var d = distanceBetween(currPos.coords,c);
					if (debugMode) {
						debugDist.push({z:BethMapZones[i],d:d});
					}
					if (i == 0 || d < nearestZone.d) {
						nearestZone = {
							z: BethMapZones[i],
							d: d
						};
					}
					if (d < BethMapZones.r) {
						foundZone = BethMapZones[i];
						break;
					}
				}
								
				if (debugMode) {
					debugDist.sort(function(a,b) {
						return a.d - b.d;
					});
					for (dbgI = 0; dbgI < debugDist.length; dbgI++) {
						debugDiv.innerHTML += debugDist[dbgI].z.id + ": " + debugDist[dbgI].d+"&prime;<br>";
					}
				}
				
				if (nearestZone.d > MIN_DISTANCE) {
					foundZone = StarZone;
				}
				else if (!foundZone) {
					foundZone = nearestZone.z;
				}
				document.getElementById("lbl").innerHTML = "View information about the<br>"+foundZone.title;
				document.getElementById("updateBtn").className = "";
				document.getElementById("updateBtn").onclick = function() {
					showInfo(foundZone.id);
				}
				
				document.getElementById("botLbl").innerHTML = "View information about the<br>"+foundZone.title;
				document.getElementById("botBtn").onclick = function() {
					showInfo(foundZone.id);
					window.scrollTo(0,1);
				}
				
				//document.getElementById("info").className = foundZone.id;
			}
			else {
				showWaiting();
			}
		}
		catch(e) {}
	}
	
	var showInfo = function(id) {
		if (id != "about" && (useW3C || useGears)) {
			document.getElementById("botBtn").style.display = "block";
		}
		else {
			document.getElementById("botBtn").style.display = "none";
		}
		document.getElementById("info").className = id;
		
		var navSel = document.getElementById("nav");
		for (i = 0; i < navSel.options.length; i++) {
			if (navSel.options[i].value == id) {
				navSel.selectedIndex = i;
				break;
			}
		}
	}
	
	var showWaiting = function() {
		document.getElementById("lbl").innerHTML = "Determining Position&hellip;";
		document.getElementById("updateBtn").className = "waiting";
		document.getElementById("updateBtn").onclick = function(){return true;};
		document.getElementById("botBtn").style.display = "none";
	}
		
	var distanceBetween = function(p1,p2) {
		// From http://www.faqs.org/faqs/geography/infosystems-faq/
		// Using formula R (in km) = 6378 - 21 * sin(lat)
		// lat = 37
		var R = 20883407.4 // ft (converted from km);
		
		var dlon = (p2.longitude - p1.longitude)*(Math.PI/180);
		var dlat = (p2.latitude - p1.latitude)*(Math.PI/180);
		var l2rads = p2.latitude*(Math.PI/180);
		var l1rads = p1.latitude*(Math.PI/180);
		
		// from http://www.faqs.org/faqs/geography/infosystems-faq/ (haversine formula)
		var a = Math.pow(Math.sin(dlat/2),2) + Math.cos(l1rads) * Math.cos(l2rads) * Math.pow(Math.sin(dlon/2),2);
		var c = 2 * Math.asin(Math.min(1,Math.sqrt(a)));
		var d = R*c;
		
		return d;
	}

	// detect if using desktop
	if (typeof(window.screen) != "undefined" && typeof(window.screen.width) != "undefined" && window.screen.width > 640) {
		document.body.className = "desktop";
	}
	else {
		window.scrollTo(0,1);
	}

	if (!geolocationSupported) {
		document.getElementById("no_gps").style.display = "block";
		document.getElementById("updateBtn").style.display = "none";
	}
	else {
		
		document.getElementById("has_gps").style.display = "block";
		
		var posOpts = {
			enableHighAccuracy: true,
			maximumAge:      1000, // how many milliseconds old can the position be?
			timeout:         5000 
		};

		if (useW3C) {
			navigator.geolocation.watchPosition(function(position) {
					currPos = position;
					updateDisplay();
				},
			function(e) {
				if (e.code == 3) {
					showWaiting();
				}
			},
			posOpts);
		}
		else if (useGears) {
			var geo = google.gears.factory.create('beta.geolocation');
		    geo.watchPosition(function(position) {
						currPos = position;
						updateDisplay();
					},
				function(e) {
					if (e.code == 3) {
						showWaiting();
					}
					else {
						/* do nothing
						alert(e.message);
						*/
					}
				},
				posOpts
			);
		}
	}

	var navSel = document.getElementById("nav");
	navSel.onchange = function() {
		showInfo(navSel.options[navSel.selectedIndex].value);
		window.scrollTo(0,1);
	};
	
}

function tdbg() {
	debugMode = !debugMode;
	if (debugMode) {
		document.getElementById("debug").style.display = "block";
		document.getElementById("debug").innerHTML = "Waiting for next GPS update...";
	}
	else {
		document.getElementById("debug").style.display = "none";
	}
}

