/*******************************************************************
* File    : AnimatedFader.js
* Created : 2000/05/28
* -Modified : 2000/07/22
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com
* -Adapted : Nuno m. s. Nunes (webmaster@artedesign-net.pt) (adaptado e melhorado)
* Purpose : To create fading text descriptions
* -improvements/changes: Cleans the div text when it fades down,
*	improved to fade onClick not onMouseOver/Mouseout
*	I took off most of the comments, sorry Roy but I had to optimize the file size
********************************************************************/


/*** Create some global variables ***/
var FadingObject = new Array();
var FadeRunning=false;
var FadeInterval=30;

/**/
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(theDiv, numSteps, startColor)
{
	if(!startColor)
		startColor = "000000";
		
	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;
	this.r		= hex2dec(startColor.slice(0,2));
	this.g		= hex2dec(startColor.slice(2,4));
	this.b		= hex2dec(startColor.slice(4,6));
}

/**/
function FadingText(theDiv, numSteps, startColor)
{
	FadingObject[theDiv] = new createFaderObject(theDiv, numSteps, startColor);
}
/**/
function start_fading()
{
	if(!FadeRunning)
		FadeAnimation();
}
/**/
function getColor(f)
{
	var r=hex2dec(f.color.slice(0,2));
	var g=hex2dec(f.color.slice(2,4));
	var b=hex2dec(f.color.slice(4,6));

	r2= Math.floor(f.r+(f.index*(r-f.r))/(f.steps) + .5);
	g2= Math.floor(f.g+(f.index*(g-f.g))/(f.steps) + .5);
	b2= Math.floor(f.b+(f.index*(b-f.b))/(f.steps) + .5);

	return("#" + dec2hex(r2) + dec2hex(g2) + dec2hex(b2));
}
/**/
function setColor(fadeObj)
{

	var theColor=getColor(fadeObj);
	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(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(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 FadeAnimation()
{
	FadeRunning = 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
				FadeRunning = 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";
			FadeRunning = true;
		}
		else if(f.state == "FADE_DOWN")
		{
			if(f.index > 0)
				f.index--;
			else
				f.index = 0;
			setColor(f);

			if(f.index == 0){
				tira_texto(f.name); // linha
				f.state="OFF";
				}
			else
				FadeRunning = 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";
			}
			FadeRunning = true;
		}
	}
	/*** Check to see if we need to animate any more frames. ***/
	if(FadeRunning)
		setTimeout("FadeAnimation()", FadeInterval);

}

function tira_texto(theDiv){

	if(document.all)
	{
		document.all[theDiv].innerHTML=" ";
	}
	else if(document.layers)
	{
		document.layers[theDiv].document.write(" ");
		document.layers[theDiv].document.close();
	}
}
