// ==UserScript==
// @name          Reddit colorizer
// @namespace     http://jeffpalm.com/redditcolor/
// @description   Colorizes the reddit scores
// @include       http://*.reddit.com*
// ==/UserScript==

/*
 * Copyright 2009 Jeffrey Palm.
 */

var TESTING = false;


function main() {
    var divs = document.getElementsByTagName("DIV");
    var scores = new Array();
    var min;
    var max;
    for (var i=0; i<divs.length; i++) {
	var d = divs[i];
	if (d.className.match(/.*score.*/)) {
	    var s = d.innerHTML;
	    if (!s.match(/\d+/)) continue;
	    var score = parseInt(s);
	    scores.push(d);
	    if (!min || score<min) min = score;
	    if (!max || score>max) max = score;
	}
    }
    for (var i=0; i<scores.length; i++) {
	var d = scores[i];
	var v = parseInt(d.innerHTML);
	var cval = Math.floor(0xff - 0xee*(max-v)/(max-min));
	var color = cval.toString(16) + "0000";
	d.style.color = "#" + color;
    }
}

try {main();} catch (e) {if (TESTING) alert('Error: ' + e);}
