//===================================================================================================================================
//
// NAME:	TEXTFADER OBJECT
// VERSION:	1.0
// AUTHOR:	Keith Salisbury, GlobalBeach 2004
//
// DESCRIPTION: Flexible amd Easy to create fading text items. Simply add the onmouseover event and it will do the rest.
//
// REQUIRES:	Fader.js - This class extends the main Fader class.
//
// USAGE:	onmouseover="if(!this.fader){this.fader=new imageFader(this,rolloverColour,steps[,timeoutCount])}"
//
//		this			[object]	the object that contains the instance of imageFader
//		rolloverColour		String		hexidecimal colour string for the colour of the rollover
//		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.fader){this.fader=new textFader(this,'336666',10)}">
//			TEXT GOES HERE
//		</a>
//
// NOTES:	Assign the anchor to a class to define custom colours, and other standard css properties
//		It will NOT work without style="color:#xxxxxx", or a being assigned a class with color defined
// 		Using class="myGroup" on the anchor tag will also enable groupings for click state
//		Currently the anchor tag only supports the firstChild, ie, you cant have more than text within a link
//
// SUPPORT:	Win IE 6, Win Netscape 7.1 (testing is in progress)
//
//===================================================================================================================================

if (typeof FaderClsLoaded=="undefined") alert("Error: please include the file Fader.js")

textFader = function(target,hexTo,steps,fadeSpeed)
{
	//alert("textFader:constructor");

	// parameters
	this.target = target;
	this.steps = steps;
	this.fadeSpeed = fadeSpeed;

	// Call superclass init
	this.init(this.target,this.steps,this.fadeSpeed);

	// subclass specific properties
	this.hexTo = (hexTo) ? hexTo : "ffffff";
	
	// Get the current colour values
	this.from = new Array(3);
	this.colour = (is_ie) ? ((this.target.style.color!="") ? this.target.style.color : this.target.currentStyle.color) : document.defaultView.getComputedStyle(this.target, "").getPropertyValue("color") 
	if (this.colour)
	{
		this.from[0] = (is_ie) ? parseInt(this.colour.substring(1,3),16) : parseInt(this.colour.substring(4,this.colour.length-1).split(',')[0]);
		this.from[1] = (is_ie) ? parseInt(this.colour.substring(3,5),16) : parseInt(this.colour.substring(4,this.colour.length-1).split(',')[1]);
		this.from[2] = (is_ie) ? parseInt(this.colour.substring(5,7),16) : parseInt(this.colour.substring(4,this.colour.length-1).split(',')[2]);
	}
	
	// Get the target colour values
	this.to = new Array(3);
	this.to[0] = parseInt(this.hexTo.substring(0,2),16);
	this.to[1] = parseInt(this.hexTo.substring(2,4),16);
	this.to[2] = parseInt(this.hexTo.substring(4,6),16);

	// Calculate the increments
	this.inc = new Array(3);
	this.inc[0] = (this.from[0]-this.to[0])/this.steps;
	this.inc[1] = (this.from[1]-this.to[1])/this.steps;
	this.inc[2] = (this.from[2]-this.to[2])/this.steps;

	// Calculate the step values
	this.colours = new Array (this.steps)
	this.val = new Array(3);
	for (var i=0;i<=this.steps;i++)
	{
		this.val[0] = (this.from[0]<this.to[0]) ? this.from[0]+(this.inc[0]*i)*-1 : this.from[0]-(this.inc[0]*i);
		this.val[1] = (this.from[1]<this.to[1]) ? this.from[1]+(this.inc[1]*i)*-1 : this.from[1]-(this.inc[1]*i);
		this.val[2] = (this.from[2]<this.to[2]) ? this.from[2]+(this.inc[2]*i)*-1 : this.from[2]-(this.inc[2]*i);
		this.val[0] = parseInt(this.val[0]);
		this.val[1] = parseInt(this.val[1]);
		this.val[2] = parseInt(this.val[2]);
		this.colours[i] = (is_ie) ? this.dec2hex(this.val[0]) + this.dec2hex(this.val[1]) + this.dec2hex(this.val[2]) : this.val[0] + "," + this.val[1] + "," + this.val[2];
	}
	
	// Destory unnecessary things
	this.from = null;
	this.to = null
	this.inc = null;
	this.val = null;
	this.colour = null;
	
}

textFader.prototype = new Fader();

textFader.prototype.action = function()
{
	this.colour = (is_ie) ? "#" + this.colours[this.stepCount] : "rgb(" + this.colours[this.stepCount] + ")";
	if (this.colour!=null){
		this.target.style.color = this.colour;
	}
}

textFader.prototype.dec2hex = function(fig)
{
//	alert("textFader:dec2hex");
	var divFig=Math.floor(fig/16)
	var modFig=Math.floor(fig % 16)
	var divFig=this.hexify(divFig)
	var modFig=this.hexify(modFig)
	return divFig+modFig
}

textFader.prototype.hexify = function (fig)
{
//	alert("textFader:hexify");
	if (fig==15) return "f"
	if (fig==14) return "e"
	if (fig==13) return "d"
	if (fig==12) return "c"
	if (fig==11) return "b"
	if (fig==10) return "a"
	if (fig<10) return ""+fig // use "" first to convert number to string
}

