/*	Link fading script v1.0 by Alex Griffioen @ www.justdesign.nl, 2005
	Feel free to use, but please leave this comment in :)
	Email your improvements to: griffioen@justdesign.nl
*/

var delay		= 1;	// Fade delay in miliseconds
var stepIn		= 10;	// Steps to fade in
var stepOut		= 20;	// Steps to fade out
var aElement	= new Array();
var aInterval	= new Array();
var aLoop		= new Array("R", "G", "B");

function init() {
	var aAnchor = document.getElementsByTagName("a");
	for(var i = 0; i < aColor.length; i++) {
		var className	= aColor[i][0];
		var aRgbOver	= getRgb(aColor[i][1]);
		var aRgbOut		= getRgb(aColor[i][2]);

		for(var j = 0; j < aAnchor.length; j++) {
			if(aAnchor[j].className == className) {
				aAnchor[j].style.color	= aColor[i][2];
				aAnchor[j].setAttribute("linkId", j);
				aAnchor[j].setAttribute("colorOver", aColor[i][1]);
				aAnchor[j].setAttribute("colorOut", aColor[i][2]);
				
				for(var k = 0; k < 3; k++) {
					aAnchor[j].setAttribute("colorOver" + aLoop[k], aRgbOver[aLoop[k]]);
					aAnchor[j].setAttribute("colorOut" + aLoop[k], aRgbOut[aLoop[k]]);
					aAnchor[j].setAttribute("changeIn" + aLoop[k], Math.floor((aRgbOver[aLoop[k]] - aRgbOut[aLoop[k]]) / stepIn));
					aAnchor[j].setAttribute("changeOut" + aLoop[k], Math.floor((aRgbOut[aLoop[k]] - aRgbOver[aLoop[k]]) / stepOut));
				}
				
				aAnchor[j].onmouseover	= linkOver;
				aAnchor[j].onmouseout	= linkOut;
			}
			aInterval.push(null);
		}
	}
}

function getHexFromCssRgb(rgb) {
	var reHex		= new RegExp("^rgb\\(([0-9]{1,3}), ([0-9]{1,3}), ([0-9]{1,3})\\)$", "i");
	var aValue		= reHex.exec(rgb);
	return "#" + getHex(aValue[1]) + getHex(aValue[2]) + getHex(aValue[3]);
}

function getRgb(hex) {
	var reHex		= new RegExp("^#([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})$", "i");
	var aValue		= reHex.exec(hex);
	var aRgb		= new Array(3);
	aRgb["R"]		= parseInt(aValue[1], 16);
	aRgb["G"]		= parseInt(aValue[2], 16);
	aRgb["B"]		= parseInt(aValue[3], 16);
	return aRgb;
}

function getHex(rgbValue) {
	rgbValue		= parseInt(rgbValue);
	var hexValue	= new String("0" + rgbValue.toString(16));
	hexValue		= hexValue.substring(hexValue.length - 2, hexValue.length);
	return hexValue.toUpperCase();
}

function linkOver() {
	var linkId	= this.getAttribute("linkId");
	if(aInterval[linkId] != null) window.clearInterval(aInterval[linkId]);
	aInterval[linkId] = window.setInterval("fade(" + linkId + ", " + this.getAttribute("changeInR") + ", " + this.getAttribute("changeInG") + ", " + this.getAttribute("changeInB") + ")", delay);
}

function linkOut() {
	var linkId	= this.getAttribute("linkId");
	if(aInterval[linkId] != null) window.clearInterval(aInterval[linkId]);
	aInterval[linkId] = window.setInterval("fade(" + linkId + ", " + this.getAttribute("changeOutR") + ", " + this.getAttribute("changeOutG") + ", " + this.getAttribute("changeOutB") + ")", delay);
}

function fade(linkId, changeR, changeG, changeB) {
	var element	= document.getElementsByTagName("a")[linkId];
	var color	= (element.style.color.indexOf("#") == -1) ? getHexFromCssRgb(element.style.color) : element.style.color;
	var aRgb	= getRgb(color);
	var hex		= "#";
		
	for(i = 0; i < 3; i++) {
		var change			= eval("change" + aLoop[i]);
		var newValue		= aRgb[aLoop[i]] + change;
		var colorOverValue	= element.getAttribute("colorOver" + aLoop[i]);
		var colorOutValue	= element.getAttribute("colorOut" + aLoop[i]);
	
		if(change > 0) {
			var toValue		= Math.max(colorOverValue, colorOutValue);
			var newValue	= (newValue > toValue) ? toValue : newValue;
		} else {
			var toValue		= Math.min(colorOverValue, colorOutValue);
			var newValue	= (newValue < toValue) ? toValue : newValue;
		}
		hex += getHex(newValue);
	}
	element.style.color = hex;

	if(hex == element.getAttribute("colorOver") || hex == element.getAttribute("colorOut")) {
		window.clearInterval(aInterval[linkId]);
		aInterval[linkId] = null;
	}
}