/*
Geocaching OS Maps - 0.1 18/05/2005
Copyright (c) 2005, Neil Jones (neil@xisoft.co.uk)
Released under the GPL http://www.gnu.org/copyleft/gpl.html

This is a Greasemonkey user script, see http://greasemonkey.mozdev.org/.

This script puts a Trig Point icon on your user stats page, 
along with a find count.

*/

// ==UserScript==
// @name          Geocaching UK Trigpoint Tracking
// @namespace     http://www.xisoft.co.uk
// @description	  Puts an extra Trigppoint icon on the user profile page
// @include       http://www.geocaching.com/profile/Default.aspx*
// @include       http://www.geocaching.com/seek/cache_details.aspx*
// ==/UserScript==

var TRIG_ICON = "data:image/gif,GIF89a%18%00%18%00%C6A%00e%CA%FDg%CB%FDh%CB%FDi%CB%FDj%CC%FDk%CC%FDo%CD%FDp%CE%FDq%CE%FDr%CE%FDs%CF%FDt%CF%FDu%D0%FDv%D0%FDy%D1%FD%7D%D2%FD~%D3%FD%7F%D3%FD%82%D4%FD%83%D4%FD%8C%D7%FE%90%D9%FE%91%D9%FE%92%D9%FE%93%DA%FE%97%DB%FE%98%DB%FE%99%DC%FE%9D%DD%FE%9F%DE%FE%A0%DE%FE%A1%DF%FE%A2%DF%FE%A3%DF%FE%A7%E1%FE%A8%E1%FE%A9%E1%FE%AB%E2%FE%AD%E3%FE%AF%E3%FE%B1%E4%FE%B5%E6%FE%BB%E8%FE%BC%E8%FE%BE%E9%FE%BF%E9%FE%C1%EA%FE%C3%EA%FE%C4%EB%FE%C8%EC%FE%CD%EE%FE%D2%EF%FE%D3%F0%FE%D9%F2%FF%DA%F2%FF%DB%F3%FF%DE%F4%FF%E0%F4%FF%EB%F8%FF%EC%F9%FF%F6%FC%FF%F7%FC%FF%F8%FD%FF%FD%FE%FF%FE%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF!%FE%15Created%20with%20The%20GIMP%00!%F9%04%01%0A%00%7F%00%2C%00%00%00%00%18%00%18%00%00%07%B4%80%7F%82%83%84%85%86%87%88%89%8A%8B%8C%85%15%19%8D%883%00%00%91%87%0F%94%12%96%84%2B%94%94.%9B%82%07%00%04%03%00%09%A1%22%94'%25%94%24%9B%02%00%0B%3D%3C%0A%00%02%96%16%940AA%2F%94%17%8D4%94%13%40%BD%3F%13%944%8C%10%945%BD%BD6%94%0F%8B%2C%94%1AA9%0D%0C8A%1B%94*%8A%08%A4%3AA%1D%94%1CA%3B%05%00%06%89%23%94%26%BD.%94-%BD(%94!%88%B1%0A%3D%D076%A0%F9h%00%20%C0!%0C%94%5E%40%5B%08-%06%25%0A%86(%05%F0%00%A2%A2%C5%8B%1FL%01%90A(%82%A7%8F%20C%3A%18%D4%22%A4I%93)B%A9%5C%B92%10%00%3B";

GM_log("The Monkey's a Watchin' ...");

// Decide what page we are looking at:
var url=new String(document.location);
if (url.match(/^http:\/\/www\.geocaching\.com\/profile\/Default\.aspx.*/i)) {
	var userName = getGCUsername();
	var count = loadTrigStats(userName, trigStatsLoaded);
}
else if (url.match(/^http:\/\/www\.geocaching\.com\/seek\/cache_details\.aspx.*/i)) {
	enhanceCacheListing();
}

function enhanceCacheListing() {
	GM_log("Enhancing cache page...");
	var links = document.getElementById('FindText');
	if (links) {
	    var newElement = document.createElement('span');
	    newElement.innerHTML = "...all nearby <A href='http://www.trigpointinguk.com/trigs/search-parse.php?gridref=&postcode=&town=&trig=&cache="+getWaypointName()+"&trigname=&view=Display+Trigpoints'>Trig Points</A>";
	    links.parentNode.insertBefore(newElement, links.nextSibling);
	}	
}

// Callback function called when trig stats have been loaded
function trigStatsLoaded(userName, userID, trigCount) {
	GM_log(userName + " has found " + trigCount + " trigs");
	var userFoundTable = document.getElementById("ProfilePanel1_dlUserFound");
	
	//-------------------------------------------------	
     	// HACK to get around a bug on the groundspeak page	
	var row = userFoundTable.rows.item(0);
	var cell = row.cells.item(0);
	var node = cell.childNodes.item(1);
	var userFoundTable = node;
	//-------------------------------------------------	
	
	 
	GM_log("userFoundTable.insertRow()");
	GM_log(userFoundTable.rows.length-3);
	var row = userFoundTable.insertRow(userFoundTable.rows.length-3);	
	var cell = row.insertCell(-1);	
	// Isert break before GB stats icons
	var GBStats = document.getElementById("GB_Stats");
	if (!GBStats) {
		cell.id = "GB_Stats";
		cell.colSpan = 3;
		cell.innerHTML = "<hr noshade width='100%' size='1'>";
	}

	// Insert Icon
	row = userFoundTable.insertRow(userFoundTable.rows.length-3);
	cell = row.insertCell(-1); 
	cell.innerHTML = "<a href='http://www.trigpointinguk.com/info/view-profile.php?u="+userID+"'>"+"<img src=\""+TRIG_ICON+"\" border='0'></a>";
	cell = row.insertCell(-1);
	cell.innerHTML = "Trig Points Logged";
	cell = row.insertCell(-1);
	cell.align="right";
	cell.innerHTML = trigCount;
}

// Load the trig stats page from Barry's excellent trigpointinguk
function loadTrigStats(userName, onLoaded) {
	if (userName) {
		GM_log("Loading Trig finds for "+userName);
		GM_xmlhttpRequest({
		    method: "GET",
		    url: "http://www.trigpointinguk.com/trigtools/user-count.php?user="+userName,
		    onload: function(responseDetails) {
			GM_log("Loaded trig info for "+ userName);
				scrapeTrigCount(responseDetails.responseText);
				if (trigID != -1)
					onLoaded(userName, trigID, trigFinds);
		    },
		    onerror: function(responseDetails) {
			GM_log("Error loading trig info for "+userName+": " + responseDetails.status +
			      " " + responseDetails.statusText);
		    }
		});
	}
}

var trigFinds=0;
var trigID = -1;

// Scrape the trig stats page to return the totals
function scrapeTrigCount(details) {
	trigID = -1;
	GM_log(details);
	var bits = details.split(/\n/);
	GM_log(bits[0]);
	GM_log(bits[1]);
	var user_bits = bits[0].split(/=/);
	var id_bits = bits[1].split(/=/);
	
	trigFinds = parseInt(user_bits[1], 10);
	trigID = parseInt(id_bits[1], 10);
	
	return trigFinds;
}
	
// Scrape the username from GC.com
function getGCUsername() {
	GM_log("Finding GC user name");
	var userProfileLabel = document.getElementById("lblUserProfile");
	if (userProfileLabel) {
		var bits = userProfileLabel.innerHTML.split(/:\s/);
                bits[1]=bits[1].replace(/\s\s/," "); // Replace double spaces
                bits[1]=bits[1].replace(/&amp;/g,"&"); // Replace &amp; with \&
                bits[1]=escape(bits[1]);
		GM_log("User Name: "+bits[1]);
		return bits[1];
	}
	GM_log("No GC User Name found");
	return null;
}

// Get the Waypoint Name
function getWaypointName() {
	GM_log("Finding Waypoint Name");
	var waypoint = document.getElementById("WaypointName");
	if (waypoint) {
		return waypoint.innerHTML;
	}
	GM_log("No Waypoint Name found");
	return null;
}


