// JavaScript Document

// ========================================================================================================
// "ExtraMove" - Module de changement de couleur 
//   by Extrafine (contact@extrafine.fr)
// ========================================================================================================
// 

function ExtraColor(pId) 
{	
	// Attribut ###############################################
	this.Id = pId;				// Id de la Balise
	this.R = 0;					// Rouge 
	this.V = 0;					// Vert
	this.B = 0;					// Bleu
	this.Rx = 0;				// Coefficient Rouge
	this.Vx = 0;				// Coefficient Vert
	this.Bx = 0;				// Coefficient Bleu
	this.RVB1 = "000000";		// Color Antérieur
	this.RVB2 = "000000";		// Color Destination
	this.Type = "background"	// Element CSS Modifier ("background" | "color")
	this.Loop = true;			// Loop
	this.Interval = null;		// Identifiant de l'Interval
	this.InMove = false;		// Mouvement en cours
	this.Fondu = true;			// Fondu du Mouvement
	this.Frequence = 50;		// Fréquence
	this.Coef = 255;			// Coefficiant
	
	// Methode ################################################
	this.Init = ExtraColor_Init;			// Initialisation Couleur
	this.ColorTo = ExtraColor_ColorTo;		// Changement vers la Couleur (y:0x)
	this.ColorBy = ExtraColor_ColorBy;		// Changement de --> vers la Couleur (x:0x | y:0x)
	this.ColorSet = ExtraColor_ColorSet;	// Calcul de la Couleur
	this.DoColor = ExtraColor_DoColor;		// Modification de la Couleur(Color: 0x)
	this.ColorBreak = ExtraColor_ColorBreak;// Arrêt de la modification de couleur
}

function ExtraColor_Init() { this.DoColor("000000"); }

function ExtraColor_ColorTo(y) // Changement vers la Couleur (y:0x)
{
	cTmpR = "00"+ this.R.toString(16);
	eLen = cTmpR.length;
	cTmpR = cTmpR.substring(eLen-2,eLen);
	cTmpV = "00"+ this.V.toString(16);
	eLen = cTmpV.length;
	cTmpV = cTmpV.substring(eLen-2,eLen);
	cTmpB = "00"+ this.B.toString(16);
	eLen = cTmpB.length;
	cTmpB = cTmpB.substring(eLen-2,eLen);
	x = cTmpR + cTmpV + cTmpB;
	this.ColorBy(x,y);	
}

function ExtraColor_ColorBy(x,y) // Changement de --> vers la Couleur (x:0x | y:0x)
{
	this.RVB1 = x;
	this.RVB2 = y;

	this.R = parseInt(this.RVB1.substring(0,2),16);
	this.V = parseInt(this.RVB1.substring(2,4),16);
	this.B = parseInt(this.RVB1.substring(4,6),16);

	this.Rx = (parseInt(y.substring(0,2),16) - parseInt(x.substring(0,2),16)) / this.Coef;
	this.Vx = (parseInt(y.substring(2,4),16) - parseInt(x.substring(2,4),16)) / this.Coef;
	this.Bx = (parseInt(y.substring(4,6),16) - parseInt(x.substring(4,6),16)) / this.Coef;

	if(this.Fondu) {
		this.InMove = true;
		clearInterval(this.Interval);
		this.Interval = null;
		var myThis = this;
		this.Interval = setInterval(function(){myThis.ColorSet()},this.Frequence);
	} else
		this.DoColor(this.RVB2);
}

function ExtraColor_ColorSet() // Calcul de la Couleur
{
	this.R += this.Rx;
	//this.R = Math.round(this.R);
	if(this.Rx < 0)
		this.R = Math.max(this.R,parseInt(this.RVB2.substring(0,2),16));
	else
		this.R = Math.min(this.R,parseInt(this.RVB2.substring(0,2),16));

	this.V += this.Vx;
	//this.V = Math.round(this.V);
	if(this.Vx < 0)
		this.V = Math.max(this.V,parseInt(this.RVB2.substring(2,4),16));
	else
		this.V = Math.min(this.V,parseInt(this.RVB2.substring(2,4),16));

	this.B += this.Bx;
	//this.B = Math.round(this.B);
	if(this.Bx < 0)
		this.B = Math.max(this.B,parseInt(this.RVB2.substring(4,6),16));
	else
		this.B = Math.min(this.B,parseInt(this.RVB2.substring(4,6),16));
	
	if(this.R == parseInt(this.RVB2.substring(0,2),16) && this.V == parseInt(this.RVB2.substring(2,4),16) && this.B == parseInt(this.RVB2.substring(4,6),16)) {
		if(this.Loop) {
			cTmp = this.RVB2;
			this.RVB2 = this.RVB1;
			this.RVB1 = cTmp;
			this.Rx = this.Rx * -1;
			this.Vx = this.Vx * -1;
			this.Bx = this.Bx * -1;
		} else {
			this.InMove = false;
			clearInterval(this.Interval);
			this.Interval = null;
		}
	}
	
	this.R = Math.round(this.R);
	this.V = Math.round(this.V);
	this.B = Math.round(this.B);
	
	cTmpR = "00"+ this.R.toString(16);
	eLen = cTmpR.length;
	cTmpR = cTmpR.substring(eLen-2,eLen);
	cTmpV = "00"+ this.V.toString(16);
	eLen = cTmpV.length;
	cTmpV = cTmpV.substring(eLen-2,eLen);
	cTmpB = "00"+ this.B.toString(16);
	eLen = cTmpB.length;
	cTmpB = cTmpB.substring(eLen-2,eLen);

	this.DoColor(cTmpR + cTmpV + cTmpB);
}

function ExtraColor_DoColor(x) // Modification de la Couleur(Color: 0x)
{
	this.R = parseInt(x.substring(0,2),16);
	this.V = parseInt(x.substring(2,4),16);
	this.B = parseInt(x.substring(4,6),16);
	
	var element = document.getElementById(this.Id);
	switch(this.Type.toLowerCase()) {
		case "background":
			element.style.backgroundColor = "#"+ x ;
			break;
		case "color":
			element.style.color = "#"+ x ;
			break;
		default:
			element.style.backgroundColor = "#"+ x ;
	}
}

function ExtraColor_ColorBreak() // Arrêt de la modification de couleur
{
	if(this.Interval != null) {
		this.InMove = false;
		clearInterval(this.Interval);
		this.Interval = null;
	}
}
