function toHex(n){
	var hexChars = "0123456789ABCDEF";
	if (n == 0) return "00";
	var j, k;
	var temp = "";
	while (n != 0){
		j = n % 16;
		n = (n - j)/16;
		temp = hexChars.charAt(j) + temp;
	}
	if (temp.length < 2){
		temp = "0" + temp;
	}
	return temp;
}

function Fader(objId, strColor, endColor, strBgColor, endBgColor, loop, speed){
if (!document.getElementById) return;
  	this.obj = document.getElementById(objId);
	this.strColor = strColor;
	this.endColor = endColor;
	this.strBgColor = strBgColor;
	this.endBgColor = endBgColor;
	if ((this.strColor != '') && (this.endColor != '')){
		this.str_red_txt = "0x"+this.strColor.substring(1,3);
		this.str_green_txt = "0x"+this.strColor.substring(3,5);
		this.str_blue_txt = "0x"+this.strColor.substring(5,7);
		this.end_red_txt = "0x"+this.endColor.substring(1,3);
		this.end_green_txt = "0x"+this.endColor.substring(3,5);
		this.end_blue_txt = "0x"+this.endColor.substring(5,7);
	}
	if ((this.strBgColor != '') && (this.endBgColor != '')){
		this.bg_str_red_txt = "0x"+this.strBgColor.substring(1,3);
		this.bg_str_green_txt ="0x"+this.strBgColor.substring(3,5);
		this.bg_str_blue_txt = "0x"+this.strBgColor.substring(5,7);
		this.bg_end_red_txt = "0x"+this.endBgColor.substring(1,3);
		this.bg_end_green_txt = "0x"+this.endBgColor.substring(3,5);
		this.bg_end_blue_txt = "0x"+this.endBgColor.substring(5,7);
	}
	this.loop = loop;
	this.speed = speed;
	this.timer = null;
	this.bgTimer = null;
}
Fader.prototype.fade = fade;
Fader.prototype.bgFade = bgFade;
Fader.prototype.toogle = toogle;
Fader.prototype.bgToogle = bgToogle;
Fader.prototype.stopFade = stopFade;
Fader.prototype.stopBgFade = stopBgFade;

function fade(){
if (!document.getElementById) return;
	this.color = "#" + toHex(this.str_red_txt) + toHex(this.str_green_txt) + toHex(this.str_blue_txt);
	this.obj.style.color = this.color;
	if (this.end_red_txt != this.str_red_txt){
		if (this.end_red_txt > this.str_red_txt){
			this.str_red_txt++;
		}
		else{
			this.str_red_txt--;
		}
	}
	if (this.end_green_txt != this.str_green_txt){
		if (this.end_green_txt > this.str_green_txt){
			this.str_green_txt++;
		}
		else{
			this.str_green_txt--;
		}
	}	
	if (this.end_blue_txt != this.str_blue_txt){
		if (this.end_blue_txt > this.str_blue_txt){
			this.str_blue_txt++;
		}
		else{
			this.str_blue_txt--;
		}
	}
if ((this.str_red_txt == this.end_red_txt) && (this.str_green_txt == this.end_green_txt) && (this.str_blue_txt == this.end_blue_txt)){
	window.clearTimeout(this.timer);
	if (this.loop){
		this.toogle();
	}
}
else{
 var current = this;
 this.timer = window.setTimeout(function(){current.fade()},current.speed);
}
}

function bgFade(){
if (!document.getElementById) return;
	this.bgColor = "#" + toHex(this.bg_str_red_txt) + toHex(this.bg_str_green_txt) + toHex(this.bg_str_blue_txt);
	this.obj.style.backgroundColor = this.bgColor;
	if (this.bg_end_red_txt != this.bg_str_red_txt){
		if (this.bg_end_red_txt > this.bg_str_red_txt){
			this.bg_str_red_txt++;
		}
		else{
			this.bg_str_red_txt--;
		}
	}
	if (this.bg_end_green_txt != this.bg_str_green_txt){
		if (this.bg_end_green_txt > this.bg_str_green_txt){
			this.bg_str_green_txt++;
		}
		else{
			this.bg_str_green_txt--;
		}
	}	
	if (this.bg_end_blue_txt != this.bg_str_blue_txt){
		if (this.bg_end_blue_txt > this.bg_str_blue_txt){
			this.bg_str_blue_txt++;
		}
		else{
			this.bg_str_blue_txt--;
		}
	}
if ((this.bg_str_red_txt == this.bg_end_red_txt) && (this.bg_str_green_txt == this.bg_end_green_txt) && (this.bg_str_blue_txt == this.bg_end_blue_txt)){
	window.clearTimeout(this.bgTimer);
	if (this.loop){
		this.bgToogle();
	}
}
else{
var current = this;
this.bgTimer = window.setTimeout(function(){current.bgFade()},current.speed);
}
}

function toogle(){
	var priv = this.strColor;
	this.strColor = this.endColor;
	this.endColor = priv;
	this.str_red_txt = "0x"+this.strColor.substring(1,3);
	this.str_green_txt = "0x"+this.strColor.substring(3,5);
	this.str_blue_txt = "0x"+this.strColor.substring(5,7);
	this.end_red_txt = "0x"+this.endColor.substring(1,3);
	this.end_green_txt = "0x"+this.endColor.substring(3,5);
	this.end_blue_txt = "0x"+this.endColor.substring(5,7);
	this.fade();
}

function bgToogle(){
	var bgpriv = this.strBgColor;
	this.strBgColor = this.endBgColor;
	this.endBgColor = bgpriv;
	this.bg_str_red_txt = "0x"+this.strBgColor.substring(1,3);
	this.bg_str_green_txt = "0x"+this.strBgColor.substring(3,5);
	this.bg_str_blue_txt = "0x"+this.strBgColor.substring(5,7);
	this.bg_end_red_txt = "0x"+this.endBgColor.substring(1,3);
	this.bg_end_green_txt = "0x"+this.endBgColor.substring(3,5);
	this.bg_end_blue_txt = "0x"+this.endBgColor.substring(5,7);
	this.bgFade();
}

function stopFade(){
	window.clearTimeout(this.timer);
	this.timer = null;
}

function stopBgFade(){
	window.clearTimeout(this.bgTimer);
	this.bgTimer = null;
}
