/*
	Name:		fader.js
	Purpose:	HTML Element background colour fader
	Site:		<Generic>			
	Author:		Matt Browne, (c) Fusion Advertising & Design Ltd 2003
	Version:	1.00 (8th July 2003)

	Changes:
			1.00	MB	08/07/2003	First version
*/


function Fader(element, start_colour, end_colour, steps, delay)
{
	this.element = element;
	this.delay = delay;
	this.interval_id = null;
	this.reversed = false;
	
	this.start_r = (start_colour & 0xFF0000) >> 16;
	this.start_g = (start_colour & 0xFF00) >> 8;
	this.start_b = start_colour & 0xFF;
	this.end_r = (end_colour & 0xFF0000) >> 16;
	this.end_g = (end_colour & 0xFF00) >> 8;
	this.end_b = end_colour & 0xFF;
	
	this.current_r = this.start_r;
	this.current_g = this.start_g;
	this.current_b = this.start_b;
	
	this.increment_r = Math.round((this.end_r - this.start_r) / steps);
	this.increment_g = Math.round((this.end_g - this.start_g) / steps);
	this.increment_b = Math.round((this.end_b - this.start_b) / steps);
	
	this.start = Fader_start;
	this.stop = Fader_stop;
	this.change = Fader_change;
	this.reverse = Fader_reverse;
	this.mouseover = Fader_mouseover;
	this.mouseout = Fader_mouseout;
}

function Fader_start(name)
{
	if (this.interval_id == null)
		this.interval_id = window.setInterval(name + ".change()", this.delay);
}

function Fader_stop()
{
	if (this.interval_id != null)
	{
		window.clearInterval(this.interval_id);
		this.interval_id = null;
	}
}

function Fader_change()
{
	var changed, string_r, string_g, string_b, colour_string;
	
	changed = false;
	
	if (((this.increment_r > 0) && (this.current_r < this.end_r)) || ((this.increment_r < 0) && (this.current_r > this.end_r)))
	{
		this.current_r += this.increment_r;
		changed = true;
	}
	
	if (((this.increment_g > 0) && (this.current_g < this.end_g)) || ((this.increment_g < 0) && (this.current_g > this.end_g)))
	{
		this.current_g += this.increment_g;
		changed = true;
	}
	
	if (((this.increment_b > 0) && (this.current_b < this.end_b)) || ((this.increment_b < 0) && (this.current_b > this.end_b)))
	{
		this.current_b += this.increment_b;
		changed = true;
	}
	
	if (changed)
	{
		if (((this.increment_r > 0) && (this.current_r > this.end_r)) || ((this.increment_r < 0) && (this.current_r < this.end_r)))
			this.current_r = this.end_r;
		
		if (((this.increment_g > 0) && (this.current_g > this.end_g)) || ((this.increment_g < 0) && (this.current_g < this.end_g)))
			this.current_g = this.end_g;
		
		if (((this.increment_b > 0) && (this.current_b > this.end_b)) || ((this.increment_b < 0) && (this.current_b < this.end_b)))
			this.current_b = this.end_b;
		
		string_r = (this.current_r & 0xFF).toString(16);
		
		if (string_r.length == 1)
			string_r = "0" + string_r;
		
		string_g = (this.current_g & 0xFF).toString(16);
		
		if (string_g.length == 1)
			string_g = "0" + string_g;
		
		string_b = (this.current_b & 0xFF).toString(16);
		
		if (string_b.length == 1)
			string_b = "0" + string_b;
		
		colour_string = "#" + string_r + string_g + string_b;
		
		document.getElementById(this.element).style.backgroundColor = colour_string;
	}
	else
		this.stop();
}

function Fader_reverse()
{
	var temp_r, temp_g, temp_b;
	
	temp_r = this.start_r;
	temp_g = this.start_g;
	temp_b = this.start_b;
	
	this.start_r = this.end_r;
	this.start_g = this.end_g;
	this.start_b = this.end_b;
	
	this.end_r = temp_r;
	this.end_g = temp_g;
	this.end_b = temp_b;
	
	this.increment_r = 1 - this.increment_r;
	this.increment_g = 1 - this.increment_g;
	this.increment_b = 1 - this.increment_b;
	
	if (this.reversed)
		this.reversed = false;
	else
		this.reversed = true;
}

function Fader_mouseover(name)
{
	if (this.reversed)
		this.reverse();
	
	this.start(name);
}

function Fader_mouseout(name)
{
	if (!this.reversed)
		this.reverse();
	
	this.start(name);
}