/*
Geocaching Customized Statbar on "My" page - 20070517
(c) 2007, Ranger Fox (Creater of the Statbar Modifier)

Greasemonkey user script: see http://greasemonkey.mozdev.org

Compatible with Greasemonkey 0.6.4.

Function:
 Replaces the gc.com statbar on your "my" page with the one you created
 with the Statbar Modifier.
 
Usage:
 Make sure you have the Statbar Modified statbar URL.  Visit your "my"
 page for the first time and it will ask you for the URL.  Put that in
 and it will replace the "my" statbar with your modified one.  Keep in
 mind there may be a resizing issue with that column.

Features:
 Nothing much else.

You get a chance to see a lot of my test code in this version!

Change Log:

* (v0.1) Test release -- well, it works...
 
*/

// ==UserScript==
// @name           GC Statbar on My
// @namespace      http://www.devfolio.com/geocaching/statbar
// @description    (v0.1) Add your Statbar Modifier statbar to your gc.com "my" page
// @include        http://www.geocaching.com/my/*

(function() {



	//  Globals.
	var username = '';
	var modified = false;

	// Make sure version of Greasemonkey is recent enough. (GM_getValue >= V0.3)
	if (!GM_getValue) {
	    alert('Please upgrade to the latest version of Greasemonkey.');
	    return;
	}

	//  Get currently signed-on geocaching.com profile (for later versions)
	var e_LogInName = document.getElementById("Header1_lnkLoginName");
	if (e_LogInName) {
		username = e_LogInName.firstChild.firstChild.data;
		username = username.replace(/<\&amp;>/g, '&');
	}

	//alert(username);



	
// -------------------------------FUNCTIONS-------------------------------- //



	//  Modify the statbar
	function Exe() {			
		if (modified) return;

		if (GetStatbarURL() != null)
		{
			modified = true;
			var url = GetStatbarURL();

			var img = document.getElementById('imgProfile');
			if (!img) return;

			var img = document.getElementById('imgProfile');

			//I could do this, but there is the sizing issue with the table
			/*
			img.src = GetStatbarURL();
			img.removeAttribute("width");  //because the width will not be right after that
			img.setAttribute("align","right");


			//Adjust the table
			var lnk = document.getElementById('lnkProfile');
			alert(lnk);
			alert(lnk.parentElement.parentElement);
			lnk.parentElement.removeAttribute("width");
			lnk.parentElement.setAttribute("width","300px");

			alert(lnk.parentElement.innerHTML);
			*/

			var p = document.getElementById('lnkProfile');
	
			p.innerHTML = '<div style="float:right;display:block;margin-left:-500px;"><img src="' + url + '" style="border:0" /></div>';

		}
		else
			Setup();
	}



	function Setup() {
		var url = prompt('Please enter your Statbar Modifier statbar URL', GetStatbarURL());
		//  If Cancel button presses.
		if (url == null) {return;}

		if (url != '')
		{
			SaveStatbarURL(url);
			modified = false;
			Exe();
		}		
	}


	//  Save counter value.
	function SaveStatbarURL(url) {
		GM_setValue('StatbarModifierURL_' + username, url + '');
	}
	
	//  Retrieve counter value.
	function GetStatbarURL() {
		var url = GM_getValue('StatbarModifierURL_' + username);
		if (!url) {
			return null;
		}
		if (url.indexOf('http://www.devfolio.com/geocaching/statbar/logo.aspx') == 0)
			return url;
		return null;
	}
		
//http://www.devfolio.com/geocaching/statbar/logo.aspx?uid=dabb99dc-7bbc-44ab-b377-9f30b4f601cb&txt=View+my+profile&img=triad

	Exe();
	//alert('ping');
})();
