// ==UserScript==
// @name        Metro Mojo Hotbox
// @namespace   http://dse.webonastick.com/
// @author      Darren Stuart Embry http://dse.webonastick.com/
// @description If you have mail, display a fixed link in the upper-right corner.
// @include http://www.*mojo.com/*
// @exclude http://www.*mojo.com/*/mymessages.cfm
// @exclude http://www.*mojo.com/*/mymessages.cfm?*
// @exclude http://www.*mojo.com/*/sendmessage.cfm
// @exclude http://www.*mojo.com/*/sendmessage.cfm?*
// ==/UserScript==
//
// This is a Greasemonkey user script.
// To install it, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox, and revisit this script.
//
// If you're still using Internet Explorer, then well, it ain't my problem.

(function() {

	var anchors = document.getElementsByTagName("a");
	if (!anchors || !anchors.length) return;

	var initialSize = 150;	// required

	var throbMode = false;
	var testMode = false;
	var alignRight = true;	// slow when throbbing.

	var throbSizeIncrement = 50;
	var throbTime = 50;
	var throbDelay = 4000;
	var numberOfThrobs = 2;
	var throbSteps = 2;

	var insertIndicator = function (msgs, href) {
		
		var newAnchor = document.createElement("a");
		if (href) {
			newAnchor.href = href;
		}
		newAnchor.appendChild(
			document.createTextNode("hot box " + msgs));
		newAnchor.style.position = "fixed";
		newAnchor.style.top = "0px";
		if (alignRight) {
			newAnchor.style.right = "0px";
		} else {
			newAnchor.style.left = "0px";
		}
		newAnchor.style.padding = "4px";
		newAnchor.style.textDecoration = "none";
		newAnchor.style.backgroundColor = "red";
		newAnchor.style.color = "yellow";
		newAnchor.style.fontWeight = "bold";
		newAnchor.style.fontFamily =
			"arial, helvetica, sans-serif";
		newAnchor.style.fontSize = initialSize + "%";
		newAnchor.style.borderStyle = "solid";
		newAnchor.style.borderColor = "yellow";
		newAnchor.style.borderWidth = "2px";
		document.body.appendChild(newAnchor);

		if (!throbMode) return;
		
		newAnchor.states = [];
		var size = initialSize;
		var state = { "delay": throbDelay };
		newAnchor.states.push(state);
		
		for (var j = 0; j < numberOfThrobs; ++j) {
			for (var i = 0; i < throbSteps; ++i) {
				var state = { 
					"size": size += throbSizeIncrement,
					"delay": throbTime 
				};
				newAnchor.states.push(state);
			}
			for (var i = 0; i < throbSteps; ++i) {
				var state = {
					"size": size -= throbSizeIncrement,
					"delay": throbTime 
				};
				newAnchor.states.push(state);
			}
		}
		newAnchor.stateIndex = 0;
		
		newAnchor.nextState = function () {
			var state = newAnchor.states[newAnchor.stateIndex];
			if (state.size) {
				newAnchor.style.fontSize = state.size + "%";
			}
			window.setTimeout(
				function () {
					newAnchor.stateIndex++;
					newAnchor.stateIndex %= 
						newAnchor.states.length;
					newAnchor.nextState();
				},
				state.delay
			);
		};

		newAnchor.nextState();
	};

	for (var i = 0; i < anchors.length; ++i) {
		var anchor = anchors[i];
		var href = anchor.href;
		if (!href) continue;

		if (!(/mymessages\.cfm/i.test(href))) continue;

		if (!anchor.hasChildNodes()) continue;
		var child = anchor.childNodes[0];
		if (child.nodeType != 3) continue; /* 3 == Node.TEXT_NODE */
		var text = child.data;

		var regexps = [/\b(\d+) msgs?\b/i, /\bhot box (\d+)\b/];

		for (var j = 0; j < regexps.length; ++j) {
			var match = text.match(regexps[j]);
			if (match && match.length) {
				insertIndicator(match[1], anchor.href);
				return;
			}
		}
	}

	if (testMode) {
		insertIndicator("none", null);
	}
})();
