/**
 * link fader script
 * (c) 2004 jeff emminger
 *
 * http://www.jeffemminger.com/
 */

function lf_fadeIn(id) {
	var el = document.getElementById(id);
	var color = lf_getRGBColor(el);

	if (!self[id + "_start"]) self[id + "_start"] = color;

	window.clearTimeout(self["TIMER_OUT_" + id]);

	if (color[0] != LF_TARGET_COLOR[0]
		|| color[1] != LF_TARGET_COLOR[1]
		|| color[2] != LF_TARGET_COLOR[2]) {
		lf_stepColors(el, color, LF_TARGET_COLOR);
		self["TIMER_IN_" + id] = window.setTimeout("lf_fadeIn('" + id + "');", LF_DELAY);
	}
}

function lf_fadeOut(id) {
	var el = document.getElementById(id);
	var color = lf_getRGBColor(el);
	var original = self[id + "_start"];//alert(original)

	window.clearTimeout(self["TIMER_IN_" + id]);

	if (color[0] != original[0]
		|| color[1] != original[1]
		|| color[2] != original[2]) {
		lf_stepColors(el, color, original);
		self["TIMER_OUT_" + id] = window.setTimeout("lf_fadeOut('" + id + "');", LF_DELAY);
	}
}

function lf_getRGBColor(el) {
	var color;

	if (window.getComputedStyle)
		color =  window.getComputedStyle(el, null).getPropertyValue("color");
	else
		color = el.currentStyle.color;

	return lf_getRGBFromHex(color);
}

function lf_getRGBFromHex(color) {
	var array;
	
	if (/rgb/.test(color)) {
		array = color.replace(/[(rgb)\(\)\s]/g, "").split(",");
	}
	else {
		color = color.replace("#", "");
		
		if (color.length == 6) {
			array = [parseInt(color.substring(0,2), 16)
				, parseInt(color.substring(2,4), 16)
				, parseInt(color.substring(4), 16)];
		}
		else if (color.length == 3) {
			array = [parseInt(color.substring(0,1), 16)
				, parseInt(color.substring(1,2), 16)
				, parseInt(color.substring(2), 16)];
		}
		else {
			alert("LinkFader.lf_faderInit() error:\n\nInvalid hex color \"" + color + "\", defaulting to [255,0,0]");
			array = [255,0,0];
		}
	}

	return array;
}

function lf_stepColors(el, from, to) {
	for (var x = 0; x < from.length; x++) {
		if (from[x] > to[x]) {
			if (from[x] - to[x] > LF_STEP)
				from[x] -= LF_STEP;
			else 
				from[x] -= 1;
		}
		else if (from[x] < to[x]) {
			if (to[x] - from[x] > LF_STEP)
				from[x] = parseInt(from[x], 10) + parseInt(LF_STEP, 10);
			else 
				from[x] = parseInt(from[x], 10) + 1;
		}
	}
	el.style.color = "rgb(" + from[0] + "," + from[1] + "," + from[2] + ")";
}

function lf_faderInit(targetColor, step, delay, attachAll) {
	//  set up defaults if none given
	self.LF_TARGET_COLOR =
		(targetColor && targetColor.constructor == Array)
		? targetColor
			: (targetColor && targetColor.constructor == String)
			? lf_getRGBFromHex(targetColor) 
				: [255,0,0];
	self.LF_STEP =
		(step && step.constructor == Number)
		? step : 15;
	self.LF_DELAY =
		(delay && delay.constructor == Number)
		? delay : 10;
	
	//  attach to all links with "link_fader" className
	var links = document.links;
	self.LF_ID_COUNTER = 0;

	for (var x = 0; x < links.length; x++) {
		//  attach to fader class hyperlink, or all if attachAll == true
		if (attachAll || /\blink_fader\b/.test(links[x].className)) {
			//  if no id, assign one
			if (links[x].getAttribute("id") == null
					|| links[x].getAttribute("id") == "") {
				links[x].setAttribute("id", "link_fader_" + LF_ID_COUNTER++);
			}
			links[x].onmouseover = function(){ lf_fadeIn(this.id); };
			links[x].onmouseout = function() { lf_fadeOut(this.id); };
		}
	}
}

window.onload = function() { lf_faderInit( [0,0,200] ); };
