var defaultColor = 'dddddd';				// Standart color for all links
var startColor='a30502';					// Start color for fading effect	
var endColor='ffffff';						// End color for fading effect
var fadeBack = true;							// enables back fading on mouseout
var colorStep = '16';						// decimal values only 1 - 255
var startFade = new Object();
var endFade=new Object();
var fadeSwitch = 1;							// Enables raising or decreasing of the color
var stopColors = 3;							// Flags how many colors must be equal to end the fading
var fadeMSeconds = 25;						// Sets the number of seconds between each fade call


// Global storages for colors and fading call for every object linked with the element id
fadeColors = new Object();				
fadeCalls = new Object();

// Transform current hex colors to arrays of decimal values 
startFade = decColorObject(startColor);
endFade= decColorObject(endColor);
linkColor = decColorObject(defaultColor);

// Enable decreasing of color if start color is higher than
// the end color

if (startFade > endFade)
	fadeSwitch = -1;						


function decColorObject(color) {
		
		var rgb = new Array('red','green','blue');	
		
		// Split color in parts with two hex values
		color = color.toUpperCase();
		
		var colorArr = color.match(/[A-Fa-f0-9]{1,2}/gi);
	
		var binColorObject = new Object();	
		if (colorArr.length > 0) {	
		
			// Calculate color
		
			for (var i=0; i < colorArr.length; i++) {
				binColorObject[rgb[i]] = transformHexToBin(colorArr[i]);
			}
		}		
		return binColorObject;
}		

function transformHexToBin(hex) {
	var charSub = 55; 	// Subtraction from character char code to get the right number
	var factor = 16;

	// Proceed only if value is not empty
	if (hex != '') {
		var charVal = hex.charCodeAt(hex.length-1);
	        var character = hex.substr(hex.length-1,1);
	        
	    // Check if the charcode is a character 
		if (charVal >=65) {
			charVal -= charSub;
			} else {
					charVal = Number(hex.substr(hex.length-1,1));
				   }
				   
		// Get the rest of the submitted hex string and transform it again if necessary
		
		var restHex = hex.substring(0,hex.length-1);
		var restVal = 0;
		
		if (restHex.length > 0) {
			// Make a recursive call to get the next hex
			var restVal = transformHexToBin(restHex);
			restVal *= factor;
			}
			
		var totalSum = charVal+restVal;
		}
		
	return totalSum;
}

function startFading(link) {
	
	var linkId = link.id;
		
	// setup start color
	var sColor = 'rgb('+startFade['red']+','+startFade['green']+','+startFade['blue']+')';
	
	link.style.color = sColor;
	
	fadeCalls[linkId] = new Object();
	fadeCalls[linkId]['currColors'] = startFade;
	fadeCalls[linkId]['mode'] = 1;
	fadeElement(linkId);
	
	// setup fading 
	//fadeCalls[linkId] = new Object();
	//fadeCalls[linkId]['interval'] = window.setInterval('fade("'+linkId+'")', fadeMSeconds);
	//fadeCalls[linkId]['mode'] = 1;
	//fadeCalls[linkId]['currColors'] = startFade;
	}
	
function endFading(link) {
	if (fadeBack)
    	fadeCalls[linkId]['mode'] = -1;
    }


function fade(linkId) {
	var currColors = fadeCalls[linkId]['currColors'];
	var fadeMode = fadeCalls[linkId]['mode'];
	var equalColors = 0;
	var currLink = document.getElementById(linkId);
	
	for (var color in currColors) {
		currColors[color] = currColors[color]+(colorStep * (fadeSwitch * fadeMode));
		
		if (fadeMode == 1) {
			// we fade up so we have to check if current color matches an end color
			if ((fadeSwitch == 1) && (currColors[color] >= endFade[color])) {
				currColors[color] = endFade[color];
				equalColors ++;
			} 
			
			if ((fadeSwitch == -1) && (currColors[color] <= endFade[color])) {
				currColors[color] = endFade[color];
				equalColors ++;
			}			
		} else {
			// we fade down so we have to check if current color matches the standart link color
			if ((fadeSwitch == 1) && (currColors[color] >= linkColor[color])) {
				currColors[color] = linkColor[color];
				equalColors ++;
			} 
			
			if ((fadeSwitch == -1) && (currColors[color] <= linkColor[color])) {
				currColors[color] = linkColor[color];
				equalColors ++;
			}
		}
	}
	
	if (equalColors == stopColors)
		stopFade(linkId);
	else
		fadeCalls[linkId]['currColors'] = currColors;	
	
	var fadeColor = 'rgb('+currColors['red']+','+currColors['green']+','+currColors['blue']+')';	
	currLink.style.color = fadeColor;	 
		
	return;
}

function fadeElement(linkId) {

	var equalColors = 0;
	var currLink = document.getElementById(linkId);
	
	while(equalColors != stopColors) {
		var currColors = fadeCalls[linkId]['currColors'];
		var fadeMode = fadeCalls[linkId]['mode'];
	
		for (var colName in currColors) {
		
alert("colName: "+colName);
		
			currColors[colName] += (colorStep * (fadeSwitch * fadeMode));
			
			if (fadeMode == 1) {
				// we fade up so we have to check if current color matches an end color
				if ((fadeSwitch == 1) && (Number(currColors[colName]) >= Number(endFade[colName]))) {
					currColors[colName] = endFade[colName];
					equalColors ++;
				} 
				
				if ((fadeSwitch == -1) && (Number(currColors[colName]) <= Number(endFade[colName]))) {
					currColors[colName] = endFade[colName];
					equalColors ++;
				}			
			} else {
				// we fade down so we have to check if current color matches the standart link color
				if ((fadeSwitch == 1) && (Number(currColors[colName]) >= Number(linkColor[colName]))) {
					currColors[colName] = linkColor[colName];
					equalColors ++;
				} 
				
				if ((fadeSwitch == -1) && (Number(currColors[colName]) <= Number(linkColor[colName]))) {
					currColors[colName] = linkColor[colName];
					equalColors ++;
				}
			}
		}
	var fadeColor = 'rgb('+currColors['red']+','+currColors['green']+','+currColors['blue']+')';	
	currLink.style.color = fadeColor;
	
	fadeCalls[linkId]['currColors'] = currColors;			
	}
	
	var fadeColor = 'rgb('+currColors['red']+','+currColors['green']+','+currColors['blue']+')';
	
	alert(fadeColor);
		
	currLink.style.color = fadeColor;
	
	return;
}	



	
function stopFade(linkId) {
		window.clearInterval(fadeCalls[linkId]['interval']);
		//delete fadeCalls[linkId];	    
	    return true;	
}
