var isDisplayed = false;
var isAction = false;


function fadeObject (idElem, speed) {
	this.idElem = idElem;
	this.speed = speed;
	this.callBack = null;
	
	this.fadeOut = function (Callback) {
		
		if (isAction)
			return;
			
		isAction = true;
		_fadeOut (this.idElem, document.getElementById (this.idElem).style.opacity*10, this.speed, this.onFadeOutComplete, Callback)
	}
	
	this.fadeIn = function () {
		if (isAction)
			return;
			
		isAction = true;
		document.getElementById (this.idElem).style.display = 'block';
		_fadeIn (this.idElem, 0, this.speed, this.onFadeInComplete);
	}
	
	this.onFadeInComplete = function () {
		isDisplayed = true;
		isAction = false;
	}
	
	this.onFadeOutComplete = function (Callback) {
		isDisplayed = false;
		isAction = false;

		if (Callback)
			Callback ();
	}
}

function _fadeIn (idElem, opacity, speed, onFadeInComplete) {
	setOpacity (idElem, opacity);
		
	if (opacity <= 10)
		setTimeout ('_fadeIn(\''+idElem+'\', '+(opacity+1)+', '+speed+', '+onFadeInComplete+')', speed);
	else {
		if (onFadeInComplete)
			onFadeInComplete ();
	}
}
	
function _fadeOut (idElem, opacity, speed, onFadeOutComplete, Callback) {
	
	if (opacity >= 0)
		setTimeout ('_fadeOut(\''+idElem+'\', '+(opacity-1)+', '+speed+', '+onFadeOutComplete+', '+Callback+')', speed);
	else {
		if (onFadeOutComplete)
			onFadeOutComplete (Callback);
	}
	setOpacity (idElem, opacity);
}

function setOpacity (idElem, value) {
	var elem = document.getElementById (idElem);
	elem.style.opacity = value/10;
	elem.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function FadeIn (idElem, opacity, speed) {
	setOpacity (idElem, opacity);
	
	if (opacity <= 10)
		setTimeout ('FadeIn(\''+idElem+'\', '+(opacity+1)+', '+speed+')', speed);
}

function FadeOut (idElem, maxOpacity, opacity, speed, OnComplete) {
	if (opacity >= maxOpacity) {
		setTimeout ('FadeOut(\''+idElem+'\', '+maxOpacity+', '+(opacity-1)+', '+speed+', '+OnComplete+')', speed);
		setOpacity (idElem, opacity);
	}
	else if (OnComplete)
		OnComplete (idElem);
}









	
function h2rvb (hcolor) {
	return Array (h2d (hcolor.substr (0, 2)),
				  h2d (hcolor.substr (2, 2)),
				  h2d (hcolor.substr (4, 2)));
}

function rvb2h (rvb) {return d2h (rvb[0]).toString() + d2h (rvb[1]) + d2h (rvb[2])} 

function addrvb (rvb1, rvb2) {
	return Array (rvb1[0] + rvb2[0],
				  rvb1[1] + rvb2[1],
				  rvb1[2] + rvb2[2]);
}

function diffrvb (rvb1, rvb2) {
	return Array (rvb1[0] - rvb2[0],
				  rvb1[1] - rvb2[1],
				  rvb1[2] - rvb2[2]);
}

function divrvb (rvb, number) {
	return Array (Math.round (rvb[0]/10),
				  Math.round (rvb[1]/10),
				  Math.round (rvb[2]/10));
}

function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}

var listObjectsFading = Array ();

function FadeColor (csstype, idElem, hcolor, hcolorto, speed, diff, _id_fade) {
	// Si premier fade, instancier ce fade
	if (!document.getElementById (idElem).fading) {
		document.getElementById (idElem).fading = _id_fade ? _id_fade : 1;
		_id_fade = document.getElementById (idElem).fading;
		document.getElementById (idElem).fadingNew = false;
	}
	
	// Si ce fade se lance pour la première fois dans sa boucle, c'est un nouveau, donc forcer son exécution !
	if (!_id_fade) {
		document.getElementById (idElem).fadingNew = document.getElementById (idElem).fading + 1;
		_id_fade = document.getElementById (idElem).fadingNew;
	}

	// Si ce fade n'est pas le nouveau, le remplacer
	if (document.getElementById (idElem).fadingNew && _id_fade != document.getElementById (idElem).fadingNew) {
		document.getElementById (idElem).fading = document.getElementById (idElem).fadingNew;
		document.getElementById (idElem).fadingNew = false;
		return;
	}
		
	// Si ce fade correspond au nouveau, le faire dormir
	if (document.getElementById (idElem).fadingNew && _id_fade == document.getElementById (idElem).fadingNew)
		setTimeout ('FadeColor(\''+csstype+'\', \''+idElem+'\', \''+hcolor+'\', \''+hcolorto+'\', '+speed+', null, '+_id_fade+')', speed);
	else {
		dcolor = h2rvb (hcolor);
		dcolorto = h2rvb (hcolorto);
		
		if (!diff)
			diff = divrvb (diffrvb (dcolorto, dcolor), 10);
		
		newhcolor = rvb2h (addrvb (dcolor, diff));
		
		if (csstype == 'background-color')
			document.getElementById (idElem).style.background = '#' + newhcolor;
		else if (csstype == 'color')
			document.getElementById (idElem).style.color = '#' + newhcolor;
	
		rcomplete = (diff[0] < 0 && dcolor[0] <= dcolorto[0]) || (diff[0] > 0 && dcolor[0] >= dcolorto[0]);
		vcomplete = (diff[1] < 0 && dcolor[1] <= dcolorto[1]) || (diff[1] > 0 && dcolor[1] >= dcolorto[1]);
		bcomplete = (diff[2] < 0 && dcolor[2] <= dcolorto[2]) || (diff[2] > 0 && dcolor[2] >= dcolorto[2]);
		
		
		if (rcomplete || vcomplete || bcomplete) {
			document.getElementById (idElem).style.color = '#' + hcolorto;
			document.getElementById (idElem).fadingNew = false;
			document.getElementById (idElem).fading = false;
		}
		else {
			setTimeout ('FadeColor(\''+csstype+'\', \''+idElem+'\', \''+newhcolor+'\', \''+hcolorto+'\', '+speed+', Array ('+diff[0]+','+diff[1]+','+diff[2]+'), '+_id_fade+')', speed);
		}
	}
}

