// Utility functions
function padLeft(sIn, cPad, iCount) {
	while(sIn.length < iCount)
		sIn=cPad+sIn;
	return sIn;
}

function To_Hex_String(iVal) {
	if (iVal > 255) iVal = 255;
	if (iVal < 0) iVal = 0;
	return padLeft(parseInt(iVal).toString(16), '0', 2);
}

// Fader Element
function oFader_Elem (oLabel, iStartX, iStartY, iEndX, iEndY, iMSMoveStart, iMSMoveTime, sStartColour, sEndColour, iMSFadeStart, iMSFadeTime) {
    this.oLabel = oLabel;
    this.iStartX = iStartX;
	this.iStartY = iStartY;
	this.iEndX = iEndX;
	this.iEndY = iEndY;
	this.iMSMoveStart = iMSMoveStart;
	this.iMSMoveTime = iMSMoveTime;
	this.sStartColour = sStartColour;
	this.sEndColour = sEndColour;
	this.iMSFadeStart = iMSFadeStart;
	this.iMSFadeTime = iMSFadeTime;
	
	this.iSCRed = parseInt("0x" + sStartColour.substr(1,2));
	this.iSCGreen = parseInt("0x" + sStartColour.substr(3,2));
	this.iSCBlue = parseInt("0x" + sStartColour.substr(5,2));
	
	this.iECRed = parseInt("0x" + sEndColour.substr(1,2));
	this.iECGreen = parseInt("0x" + sEndColour.substr(3,2));
	this.iECBlue = parseInt("0x" + sEndColour.substr(5,2));
	
	this.bFinished = false;
	
	// convert to hex <integer>.toString(16);
	
}

/**
// Fader Element
function oFader_Elem (oParent, sWord, iStartX, iStartY, iEndX, iEndY, iMSMoveStart, iMSMoveTime, sStartColour, sEndColour, iMSFadeStart, iMSFadeTime) {
    this.oParent = oParent;
    var oLabel = document.createElement('label');
    oLabel.style.position = 'absolute';
    var oText = document.createTextNode(sWord);
    this.oLabel = oLabel;
    this.oLabel.appendChild(oText);
    this.oParent.appendChild(this.oLabel);
	
	this.iStartX = iStartX;
	this.iStartY = iStartY;
	this.iEndX = iEndX;
	this.iEndY = iEndY;
	this.iMSMoveStart = iMSMoveStart;
	this.iMSMoveTime = iMSMoveTime;
	this.sStartColour = sStartColour;
	this.sEndColour = sEndColour;
	this.iMSFadeStart = iMSFadeStart;
	this.iMSFadeTime = iMSFadeTime;
	
	this.iSCRed = parseInt("0x" + sStartColour.substr(1,2));
	this.iSCGreen = parseInt("0x" + sStartColour.substr(3,2));
	this.iSCBlue = parseInt("0x" + sStartColour.substr(5,2));
	
	this.iECRed = parseInt("0x" + sEndColour.substr(1,2));
	this.iECGreen = parseInt("0x" + sEndColour.substr(3,2));
	this.iECBlue = parseInt("0x" + sEndColour.substr(5,2));
	
	this.bFinished = false;
	
	// convert to hex <integer>.toString(16);
	
}
**/
//function 
oFader_Elem.prototype.Reset = function () {
    //this.oLabel.style.display = 'block';
	this.oLabel.style.left = this.iStartX;
	this.oLabel.style.top = this.iStartY;
	this.oLabel.style.color = this.sStartColour;
	this.oLabel.style.display = 'none';
	//this.oLabel.style.visibility = 'hidden';
	this.bFinished = false;
}

function changePosition(oLabel, x, y) {
 
 if(oLabel != null) {
  if(oLabel.style == null) { // Navigator 4
   oLabel.left = x;
   oLabel.top = y;
  }else if(oLabel.style.left != null) { // DOM-capable
   oLabel.style.left = x + 'px';
   oLabel.style.top = y + 'px';
  }else{ // IE 4
   oLabel.style.posLeft = x
   oLabel.style.posTop = y
  }
 }
}

//function 
oFader_Elem.prototype.Update = function (iMilli) {
	var fXDiff = 0;
	var fYDiff = 0;
	var fMoveMult = ((iMilli - this.iMSMoveStart) / this.iMSMoveTime);
	if (iMilli >= this.iMSMoveStart && (iMilli <= (this.iMSMoveTime + this.iMSMoveStart))) {
		fXDiff = ((this.iEndX - this.iStartX) * fMoveMult);
		fYDiff = ((this.iEndY - this.iStartY) * fMoveMult);
		this.oLabel.style.display = 'block';
		this.oLabel.style.visibility = 'visible';
		//this.oLabel.style.left = this.iStartX + fXDiff;
		//this.oLabel.style.top = this.iStartY + fYDiff;
		changePosition(this.oLabel, this.iStartX + fXDiff, this.iStartY + fYDiff)
		
	}
	
	
	var sHexCol = '#FF0000'; //this.oLabel.style.color;
	var fFadeMult = 1.0;
	if (iMilli >= this.iMSFadeStart && (iMilli <= (this.iMSFadeTime + this.iMSFadeStart))) {
	    this.oLabel.style.display = 'block';
	    this.oLabel.style.visibility = 'visible';
		fFadeMult = ((iMilli - this.iMSFadeStart) / this.iMSFadeTime);
		sHexCol = "#" + To_Hex_String((this.iSCRed + ((this.iECRed - this.iSCRed) * fFadeMult))) + To_Hex_String((this.iSCGreen + ((this.iECGreen - this.iSCGreen) * fFadeMult))) + To_Hex_String((this.iSCBlue + ((this.iECBlue - this.iSCBlue) * fFadeMult)));
		try {
			this.oLabel.style.color = sHexCol;
		}
		catch (oE) {
		}
	}
		
	if (iMilli > (this.iMSFadeTime + this.iMSFadeStart) && iMilli > (this.iMSMoveTime + this.iMSMoveStart)) {
		this.bFinished = true;
		this.oLabel.style.display = 'none';
		//this.oLabel.style.visibility = 'hidden';
	} else {
		
	}
}


// Fader
function oFader() {
	this.aElems = new Array();
	this.iCurrMilliSecs = 0;		// Current fader time
	this.bLoop = false;
}

oFader.prototype.Add_Label_To_DOM = function (oParent, sWord, iStartX, iStartY, iEndX, iEndY, iMSMoveStart, iMSMoveTime, sStartColour, sEndColour, iMSFadeStart, iMSFadeTime) {
	// sStartColour and sEndColour are hex definitions in the form #RRGGBB
	// iMSMoveStart is the time the movement should start
	 var oLabel = document.createElement('div');
    oLabel.style.position = 'absolute';
    // works for ff
    oLabel.className = 'Fader_Elem1';
    
    //oLabel.setAttribute('className', 'Fader_Elem1');
    
    //oLabel.nodeValue = sWord;
    var oText = document.createTextNode(sWord);
    oLabel.appendChild(oText);
    oParent.appendChild(oLabel);
    
	this.aElems[this.aElems.length] = new oFader_Elem(oLabel, iStartX, iStartY, iEndX, iEndY, iMSMoveStart, iMSMoveTime, sStartColour, sEndColour, iMSFadeStart, iMSFadeTime);
}

oFader.prototype.Add_Label = function (oLabel, iStartX, iStartY, iEndX, iEndY, iMSMoveStart, iMSMoveTime, sStartColour, sEndColour, iMSFadeStart, iMSFadeTime) {
	// sStartColour and sEndColour are hex definitions in the form #RRGGBB
	// iMSMoveStart is the time the movement should start
	this.aElems[this.aElems.length] = new oFader_Elem(oLabel, iStartX, iStartY, iEndX, iEndY, iMSMoveStart, iMSMoveTime, sStartColour, sEndColour, iMSFadeStart, iMSFadeTime);
}

oFader.prototype.Initialise = function () {
	for (var ife = 0; ife < this.aElems.length; ife++) {
		this.aElems[ife].Reset();	
	}
}

oFader.prototype.Go = function (bLoop) {
	var iTimestep = 30;
	if (bLoop) {
		//first call so initialise
		this.bLoop = bLoop;
		this.Initialise();
	}
	
	var iFinished = 0;
	
	for (var ife = 0; ife < this.aElems.length; ife++) {
		this.aElems[ife].Update(this.iCurrMilliSecs);
		if (this.aElems[ife].bFinished) {
			iFinished++;
		}
	}
	var theFader = this;
	if (this.aElems.length == iFinished) {
		// all elements are finished
		if (this.bLoop) {
			// reset everything
			this.iCurrMilliSecs = 0;
			for (var ife = 0; ife < this.aElems.length; ife++) {
				this.aElems[ife].Reset();	
			}
			theFader.Go();
			//window.setTimeout(function() { theFader.Go(); }, iTimestep);
		}
	} else {
		this.iCurrMilliSecs += iTimestep;
		window.setTimeout(function() { theFader.Go(); }, iTimestep);
	}
	
}