//===================================================================================================================================
//
// NAME:	kCOLORFADER OBJECT
// VERSION:	1.2
// AUTHOR:	Keith Salisbury, GlobalBeach 2004
//
// DESCRIPTION: Flexible amd Easy to create color fading a specific property of an item. Simply add the onmouseover event and it
//				will do the rest.
//
// REQUIRES:	kFader.js - This class extends the main kFader class.
//
// USAGE:		onmouseover="if(!this.kFader){this.kFader=new kColorFader(this,rolloverColour,property,steps[,timeoutCount])}"
//
//				this				[object]	the object that contains the instance of imagekFader
//				rolloverColour		String		hexidecimal colour string for the colour of the rollover
//				property			String		style property that will be affected by the colour fade
//				steps				Number		number of steps to use for the fade
//				[timeoutCount]		Number		Time in milliseconds between each step of the fade (Optional)
//
// EXAMPLE:		<a href=# onMouseOver="if(!this.kFader){this.kFader=new kColorFader(this,'336666','color',10)}">
//				TEXT GOES HERE
//				</a>
//
// SUPPORT:		Win IE 5+, Netscape 7.1, Mozilla FireFox 1.0, Safari 1.2
//
// HOSTORY:
//			1.0 - Core Code
//			1.1 - Math.floor the values so that dec2hex function always works
//			1.2 - corrected get style syntax for Safari 1.2 Mac
//
//===================================================================================================================================

if (typeof kFaderClsLoaded=="undefined"){
	alert("core kFader class required");
}

kColorFader = function(target,hexTo,property,steps,fadeSpeed)
{
	//alert("kColorFader:constructor");
	this.id = "kColorFader";
	// parameters
	this.target = target;
	this.steps = steps;
	this.fadeSpeed = fadeSpeed;

	// Call superclass init
	this.init(this.target,this.steps,this.fadeSpeed);

	// get the property values
	this.propertyArray=new Array();
	(typeof(property)=="string") ? this.propertyArray[0] = property : this.propertyArray = property;

	// get the colorTo values
	this.hexToArray=new Array();
	(typeof(hexTo)=="string") ? this.hexToArray[0] = hexTo : this.hexToArray = hexTo;

	// constuctor the colour array for each property
	this.coloursArray = new Array();
	for (var i=0;i<this.propertyArray.length;i++)
	{
		// Get the current colour values
		this.coloursArray[i] = this.getColors(this.propertyArray[i], this.hexToArray[i]);
	}
}

kColorFader.prototype = new kFader();

kColorFader.prototype.action = function()
{
	for (var i=0;i<this.coloursArray.length; i++)
	{
		if (this.coloursArray[i][this.stepCount]!="")
		{
			this.target.style[this.propertyArray[i]] = this.coloursArray[i][this.stepCount];
			//this.target.style[this.property] = this.colours[this.stepCount];
			//this.target.innerHTML += "<br>" + this.propertyArray[i] + "=" + this.coloursArray[i][this.stepCount];
		}
	}
}

kColorFader.prototype.getColors = function(property, hexTo)
{
	var colour;
	var from = new Array(3);
	if (is_ie){
		colour = this.target.currentStyle[property];
	} else {
		colour = (this.target.style[property]) ? this.target.style[property] : (document.defaultView.getComputedStyle) ? document.defaultView.getComputedStyle(this.target, "").getPropertyValue(property) : document.styleSheets[0].cssRules[1].style[property].toLowerCase();
	}

	if (colour)
	{
		// are we using hex or rgb?
		var isHex = (colour.substring(0,1)=="#") ? true : false;
		from[0] = (isHex) ? parseInt(colour.substring(1,3),16) : parseInt(colour.substring(4,colour.length-1).split(',')[0]);
		from[1] = (isHex) ? parseInt(colour.substring(3,5),16) : parseInt(colour.substring(4,colour.length-1).split(',')[1]);
		from[2] = (isHex) ? parseInt(colour.substring(5,7),16) : parseInt(colour.substring(4,colour.length-1).split(',')[2]);
	}
	// Get the target colour values
	var to = new Array(3);
	to = [parseInt(hexTo.substring(0,2),16),parseInt(hexTo.substring(2,4),16),parseInt(hexTo.substring(4,6),16)];

	// Calculate the increments
	var inc = new Array(3);
	inc = [(from[0]-to[0])/this.steps,(from[1]-to[1])/this.steps,(from[2]-to[2])/this.steps];

	// Calculate the step values
	var colours = new Array (this.steps)

	var val = new Array(3);
	for (var i=0;i<=this.steps;i++)
	{
		val = [Math.floor(from[0]-(inc[0]*i)), Math.floor(from[1]-(inc[1]*i)), Math.floor(from[2]-(inc[2]*i))];
		colours[i] = (isHex) ? "#" + this.dec2hex(val[0]) + this.dec2hex(val[1]) + this.dec2hex(val[2]) : "rgb(" + val[0] + "," + val[1] + "," + val[2] + ")";
	}

	return colours;
}

kColorFader.prototype.hexChars = "0123456789ABCDEF";

kColorFader.prototype.dec2hex = function(Dec)
{
	return '' + this.hexChars.charAt((Dec - (Dec%16))/16) + this.hexChars.charAt(Dec % 16) 
}
