// ==UserScript==
// @name		deviantFORUM - Mark Recent
// @namespace		http://www.youkai.de/da-scripts
// @description		Mark the most recent visible posts in a thread
// @include		http://forum.deviantart.com/*
// ==/UserScript==

// Written by NekoDramon - http://nekodramon.deviantart.com
// Modifications by Kalle Räisänen - http://www.five-by-five.se/
// Version 1.01-K (Dec 11, 2005)


(function()
{

function toHex(intval)
{
	var hexvals = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
	var str = hexvals[parseInt(intval/0x10)] + hexvals[intval - (parseInt(intval/0x10)*0x10)];
	return str;
}

function humanize(val)
{
	switch(val)
	{
		case 1: return 'Most';
		case 2: return '2nd most';
		case 3: return '3rd most';
		default:return val + 'th most';
	}
}

var mb_links = '';
var modblock, commentrange, counter, current;
var commentindex = new Array();
var colors = new Array(0xff, 0xf0, 0xee);

modblock = document.getElementById('output');
commentrange = document.evaluate("//div[@class='item-head float-holder']/a[@id]", modblock, null, XPathResult.ANY_TYPE, null); 

counter = 0;
current = commentrange.iterateNext();

if(!current)
	return;

while (current)
{
	commentindex[counter] = current.id;
	commentindex[counter] = commentindex[counter].slice(1);
	current = commentrange.iterateNext();
	counter++;
}
commentindex.sort(function(a,b){return a-b;});

for(var i = 1; i < 6; i++)
{
	var coltext = '#';
	current = document.getElementById('c' + commentindex[counter-i]);
	current = current.parentNode;

	for(j = 0; j < 3; j++) {
		coltext += toHex(colors[j] - (i * 0x1a));
	}
   current.style.backgroundColor = coltext;
	current.innerHTML += '<strong>' + humanize(i) + 
	                     '</strong> recent (visible) post. <strong><a href="#deviantART">Top</a></strong></p>';
	current.parentNode.innerHTML = '<a name="p' + commentindex[counter-i] + '"></a>' + current.parentNode.innerHTML;

	mb_links += ((i == 1) ? '&raquo;' : ',') + ' <a href="#p'+ commentindex[counter-i] + '"><strong>' +
	                                           humanize(i) + '</strong> recent post</a>';
	if(counter-i == 0)
		break;
}

var top_comment = document.getElementById('c' + commentindex[0]);
var forum_top   = top_comment.parentNode.parentNode;
forum_top.innerHTML = '<p class="item-body" style="margin: -1em 0em 1em 0em; border-top: 0px;">' + 
                      mb_links + '</p>'+ forum_top.innerHTML;

})();
