addEvent(window, 'load', fade_init);
/*
	Color fader script			
*/

var _FADE_NAME = 'fader';
//var _FADE_COLORS = ['F8ECEC', 'ECCFCF', 'DAA0A0', 'C67070', 'B34040'];
var _FADE_COLORS = ['FCF6F6', 'F6E7E7', 'EDD0D0', 'E4B8B8', 'DAA0A0', 'C67070', 'B34040'];
var _FADE_START_DELAY = 25;
var _FADE_STEP_DELAY = 125;
var _FADE_ATTRIBUTE_CHANGE = 'backgroundColor';

var _FADE_CUSTOM_FADERS = new Object;

function Fader(sName, aColors, iStartDelay, iStepDelay, sAttributeChange) {
	this.fadeName = sName ? sName : _FADE_NAME;
	this.fadeColors = aColors ? aColors : _FADE_COLORS;
	this.startDelay = iStartDelay ? iStartDelay : _FADE_START_DELAY;
	this.stepDelay = iStepDelay ? iStepDelay : _FADE_STEP_DELAY;
	this.attributeChange = sAttributeChange ? sAttributeChange : _FADE_ATTRIBUTE_CHANGE;
	
	var me = this;
	this.fadeHash = function(oLink) {
		if (!oLink.hash) {
			var oLink = getTarget(oLink);
		}
		me.fade(oLink.hash.replace(/#/, ''));
	}
}

// Starts up the fading colors thing that can be used to highlight parts of
// a page. Really good stuff. Just be sure to define the _FADE_COLORS variable
// somewhere.

Fader.prototype.fade = function(sId) {
	var thisObj = this;
	setTimeout(function() { thisObj.doFade(thisObj.fadeColors.length, sId, null) }, this.startDelay);
}

Fader.prototype.doFade = function(iCurrIdx, sId, origColor) {

	var oElem = getObject(sId);

	if (iCurrIdx == this.fadeColors.length) {
		eval('origColor = oElem.style.' + this.attributeChange + ';');
	}
	
	if (iCurrIdx > 0) {
		eval('oElem.style.' + this.attributeChange + ' = \'#' + this.fadeColors[iCurrIdx - 1] + '\';');
		var thisObj = this;
		setTimeout(function() { thisObj.doFade(--iCurrIdx, sId, origColor) }, this.stepDelay);
	} else {
		eval('oElem.style.' + this.attributeChange + ' = origColor;');
	}
}

function fade_config() {
	// Can use this to set up custom faders by calling in a 'script' tag
	// Normally, though, the fader script would work from the default values
	// Can pass instances of the fader to use, which would then be triggered
	// by different 'class' attributes in the tags.
	
	if (arguments.length > 0) {
		for (var i = 0; i < arguments.length; i++) {
			_FADE_CUSTOM_FADERS[arguments[i].fadeName] = arguments[i];
		}
	}
}

function fade_init() {
	if (!document.getElementsByTagName) return;
	
	var oFade = new Fader();
	var oLinks = document.getElementsByTagName('a');
	for (var i = 0; i < oLinks.length; i++) {
		var oLink = oLinks[i];
		if (oLink.hash && 
			((oLink.pathname == location.pathname) || ('/' + oLink.pathname == location.pathname)) &&
			oLink.search == location.search) 
		{
			// Check to see if this link should be ignored
			var reIgnoreFade = /fade-no/;
			var aMatch = reIgnoreFade.exec(oLink.className);
			if (aMatch == null) {
				// Check to see if the link has a custom class that indicates a unique fader.
				var reCustomFade = /fade-cust-(\w+)/;
				var aMatch = reCustomFade.exec(oLink.className);
				if (aMatch != null && _FADE_CUSTOM_FADERS[aMatch[1]]) {
					addEvent(oLink, 'click', _FADE_CUSTOM_FADERS[aMatch[1]].fadeHash);
				} else {
					addEvent(oLink, 'click', oFade.fadeHash);
				}
			}
		}
	}
}