/*
Geocaching More Maps - v1.1 2007-06-12
(c) 2007, Prime Suspect Software

Greasemonkey user script: see http://greasemonkey.mozdev.org

Compatible with Greasemonkey 0.6.4.

Function:
 To the existing Google Maps, this adds Satellite and Hybrid options. 
 Optionally, a link that will show door-to-door driving directions from
 any pre-set coordinates to the cache location (or a point on a road
 that's nearest to the cache).
 
Usage:
 The first time the script is run, you will be asked for a lable for 
 the driving direction link, then the latitude and longitude for your
 starting point. You can change these at any later time by selecting 
 User Script Commands from the browser's Tools menu.

Change Log:

* (v1.1) Altered method of getting waypoint ID to accomodate site change.
* (v1.0) Initial release.

*/

// ==UserScript==
// @name				GC More Maps
// @filename			gc_More_Maps.user.js
// @description			(v1.1) Adds more map options, including driving directions
// @namespace       	http://gmscripts.locusprime.net/
// @include         	http://www.geocaching.com/seek/cache_details.aspx*
// ==/UserScript==

(function() {

	// Declare variables.
	var HomeLabel, HomeDdLat, HomeDdLon, WaypointId, 
			CoordDdLat, CoordDdLon, CoordDd, CoordFound;
	var Reset = 0;
	
	// Make sure version of Greasemonkey is current. (GM_getValue >= V0.3)
	if (!GM_getValue) {
	    alert('Please upgrade to the latest version of Greasemonkey.');
	    return;
	}

	// Get Waypoint ID.
	xPathSearch = "//font[@color='#1c7d87']";
	var PathList = document.evaluate(
		xPathSearch,
		document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null);
	var ik = PathList.snapshotLength;
	if (ik >= 1) {
		var cn = PathList.snapshotItem(0);
		while (cn.nodeName != '#text') {
			cn = cn.firstChild;
		}
		if (cn.nodeName == '#text') {
			WaypointId = cn.data;
		} else {
			WaypointId = 'Cache';
		}
	}
	
	
	// Get coordinates in degree.decimal format.
	e_MapLinks_MapLinks = document.getElementById("MapLinks_MapLinks")
	// If no map links (i.e., locationless cache), then exit script.
	if (!e_MapLinks_MapLinks) {return}
	// Otherwise, continue processing.
	CoordDd = e_MapLinks_MapLinks.innerHTML + "";
	CoordFound = CoordDd.match(/lat=\-?[0-9\.]+/);
	CoordFound = CoordFound[0].match(/\-?[0-9\.]+/);
	CoordDdLat = CoordFound[0];
	CoordFound = CoordDd.match(/lon=\-?[0-9\.]+/);
	CoordFound = CoordFound[0].match(/\-?[0-9\.]+/);
	CoordDdLon = CoordFound[0];

	// Get/Set values.
	GetSetValues(0);

	// Update links.
	UpdateMapLinks();


	// Create Greasemonkey Tools menu option to allow user to change settings.
	GM_registerMenuCommand('Change GC Google Directions Settings', function() {
		GetSetValues(1);
		UpdateMapLinks();
	});




	//--------------Functions--------------//


	function GetSetValues(Reset) {
		
		if (!GM_getValue('HomeLabel') || Reset) {
			if (HomeLabel == null) {HomeLabel = 'My Home'}
			var RtnVar = prompt('Enter label for starting point.\nDirections From...', 
					HomeLabel);
			if ((RtnVar == '') || (RtnVar == null)) {
				if (!Reset) {
					alert('Operation canceled, or entry blank.\nScript aborting. Refresh page to restart');
				} else {
					alert('Operation canceled, or entry blank.\nSelect menu option again to restart');
				}
				return;
			} else {
				HomeLabel = RtnVar;
				GM_setValue('HomeLabel', HomeLabel);
			}
		} else {
			HomeLabel = GM_getValue('HomeLabel');
		}	
	
		if (!GM_getValue('HomeDdLat') || Reset) {
			if (HomeDdLat == null) {HomeDdLat = 'N 00 00.000'}
			var PromptText = 'Enter the LATITUDE for ' + HomeLabel + '.\nCoordinates may be in signed ' +
					'decimal format,\nor degree decimal-minutes format.'
			var RtnVar = prompt(PromptText, HomeDdLat);
			if ((RtnVar == '') || (RtnVar == null)) {
				if (!Reset) {
					alert('Operation canceled, or entry blank.\nScript aborting. Refresh page to restart');
				} else {
					alert('Operation canceled, or entry blank.\nSelect menu option again to restart');
				}
				return;
			} else {
				HomeDdLat = RtnVar;
				GM_setValue('HomeDdLat', HomeDdLat);
			}
		} else {
			HomeDdLat = GM_getValue('HomeDdLat');
		}
	
		if (!GM_getValue('HomeDdLon') || Reset) {
			if (HomeDdLon == null) {HomeDdLon = 'W 000 00.000'}
			var PromptText = 'Enter the LONGITUDE for ' + HomeLabel + '.\nCoordinates must be in ' +
					'the same format\nused for the LATITUDE.'
			var RtnVar = prompt(PromptText, HomeDdLon);
			if ((RtnVar == '') || (RtnVar == null)) {
				if (!Reset) {
					alert('Operation canceled, or entry blank.\nScript aborting. Refresh page to restart');
				} else {
					alert('Operation canceled, or entry blank.\nSelect menu option again to restart');
				}
				return;
			} else {		
				HomeDdLon = RtnVar;
				GM_setValue('HomeDdLon', HomeDdLon);
			}
		} else {
			HomeDdLon = GM_getValue('HomeDdLon');
		}
		
	}



	
	function UpdateMapLinks() {
		// Declare local variables.
		var MoreMapsURL, CurMaps, MapsPart1, MapsPart2, MapsAll, HomeLableOK;
		// Fix any angle brackets in home label name.
		HomeLabelOK = HomeLabel.replace(/>/g,'&gt;');
		HomeLabelOK = HomeLabelOK.replace(/</g,'&lt;');
		// Create string for new map links.
		MoreMapsURL  = '<a href="http://maps.google.com/maps?q=' + CoordDdLat + '+' + CoordDdLon +
				'+(' + WaypointId +')" target="_blank">Google Maps</a><br>';
		MoreMapsURL += '<a href="http://maps.google.com/maps?q=' + CoordDdLat + '+' + CoordDdLon +
				'+(' + WaypointId +')&t=k" target="_blank">Google Satellite</a><br>';
		MoreMapsURL += '<a href="http://maps.google.com/maps?q=' + CoordDdLat + '+' + CoordDdLon +
				'+(' + WaypointId +')&t=h" target="_blank">Google Hybrid</a><br>';
		MoreMapsURL += '<a href="http://maps.google.com/maps?q=' + HomeDdLat + ',' + HomeDdLon + 
				'+(' + escape(HomeLabel) + ')+to+' + CoordDdLat + ',' + CoordDdLon +
				'+(' + WaypointId +')" target="_blank">Directions From ' + 
				HomeLabelOK + '</a><br>';	
		// Replace existing Google Maps string with MoreMaps string.
		CurMaps = e_MapLinks_MapLinks.innerHTML;
		MapsPart1 = CurMaps.substring(0, CurMaps.indexOf('<a href="http://maps.google'));
		MapsPart2 = CurMaps.substring(CurMaps.indexOf('<a href="http://www.mapquest'));
		MapsAll = MapsPart1 + MoreMapsURL + MapsPart2;
		e_MapLinks_MapLinks.innerHTML = MapsAll;
	}

})();