//script by wirus - http://wirus.boo.pl

// config
var step = 12;
var normal_color 	= new Array(0, 0, 0);
var hover_color		= new Array(51, 102, 51);

// do not modify the code below!!
var hover = new Array();
var current_r = new Array();
var current_g = new Array();
var current_b = new Array();

function dec2hex(dec)
{
	if(dec < 0) 	dec = 0;
	if(dec > 255) 	dec = 255;
	
	var hex = new Array("0", "1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
	return hex[Math.floor(dec / 16)] + hex[dec % 16];
}

function rgb2hex(r,g,b)
{
	return "#" + dec2hex(r) + dec2hex(g) + dec2hex(b);
}

function highlight(anchor)
{
	var number = anchor.match(/[0-9]+/);
	hover[number] = true;
	return false;
}

function downlight(anchor)
{
	var number = anchor.match(/[0-9]+/);
	hover[number] = false;
	return false;
}

function fade()
{
	for(i = 0; i < hover.length; i++)
	{
		var dest_r = (hover[i] == true) ? hover_color[0] : normal_color[0];
		var dest_g = (hover[i] == true) ? hover_color[1] : normal_color[1];
		var dest_b = (hover[i] == true) ? hover_color[2] : normal_color[2];
		
		if(current_r[i] > dest_r) current_r[i] -= step;
		else if(current_r[i] < dest_r) current_r[i] += step;
			
		if(current_g[i] > dest_g) current_g[i] -= step;
		else if(current_g[i] < dest_g) current_g[i] += step;
			
		if(current_b[i] > dest_b) current_b[i] -= step;
		else if(current_b[i] < dest_b) current_b[i] += step;
		
		document.getElementById("switch" + i).style.color = rgb2hex(current_r[i], current_g[i], current_b[i]);
	}
	return false;
}

function init_switch()
{
	var a_tags = document.getElementsByTagName("a");
	var rel_attribute;
	var n = 0;
	
	for(i = 0; i < a_tags.length; i++)
	{
		var anchor = a_tags[i];
		if(anchor.getAttribute("rel") == "switch")
		{
			hover[n]	 = false;
			current_r[n] = normal_color[0];
			current_g[n] = normal_color[1];
			current_b[n] = normal_color[2];
			
			anchor.setAttribute("id", "switch" + n);
			anchor.onmouseover	= function() { highlight(this.id); return false; }
			anchor.onmouseout 	= function() { downlight(this.id); return false; } 
			n++;
		}	
	}
	setInterval("fade()", 10);
	return false;
}

window.onload = init_switch;

