// ==UserScript==
// @name           Lizard Feeder: Focus/Context + Word Cloud
// @namespace      feeds.mozilla.com
// @include        http://feeds.mozilla.com/
// ==/UserScript==

/*
 * Multi-column (just add float left, line 29)
 * unsuccesful port to DOM mutation
 * Version 0.0.4 Feb 20, 2009
 * Initial version by Andy Edmonds
 * This script is licensed under the MPL.
 * Excluding ReplaceAll prototype (license unstated)
 */


String.prototype.replaceAll = function(pcFrom, pcTo){
	var i = this.indexOf(pcFrom);
	var c = this;
	while (i > -1){
	c = c.replace(pcFrom, pcTo); 
	i = c.indexOf(pcFrom);
	}
	return c;
}

var feedHack = {
	refresh: 300,
	timeout: false,
	counts: {},
	words: {},
	ignore: { 'the': true, 'a':true, 'of':true, 'bug': true, '1': true, '2':true, 'mozilla': true, 'rt': true, 'its':true, 'doesnt': true,
	'it':true, 'is':true, 'in': true, 'at': true, 'and':true, 'com':true, 'it':true, 'from':true, 'when':true, 'for':true, 
	'tinyurl':true, 'to': true, '0':true, 'and':true, 'bit':true, 'ly':true, 'https':true, 'on':true, 'no':true, 'do':true,'you':true,'all':true, 
	'with':true, 'how':true, 'be':true, 'like':true, 'will':true, 'me':true, 'my':true, 'but':true, 'can': true, 'as':true, 'or':true, 'not':true,
	 'are':true, 'does':true, 'if':true, 'that':true, 'has':true, 'an':true, 'also':true, 'www':true
	}
};



feedHack.init = function init() {
	$ = unsafeWindow.jQuery;
	$("li.filter").css("opacity", .5);
	$("li.filter").css("font-size", this.inactiveEm + 'em');
	$("#intro").css("z-index", 20);
	$("#content").append("<div style='position:absolute;top:180px;left:630px;z-index:10;opacity:.5;background-color:;width:400px;height:2000px;overflow:hidden;' id='wordpalette'>&nbsp;</div>");
	window.setInterval(this.feedHack, this.refresh);
}

// From http://diveintogreasemonkey.org/patterns/add-css.html
feedHack.addGlobalStyle = function (css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}



feedHack.feedHack = function () {
	$ = unsafeWindow.jQuery;
	var text;
	for(var type in feedHack.counts) {
	feedHack.counts[type]=0;
	$("li.filter.group-" + type).css("opacity", 1);
}
$("li.entry:visible").each(
	function( offset ){
	//console.log(this);
		if($(this).css("maxWidth") != '360px') {
			node = this.getElementsByTagName("A")[0].innerHTML;
			node = node.replaceAll("http://","");
			node = node.replaceAll("/"," ");
			node = node.replaceAll("\\"," ");
			node = node.replaceAll("."," ");
			node = node.replaceAll("."," ");
			node = node.replaceAll("."," ");
			node = node.replaceAll(","," ");
			node = node.replaceAll("- "," ");
			node = node.replaceAll(" -"," ");
			node = node.replaceAll("_"," ");
			node = node.replaceAll(":"," ");
			node = node.replaceAll("'"," ");
			node = node.replaceAll('"',' ');
			node = node.replaceAll('"',' ');
			node = node.replaceAll(')','');
			node = node.replaceAll('(','');
			node = node.replaceAll('!','');
			node = node.replaceAll('?','');
			node = node.replaceAll('>','');
			node = node.replaceAll('<','');
			node = node.replaceAll('[','');
			node = node.replaceAll(']','');
			node = node.replaceAll(';','');
			node = node.replaceAll('@','');
			var nodes = node.split(" ");
			for(var i =0;i<nodes.length;i++) {
			text = nodes[i];
			
			if(text.length > 1 && typeof(feedHack.ignore[text.toLowerCase()]) == 'undefined') {
			if(typeof(feedHack.words[text.toLowerCase()]) == 'undefined') {
			feedHack.words[text.toLowerCase()] = new Object; 
			feedHack.words[text.toLowerCase()].count=0;
			feedHack.words[text.toLowerCase()].pretty=nodes[i];
			//console.log("adding " + nodes[i]);
			}
			feedHack.words[text.toLowerCase()].count++ 
			}
		}
	}
	this.style.maxWidth = '360px';
	
	var arClass = this.className.split(" ");
		for(var classOffset = 0; classOffset < arClass.length; classOffset++) {
			if(arClass[classOffset].indexOf("group-") > -1) {
			var type = arClass[classOffset].split("-")[1];
			if(!feedHack.counts[type]) feedHack.counts[type]=0;
			feedHack.counts[type]++;
		}
	}
	});
	var sum = 0;
	for(var type in feedHack.counts) {
		sum += feedHack.counts[type];
	}
	for(var type in feedHack.counts) {
		//console.log("Bouns ems " + (feedHack.counts[type]/sum)+ " " + ((1.0 * feedHack.baseEm + ((feedHack.counts[type]/sum)*feedHack.ems)) + "em") );
		$("li.group-"+ type + ".filter" ).css("font-size", (1.0 * feedHack.baseEm + ((feedHack.counts[type]/sum)*feedHack.ems)) + "em") ;
	}
	//console.log(feedHack.words);
	
	for(j in feedHack.words) {
		var id = "s" + encodeURIComponent(j);
		var ele = $("#" + id);
		var count =feedHack.words[j].count; 
		if(count > 2) {
			if($("#" + id).length ) {
				if( ele.attr("count") != count) {
					var temp =  $("#" + id).remove();
					temp.prependTo("#wordpalette");
					$("#" + id).css("fontSize", .5 + Math.log(count) + "em");		
					$("#" + id).attr("count", count);
				}
		 	} else {
				$("#wordpalette").append('<span count="' +  count + '" style="font-size:' + (.5 + Math.log(count)) + 'em;" id="' + id + '">' + feedHack.words[j].pretty + ' </span>');
				 
			}
		}
	}
}


feedHack.init();

