// ==UserScript==
// @name           FA Colorize Names
// @namespace      http://wakimiko.wwwchan.com
// @description    Colorizes names according to the poster's ID.
// @author         WakiMiko
// @license        GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @include        http://boards.fightingamphibians.org/*/*
// ==/UserScript==

var MOD = 210; 
var ADJUST = 20;

function idToColor(id) {

	var red = parseInt(id.substring(0,2), 16);
	var green = parseInt(id.substring(2,4), 16);
	var blue = parseInt(id.substring(4,6), 16);

	red = red % MOD + ADJUST;
	blue = blue % MOD + ADJUST;
	green = green % MOD + ADJUST;
		
	return "#" + decToHex(red) + decToHex(green) + decToHex(blue);
	
}

function decToHex(i) {

	return i > 16 ? i.toString(16) : ("0" + i.toString(16));
}


function colorize(postername, color) {
	
	if(postername.firstChild && postername.firstChild.tagName == "A")
		postername.firstChild.style.color = color; //color mailto link if there is one
	else
		postername.style.color = color; //if not color parent span tag instead
		
	if(postername.nextSibling.className == "postertrip") //color trip if there is one
		postername.nextSibling.style.color = color;

}



var names = document.evaluate("//span[@class='postername']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

if(names.snapshotLength == 0 || !names.snapshotItem(0).parentNode.nextSibling.nodeValue.match(/ID: /)) //this is ugly
	return; // we are on a board without IDs
	
for(var i = 0; i < names.snapshotLength; i ++) {
	var current = names.snapshotItem(i);
	var id = current.parentNode.nextSibling.nodeValue.match(/ID: ([a-f0-9]{6})/)[1];
		color = idToColor(id);

	colorize(current, color);
		
}

