/* © Ambacie, 2007
Rdc 0.0.3
Thierry Mang, Ludovic Laly
contact@ambacie-referencement.com

Cette classe est la propriété d'Ambacie. Toute utilisation, reproduction,
modification, communication ou représentation intégrale ou partielle
du présent document qui n’a pas été préalablement autorisée par écrit
est formellement interdite.
Une telle utilisation, reproduction, modification, communication ou représentation
non autorisée, par quelque moyen que ce soit, constituerait une contrefaçon
sanctionnée par la loi aux plans pénal et civil et, d'une manière générale,
une atteinte aux droits d'Ambacie.

Le fait que vous puissiez accéder à cet en-tête signifie que vous avez 
pris connaissance de la licence et que vous en avez accepté les
termes.
*/
function Rdc( blockSrc, colorStart, colorEnd, pause, blockDest )
{
	if( !document.getElementById( blockSrc ) ) { return false; }

	this.stopTimer			= false;
	this.blockSrc			= document.getElementById( blockSrc );
	this.pause				= ( pause && pause != ""  ) ? pause : 4000 ;
	this.colorStart			= ( colorStart && colorStart != "" ) ? colorStart : "#ffffff" ;
	this.colorEnd			= ( colorEnd && colorEnd != "" ) ? colorEnd : "#000000" ;
	this.blockDest			= ( blockDest && blockDest != "" ) ? document.getElementById( blockDest ) : this.blockSrc ;

	this.texts				= this.parser( this.blockSrc ).split( "null" );
	this._removeNode( this.blockSrc );
	this.timer("this.controler()", this.pause);
};

Rdc.prototype.controler = function()
{
	this._removeNode( this.blockDest );
	this.blockDest.appendChild( this._createNode( this.getText() ) );
	this.initFader(this.colorStart, this.colorEnd);
	this.timer('this.fader()', 50);
};

Rdc.prototype.timer = function(command, t)
{
	eval(command);
	if( !this.stopTimer ) {
		tm = this;
		window.setTimeout('tm.timer("' + command + '", ' + t + ')', t);
	} else {
		this.stopTimer = false;
	}
}

Rdc.prototype.parser = function(node)
{
	var n = node.childNodes.length;
	var str = "";
	if ( n == 0 ) { return node.nodeValue; }
	else {
		for (var i = 0; i < n; i++) { str += this.parser( node.childNodes[i] ); }
		return str;
	}
}

Rdc.prototype.initFader = function(c1, c2)
{
	this.divresultfrom = new Array(this.hexToDec(c1.substr(1,2)), this.hexToDec(c1.substr(3,2)), this.hexToDec(c1.substr(5,2)));
	this.stepRVB = new Array(
		((this.hexToDec(c2.substr(1,2))-this.divresultfrom[0])/20),
		((this.hexToDec(c2.substr(3,2))-this.divresultfrom[1])/20),
		((this.hexToDec(c2.substr(5,2))-this.divresultfrom[2])/20)
	);
	this.steps = 0;
};
	
Rdc.prototype.fader = function()
{
	this.blockDest.childNodes[0].style.color = "rgb(" + parseInt(this.divresultfrom[0]+(this.steps*this.stepRVB[0])) +","+ parseInt(this.divresultfrom[1]+(this.steps*this.stepRVB[1])) +","+ parseInt(this.divresultfrom[2]+(this.steps*this.stepRVB[2])) +")";	
	this.steps++;
	if( this.steps >= 20 ) { this.stopTimer = true; }
};

Rdc.prototype.getText = function()
{
	this.texts.push(this.texts.shift());
	return this.texts[0];
};

Rdc.prototype._createNode = function(string)
{
	elm = document.createElement( "span" );
	str = document.createTextNode( string );
	elm.style.color = this.colorStart;
	elm.appendChild( str );
	return elm;
};

Rdc.prototype._removeNode = function(node)
{
	while( node.childNodes.length > 0 ) node.removeChild( node.childNodes[0] );
};

Rdc.prototype.hexToDec = function(hex)
{
	return parseInt( hex, 16 );
};

