/*
Geocaching Page Title - v1.2 2008-07-26
(c) 2008, Prime Suspect Software

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

Compatible with Greasemonkey 0.6.4.

Modifies Cache Page Titles

Change Log:

* (v1.2) 2008-07-26 Bug fix.

* (v1.1) 2008-07-25 Fix to accomodate site changes.

* (v1.0) 2008-07-15 Initial release.


// ==UserScript==
// @name             GC Customize Browser Title
// @description      (v1.2) Modifies Cache Page Titles
// @namespace        http://gmscripts.locusprime.net/
// @include          http://www.geocaching.com/seek/cache_details.aspx?*
// @homepage         http://gmscripts.locusprime.net/
// ==/UserScript==

*/

	//  Trim functions.  Use as yyy = xxx.trim();
	String.prototype.trim=function() {
		return this.replace(/^\s*|\s*$/g,''); 
	}
	String.prototype.ltrim=function() {
		return this.replace(/^\s*/g,'');
	}
	String.prototype.rtrim=function() {
		return this.replace(/\s*$/g,'');
	}

	//  Get currently signed-on geocaching.com profile.
	var e_LogIn = document.getElementById("Header1_urlLogin");
	if (e_LogIn.firstChild.data != 'Log out') { return; }
	SignedInAs = e_LogIn.parentNode.childNodes[1].firstChild.data;
	SignedInAs = SignedInAs.replace(/<\&amp;>/g, '&');

	
	//  Register edit options
	GM_registerMenuCommand('Customize Browser Title Template', fEditTemplate);
	GM_registerMenuCommand('Use Cache Icon as Browser Page Icon?', fEditIcon);

	
	//  Get title template.
	var dftTmplt = '%wpt% (%type%) %name% by %owner% in %loc%';
	var oTmplt = GM_getValue("Template_" + SignedInAs, dftTmplt);
	
	
	//  Get individual elements needed to build title. Waypoint, Name, Type, Location, Owner.
	var x = document.title.split(' ');
	var cWptid = x[0];

	var e_CacheName = document.getElementById("CacheName");
	var cName = e_CacheName.firstChild.data;
	
	var cTypeImg = e_CacheName.parentNode.childNodes[1].firstChild;
	var cType = cTypeImg.alt;
	cType = cType.replace(/[ -]cache$/i,'');
	cType = cType.replace(/Cache In Trash Out Event/i,'CITO');
	cType = cType.replace(/\sExhibit/i,'');
	cType = cType.replace(/Letterbox/i,'LBX');
	
	var e_CacheOwner = document.getElementById("CacheOwner");
	var cOwner = e_CacheOwner.childNodes[1].firstChild.data;
	
	var e_Location = document.getElementById("Location");
	var cLocation = e_Location.firstChild.data;
	cLocation = cLocation.replace(/^In\s/i,'');

	
	//  Insert data into template.
	nTitle = oTmplt.replace(/%wpt%/gi, cWptid);
	nTitle = nTitle.replace(/%type%/gi, cType);
	nTitle = nTitle.replace(/%loc%/gi, cLocation);
	nTitle = nTitle.replace(/%owner%/gi, cOwner);
	nTitle = nTitle.replace(/%name%/gi, cName);

	
	//	Change document title.
	document.title = nTitle;

	
	//  Change page icon to match cache icon.
	var ChangeIcon = GM_getValue('UseIcon' + SignedInAs, 'On');
	if (ChangeIcon == 'On') {
		var tUrl = cTypeImg.src;
		if (! tUrl.match(/1304\.gif$/)) {
			var tUrl = tUrl.replace(/wpttypes/i, 'wpttypes/sm');
		}
		var favicon = document.createElement('link');
		favicon.rel = 'shortcut icon';
		favicon.type ="image/x-icon"
		favicon.href = tUrl;
		var allHead = document.getElementsByTagName('head');
		allHead[0].appendChild(favicon);
	}



// ---------------------------------------Functions----------------------------------------- //	
	
	
	//  Allow user to customize title template. 
	function fEditTemplate() {
		var xTemplate = GM_getValue("Template_" + SignedInAs, dftTmplt);
		xTemplate = xTemplate.trim();
		if (!xTemplate.length) { xTemplate = dftTmplt };		
		var Resp = prompt(
				'Edit the template for formatting the browser title. You may use the following' +
				'replacement values:\n' +
				'\n' +
				'\t%wpt% = Waypoint ID\n' +
				'\t%type% = Cache Type\n' +
				'\t%name% = Cache Name\n' +
				'\t%owner% = Owner\'s Name\n' +
				'\t%loc% = Location of Cache\n' +
				'\n' +
				'Blank out the entry field and click OK to restore the ' +
				'template to its original value.', xTemplate);
		if (Resp == null) {
			alert('Template editing canceled.');
		} else {
			Resp = Resp.trim();
			if (!Resp.length) {
				Resp = dftTmplt;
			}
			GM_setValue("Template_" + SignedInAs, Resp)
			alert('Title Template been edited.\n\n' +
					'This change will appear when the page is refreshed.\n');
		}
	}


	//  Turn Icon display on or off, via Tools menu option.
	function fEditIcon() {
		var ynUseIcon = GM_getValue('UseIcon' + SignedInAs, 'On');
		var Resp = confirm(
				'You can set the cache page browser icon to be the same as\n' +
				'the cache type icon. This lets you see the cache type even\n' +
				'if that part of the browser title is obscured.\n' +
				'\n' +
				'Click OK to turn this function On\n' +
				'Click CANCEL to turn this function Off\n' +
				'\n' +
				'This function is currently turned ' + ynUseIcon + '.');
		if (Resp) {
			ynUseIcon = 'On';
		} else {
			ynUseIcon = 'Off';
		}
		GM_setValue('UseIcon' + SignedInAs, ynUseIcon);
		alert('Cache Icon display has been turned ' + ynUseIcon + '.\n\n' +
				'This change will appear when the page is refreshed.\n');
	}
	