/*******************************************************************
*
* File    : AnimatedFader.js
*
* Created : 2000/05/28
*
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com
*	    New features by TEAM Program S.n.c. www.teamprogram.com
*
* Purpose : To create fading text descriptions
*
* History
* Date         Version        Description
*
* 2000-05-28	1.0		Initial version. Based on the State Transition
*					Diagram created for animated rollovers I
*					modified the code to fade text.
* 2000-05-29	1.1		I did not follow the STD correctly and introduced
*					a bug that left objects in the ON state.
*					This is now corrected.
* 2000-07-01	1.2		Added starting and ending color setup capabilities
***********************************************************************/
/*** Create some global variables ***/
var FadingObject = new Array();
var AnimationRunning=false;
var Timer=null;
var FrameInterval=10;
var start_color;

/*******************************************************************************/
/*** These are the simplest HEX/DEC conversion routines I could come up with ***/
/*** I have seen a lot of fade routines that seem to make this a             ***/
/*** very complex task. I am sure somene else must've had this idea          ***/
/*******************************************************************************/
var hexDigit=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function dec2hex(dec)
{
	return(hexDigit[dec>>4]+hexDigit[dec&15]);
}
function hex2dec(hex)
{
	return(parseInt(hex,16))
}
/******************************************************************************************/

/***********************************************************
* Function   : createFaderObject
*
* Parameters : theDiv   - The name of the DIV in which to fade the text
*              numSteps - The number of steps to use in the fade.
*              
* Description : Creates an object that can hold the current
*               state of the fade animation for a particular DIV
***********************************************************/
function createFaderObject(theDiv, numSteps)
{
	this.name		= theDiv;
	this.text		= null;
	this.color		= "FFFFFF";
	this.next_text	= null;
	this.next_color	= null;
	this.state		= "OFF";
	this.index		= 0;
	this.steps		= numSteps;
}

/***********************************************************
* Function   : FadingText
*
* Parameters : theDiv   - The name of the DIV in which to fade the text
*              numSteps - The number of steps to use in the fade.
*              
* Description : Library function to be called from the main HTML.
*		    Creates an object that can hold the current
*               state of the fade animation for a particular DIV
***********************************************************/
function FadingText(theDiv, numSteps)
{
	FadingObject[theDiv] = new createFaderObject(theDiv, numSteps);
}
/*****************************************************************
* Function    : start_fading
*
* Description : If the FadeAnimation loop is not currently running
*		    then it is started.
*****************************************************************/
function start_fading()
{
	if(!AnimationRunning)
		FadeAnimation();
}
/*****************************************************************
*
* Function   : setStartColor
*
* Parameters : 	   color   - The starting color
*
* Description: Set the starting and ending color for the fading
*
*****************************************************************/
function setStartColor(color)
{
	start_color = color;
}
/*****************************************************************
*
* Function   : getColor
*
* Parameters : 	   color   - The finishing color (starting color is 000000)
*		   step_no - The current step number
*		   steps   - The number of steps in the fade.
*
* Description: Calculates the color of the link depending on
*		   how far through the fade we are.
*
*****************************************************************/
function getColor(color, step_no, steps)
{
	var sr=hex2dec(start_color.slice(0,2));
	var sg=hex2dec(start_color.slice(2,4));
	var sb=hex2dec(start_color.slice(4,6));

	var r=hex2dec(color.slice(0,2));
	var g=hex2dec(color.slice(2,4));
	var b=hex2dec(color.slice(4,6));

	r2= Math.floor((step_no*(r-sr))/(steps) + .5 + sr);
	g2= Math.floor((step_no*(g-sg))/(steps) + .5 + sg);
	b2= Math.floor((step_no*(b-sb))/(steps) + .5 + sb);

	return("#" + dec2hex(r2) + dec2hex(g2) + dec2hex(b2));
}
/*****************************************************************
*
* Function   : setColor
*
* Parameters : fadeObj   - The TextFader object to set
*
* Description: Gets the color of the text and writes it to
*		   the DIV.
*
*****************************************************************/
function setColor(fadeObj)
{

	var theColor=getColor(fadeObj.color, fadeObj.index, fadeObj.steps);
	var str="<FONT COLOR="+ theColor + ">" + fadeObj.text + "</FONT>";
	var theDiv=fadeObj.name;

	if(document.all)
	{
		document.all[theDiv].innerHTML=str;
	}
	else if(document.layers)
	{
		document.layers[theDiv].document.write(str);
		document.layers[theDiv].document.close();
	}
}
/*****************************************************************
*
* Function   : fade_up
*
* Parameters : theDiv  - The div in which to display the text
*		   newText - The text to display when faded in
*		   newColor- The color the text will be.
*
* Description: Depending on the current "state" of the fade
*		   this function will determine the new state and
*		   if neccessary, start the fade effect.            
*
*****************************************************************/
function fade_up(theDiv, newText, newColor)
{
	var f=FadingObject[theDiv];

	if(newColor == null)
		newColor="FFFFFF";

	if(f.state == "OFF")
	{
		f.text  = newText;
		f.color = newColor;
		f.state = "FADE_UP";
		start_fading();
	}
	else if( f.state == "FADE_UP_DOWN"
		|| f.state == "FADE_DOWN"
		|| f.state == "FADE_DOWN_UP")
	{
		if(newText == f.text)
			f.state = "FADE_UP";
		else
		{
			f.next_text  = newText;
			f.next_color = newColor;
			f.state      = "FADE_DOWN_UP";
		}
	}
}
/*****************************************************************
*
* Function   : fade_down
*
* Parameters : theDiv  - The div in which to display the text
*
* Description: Depending on the current "state" of the fade
*		   this function will determine the new state and
*		   if neccessary, start the fade effect.            
*
*****************************************************************/
function fade_down(theDiv)
{
	var f=FadingObject[theDiv];

	if(f.state=="ON")
	{
		f.state="FADE_DOWN";
		start_fading();
	}
	else if(f.state=="FADE_DOWN_UP")
	{
		f.state="FADE_DOWN";
		f.next_text = null;
	}
	else if(f.state == "FADE_UP")
	{
		f.state="FADE_UP_DOWN";
	}
}
/*******************************************************************
*
* Function    : Animate
*
* Description : This function is based on the Animate function
*		    of animate.js (animated rollovers).
*		    Each fade object has a state. This function
*		    modifies each object and changes its state.
*****************************************************************/
function FadeAnimation()
{
	AnimationRunning = false;
	for (var d in FadingObject)
	{
		var f=FadingObject[d];

		if(f.state == "FADE_UP")
		{
			if(f.index < f.steps)
				f.index++;
			else
				f.index = f.steps;
			setColor(f);

			if(f.index == f.steps)
				f.state="ON";
			else
				AnimationRunning = true;
		}
		else if(f.state == "FADE_UP_DOWN")
		{
			if(f.index < f.steps)
				f.index++;
			else
				f.index = f.steps;
			setColor(f);

			if(f.index == f.steps)
				f.state="FADE_DOWN";
			AnimationRunning = true;
		}
		else if(f.state == "FADE_DOWN")
		{
			if(f.index > 0)
				f.index--;
			else
				f.index = 0;
			setColor(f);

			if(f.index == 0)
				f.state="OFF";
			else
				AnimationRunning = true;
		}
		else if(f.state == "FADE_DOWN_UP")
		{
			if(f.index > 0)
				f.index--;
			else
				f.index = 0;
			setColor(f);

			if(f.index == 0)
			{
				f.text      = f.next_text;
				f.color     = f.next_color;
				f.next_text = null;
				f.state     ="FADE_UP";
			}
			AnimationRunning = true;
		}
	}
	/*** Check to see if we need to animate any more frames. ***/
	if(AnimationRunning)
		setTimeout("FadeAnimation()", FrameInterval);

}
