
/* === A fader class ===
   parameters:
	objName: name of the object
	from: first color in the sequence, array of the color component
	to: last color in the sequence
	steps: number of steps in the sequence
	pause: ms between each step
 */
function Fader(objName, from, to, steps, pause) {
	this.objName = objName;

	this.timeout = null;
	
	this.el = null;
	this.fading = false;
	
	this.from = from;
	this.to = to;
	this.steps = steps;
	this.pause = pause;
	
	/* Start fading an object */
	this.start = function(e) {
		if (this.timeout)
			window.clearTimeout(this.timeout);
		if (this.el)
			this.el.style.color = '';
		
		this.el = event.srcElement;
		this.fading = true;
		this.fade(this.steps);
	}
	
	/* Reset the element back to the origonal color */
	this.reset = function(e) {
		this.fading = false;
		
		if (this.el)
			this.el.style.color = '';
	}
	
	/* PRIVATE: fading */
	this.fade = function(step) {
		if ((step >= 0) && (this.fading)) {
			var rgb = [0, 0, 0];
			for (j = 0; j < 3; j ++)
				rgb[j] = Math.floor(this.from[j] + (this.to[j] - this.from[j]) / (this.steps + 1) * (this.steps - step));
				
			this.el.style.color = 'rgb(' + rgb[0] + ', ' + rgb[1] + ', ' + rgb[2] + ')';
			this.timeout = window.setTimeout(objName + '.fade(' + (step - 1) + ');',  this.pause);
			
		} else
			this.fading = false;
	}
}
