/*
Geocaching True Totals - v1.0 2005-11-06
(c) 2005, Prime Suspect Software

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

Function:
 Add a Finds-Per-Day statistic, when displaying either all logs,
 or all finds.
 
Notes:
 The number of days used for the calculation are counted from the day
 you logged your first find, to the current date. The Finds count
 represents the total number of Find logs, not the number of caches
 you logged Finds to. If you logged a Find to the same cache twice,
 it counts as two finds. Events logged prior to the introduction of
 the "attended" log type will be included as a cache find.

*/

// ==UserScript==
// @name           GC Finds Per Day
// @namespace      http://gmscripts.locusprime.net/
// @description    Adds Finds-Per-Day statistic
// @include        http://www.geocaching.com/my/logs.aspx?*
// ==/UserScript==

(function() {

	var unsafeWindow = unsafeWindow || window;
	
	//  Check URL parms
	var s = UrlParm('s', true);
	var lt = UrlParm('lt', true);
	
	//  If not showing all found logs, exit script.
	if ((s != '') && (s != '1')) {return}
	if ((lt != '') && (lt != '2')) {return}

	//  Build search path.
	var xPathSearch = "//img[@alt='Found it']";
	//  Execute xpath query to get a collection of images.
	var ImgList = document.evaluate(
		xPathSearch,
		unsafeWindow.document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null);
	
	//  Total Finds.
	var totFinds = ImgList.snapshotLength;
	
	//  Get date of first find.
	var xFirst = ImgList.snapshotItem(totFinds - 1);
	var xDate = xFirst.parentNode.nextSibling.nextSibling.firstChild.data;
	var xDateParts = xDate.split('/');
	var dStart = new Date(0);
	dStart.setFullYear(xDateParts[2], xDateParts[0] - 1, xDateParts[1]);
	dStart.setHours(0);
	dStart.setMinutes(0);
	dStart.setSeconds(0);

	//  Current date
	var dNow = new Date();
	dNow.setHours(0);
	dNow.setMinutes(0);
	dNow.setSeconds(0);
	
	//  Calculate days per find.
	var delta = ((dNow - dStart) / 86400000) + 1;	
	delta = parseInt(delta);
	var fpd = totFinds / delta;
	fpd = fpd.toFixed(4);
	
	//  Add to page.
	var e_lbHeading = unsafeWindow.document.getElementById("lbHeading");
	if (e_lbHeading) {
		var tNode = e_lbHeading.parentNode.nextSibling.nextSibling;
		rText = tNode.data;
		rText = rText.replace(/:/, '');
		tNode.data = rText + ' (Finds/Day: ' + fpd + '):';
	}
	

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


	//  Returns a URL parameter.
	//    ParmName - Parameter name to look for.
	//    IgnoreCase - (optional) *false, true. Ignore parmeter name case.
	//    UrlString - (optional) String to search. If omitted, document URL is used.
	function UrlParm(ParmName, IgnoreCase, UrlString) {
		var RegRslt, sc = '', RtnVal = '';
		if (IgnoreCase) {sc = 'i'}
		if(UrlString) {
			var PageUrl = UrlString;
		} else {
			PageUrl = unsafeWindow.document.location + '';
		}
		var ParmString = PageUrl.substring(PageUrl.indexOf('?') + 1);
		var RegEx1 = new RegExp('(^|&)' + ParmName + '=(.*?)(&|#|$)', sc);
		RegRslt = RegEx1.exec(ParmString);
		if (RegRslt) {RtnVal = RegRslt[2]}
		return RtnVal;
	}



})();

