// Gmail: Random Signature Remote
// version 1.0
// 2006-07-04
// Copyright (c) 2006- Stuart Langridge
// Based on Gmail: Random Signature 0.4 BETA
// Copyright (c) 2005-2006, Robson Braga Araujo
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Gmail: Random Signature Remote", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Gmail: Random Signature Remote
// @namespace     http://www.kryogenix.org/code/browser/greasemonkey/
// @description   Insert a random sig in your gmail email, fetched from a URL.
// @include       http://gmail.google.com/*
// @include       https://gmail.google.com/*
// @include       http://mail.google.com/*
// @include       https://mail.google.com/*
// @author        Stuart Langridge (sil at kryogenix dot org)
//
/////////////////////////////////////////////////////////////
//
//
// ==/UserScript==

var UpdateGmailVariable = null;

(function(){

var username;
init();

 
function init()
{
	// Get the current username to retrieve the correct preferences
	username = getUsername();
	if (username && username == "") return;

	// For some reason, the Gmail data function (top.js.P) now invalidates the page. I couldn't understand
	// why, so I'll just find the function that updates the variables and use that.
	var js = unsafeWindow.top.document.getElementsByName('js')[0].contentWindow;
	if (js.wrappedJSObject) js = js.wrappedJSObject;
	for (var propName in js) {
		var propValue = js[propName];
    
		if (typeof(propValue) != "function") {
			continue;
		}
    
		var functionBody = propValue.toString();

		// This if is just for performance reasons
		if (functionBody.indexOf('a[b][0]') != -1) {
			// This is to make sure we have the right function
			if (functionBody.match(/\w\[\w\[\w\]\[\w\]\]\s*=\s*\w\[\w\]\[\w\]/)) {
				UpdateGmailVariable = propValue;
				// TODO This break makes Firefox 1.5.0.1 crash
				//break;
			}
		}
	}

	changeSignature();
}

function getUsername()
{
	user_tag = document.getElementById('prf_g').parentNode.childNodes[0];
	return user_tag.data ? user_tag.data : user_tag.childNodes[0].data;
}

// function copied from http://www.faqts.com/knowledge_base/view.phtml/aid/11698
function addHTML (ele, html) {
	if (document.all)
		ele.insertAdjacentHTML('beforeEnd', html);
	else if (document.createRange) {
		var range = document.createRange();
		range.setStartAfter(ele.lastChild);
		var docFrag = range.createContextualFragment(html);
		ele.appendChild(docFrag);
	}
	else if (document.layers) {
		var l = new Layer(window.innerWidth);
		l.document.open();
		l.document.write(html);
		l.document.close();
		l.top = document.height;
		document.height += l.document.height;
		l.visibility = 'show';
	}
}

function changeSignature() {
	if (UpdateGmailVariable) {
	  GM_xmlhttpRequest({
	    method: 'GET',
	    url: 'http://example.org/CHANGE_THIS_URL/fortunes.txt',
	    headers: {
	        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
	        'Accept': 'text/plain',
	    },
	    onload: function(responseDetails) {
	      var tagline_array = responseDetails.responseText.split('\n%\n');
	      dynamic_part = tagline_array[Math.floor(Math.random()*tagline_array.length)];
  	    UpdateGmailVariable(['p', ['sx_sg', dynamic_part]]);
	    }
		});

  }
}

})();






