/*****************************************************************************/
/* JavaScript - Includefile                                                  */
/*                                                                           */
/* Author: Sebastian Gingter, sebastian@gingter.org                          */
/* Copyright 2003 by Sebastian Gingter                                       */
/*                                                                           */
/* This script can be included into a webpage to make the hyperlinks fade    */
/* in an out. The colors are taken out of the css, so be sure to have one :) */
/*                                                                           */
/* This copyright note must be kept in place if you plan to use this script  */
/*****************************************************************************/

// read color value from css style
	function getCSSValue(style,prop)
	{
		var rules;
		var rulesLength;
		var docstyle=document.styleSheets.item(0);

		if(docstyle.rules)
			rules = docstyle.rules;		// IE
		if(docstyle.cssRules)
			rules=docstyle.cssRules;	// Mozilla

		rulesLength = rules.length;
		for(var i = 0; i < rulesLength; i++)
			if(rules.item(i).selectorText.toLowerCase() == style.toLowerCase()){
				return rules.item(i).style[prop];
			}
	}

// fading Script
	// set configuration variables
	startColor = "#000000";		// MouseOut link color 
	endColor = "#000000";		// MouseOver link color 
	actStepColor = "#000000"	// actual fading color
	steps = 25;			// delay when fading
	actStep = 0;			// actual stepping value
	autoFade = true;		//  set to true or false; true will cause all links to fade automatically 
	sloppyClass = false;		// set to true or false; true will cause all CSS classes with "fade" in them to fade onmouseover 

	// fill array with color values
	hexa = new makearray(16); 
	for(var i = 0; i < 10; i++)   // values 1 to 10
		hexa[i] = i; 
	hexa[10]="a"; hexa[11]="b"; hexa[12]="c";  // values a to f
	hexa[13]="d"; hexa[14]="e"; hexa[15]="f"; 

	// capture the document events
	document.onmouseover = domouseover; 
	document.onmouseout = domouseout; 

	// convert hex color values to numbers
	startColor = dehexize(startColor.toLowerCase()); 
	endColor = dehexize(endColor.toLowerCase()); 
	actStepColor = dehexize(actStepColor.toLowerCase()); 

	var fadeId = new Array(); 

	// calculate the numbers from the hex values
	function dehexize(Color){ 
		var colorArr = new makearray(3); 
		for (i=1; i<7; i++){ 
			for (j=0; j<16; j++){ 
				if (Color.charAt(i) == hexa[j]){ 
					if (i%2 !=0) 
						colorArr[Math.floor((i-1)/2)]=eval(j)*16; 
					else 
						colorArr[Math.floor((i-1)/2)]+=eval(j); 
				} 
			}
		} 
		return colorArr; 
	}

	// captured event to fade in
	function domouseover() { 
		if(document.all){ 
			var srcElement = event.srcElement; 
			if ((srcElement.tagName == "A" && autoFade) || srcElement.className == "fade" || (sloppyClass && srcElement.className.indexOf("fade") != -1)){
			    if (srcElement.className.length == 0 )
					prefix = 'a';
				else
					prefix = 'a.';
				startColor = dehexize( getCSSValue(prefix + srcElement.className, 'color') );
				endColor = dehexize ( getCSSValue(prefix + srcElement.className +':hover', 'color') );
				fade( startColor, endColor ,srcElement.uniqueID, steps);
			}
		} 
	} 

	// captured event to fade out
	function domouseout() { 
		if (document.all){ 
			var srcElement = event.srcElement;
			if ((srcElement.tagName == "A" && autoFade) ||
			   srcElement.className == "fade" ||
			   (sloppyClass && srcElement.className.indexOf("fade") != -1)){
			    if (srcElement.className.length == 0 )
					prefix = 'a';
				else
					prefix = 'a.';
				endColor = dehexize ( getCSSValue(prefix + srcElement.className, 'color') );
				fade(actStepColor, endColor, srcElement.uniqueID, actStep);
			}
		}
	}

	// create an initialised array of length n
	function makearray(n) {
		this.length = n;
		for(var i = 1; i <= n; i++)
			this[i] = 0;
		return this;
	}

	// return a hex value from a number
	function hex(i) {
		if (i < 0)
			return "00";
		else if (i > 255)
			return "ff";
		else
			return "" + hexa[Math.floor(i/16)] + hexa[i%16];
	}
	
	// set color value of given element
	function setColor(r, g, b, element) {
		var hr = hex(r);
		var hg = hex(g);
		var hb = hex(b);
		element.style.color = "#"+hr+hg+hb;
		actStepColor = dehexize(element.style.color);	// store actual fading color
	}
	
	// fade given element from start to end color within given steps
	function fade(s,e, element, step){
		var sr = s[0]; var sg = s[1]; var sb = s[2]; // start color
		var er = e[0]; var eg = e[1]; var eb = e[2]; // end color

		if (fadeId[0] != null && fadeId[0] == element){
			// left element is still fading in, so stop timeouts:
			var i = 0;
			while(i < fadeId.length){
				clearTimeout(fadeId[i]);
				i++;
			}
		}

		// set timeouts for every step of the fade
		for(var i = 0; i <= step; i++) {
			fadeId[i+1] = setTimeout("setColor("+
			"Math.floor("+sr+"*(("+step+"-"+i+")/"+step+")+"+er+"*("+i+"/"+step+")),"+	// red values
			"Math.floor("+sg+"*(("+step+"-"+i+")/"+step+")+"+eg+"*("+i+"/"+step+")),"+	// green values
			"Math.floor("+sb+"*(("+step+"-"+i+")/"+step+")+"+eb+"*("+i+"/"+step+")),"+	// blue values
			element+");",		// element to fade
			i*step);		// milliseconds to step i
			
			actStep = i + 1; 	// store done steps (used by fade out)
		} 
		fadeId[0] = element;		// store faded element
	}
