// Copyleft (c)2007, The GreaseMonkey Scripter
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
// This is a Greasemonkey user script.
//
// To run this script, you need Greasemonkey: http://www.greasespot.net/
// Then restart Firefox and revisit this script.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "CommentCollapser", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name           MotleyFool CAPS Stats
// @namespace      http://gmscripter.blogspot.com/
// @description    On MotelyFool's CAPS site, calc some percentages.
// @include        http://caps.fool.com/Ticker.aspx?ticker=*
// ==/UserScript==

// START: id="allstar"
var allStarDivs = document.getElementById("allstar").getElementsByTagName("DIV");
var addendStack = [];
	
function calcPct(addends, destinationNode) {
	var out = parseFloat(addends[0]);
	var under = parseFloat(addends[1]);
	var pct = 0.0;
	if ((out + under) > 0.0) { // no division by zero
		pct = ( out / (out + under) ) * 100.0;
		var radix = 10;
		destinationNode.nodeValue =  "= " + pct.toString(radix).substr(0,5) + "%) " + destinationNode.nodeValue;
	} else {
		destinationNode.nodeValue =  "= n/a%) " + destinationNode.nodeValue;
	}
}

GM_log("Starting MotleyFool CAPS Stats...");
// length - 3, to ignore deep nodes at end
for (var i=0; i < allStarDivs.length -3; i++) {  // TODO (var i in allStarDivs) causes error!? - compare other userscript
	if (allStarDivs[i].className == "totalbulls") {
		addendStack.push( allStarDivs[i].firstChild.firstChild.nodeValue);
	}
	if (allStarDivs[i].className == "totalbears") {
		addendStack.push( allStarDivs[i].firstChild.firstChild.nodeValue);
		calcPct(addendStack, allStarDivs[i].firstChild.firstChild);
		addendStack = [];
	}
}
