function Fade(startR, startG, startB, endR, endG, endB) { 	

    this.startRed   = parseInt(startR, 10);
    this.startGreen = parseInt(startG, 10);
    this.startBlue  = parseInt(startB, 10);
    
    this.endRed   = parseInt(endR, 10);
    this.endGreen = parseInt(endG, 10);
    this.endBlue  = parseInt(endB, 10);
    
    this.numSteps = 10;
    this.duration = 30;
    
    this.fadein = _Fadein;
    this.fadeout = _Fadeout;
    this.setObject = _Object;
    this.incr = _Incr;
    this.decr = _Decr;
    this.getColor = _Color;
    this.colorToString = _ColorToString;
    this.playEffect = _PlayEffect;

    this.deltaRed   = (this.endRed - this.startRed) / this.numSteps;
    this.deltaGreen = (this.endGreen - this.startGreen) / this.numSteps;
    this.deltaBlue  = (this.endBlue - this.startBlue) / this.numSteps;	

    this.init = _Init;
    this.currStep = 0;
    this.init()
    this.timerID = 0;
    this.lock = false;
}

function _PlayEffect() {
    this.object.style.color = this.getColor();    
}



function _Init() {
    this.currRed   = this.startRed;
    this.currGreen = this.startGreen;
    this.currBlue  = this.startBlue;
}

function _ColorToString() {
    return this.currRed + " " + this.currGreen + " " + this.currBlue;
}

function _Color() {
    var hexRed   = decToHex(this.currRed);
    var hexGreen = decToHex(this.currGreen);
    var hexBlue  = decToHex(this.currBlue);
    
    if (hexRed == -1 || hexGreen == -1 || hexBlue == -1) {
	alert(this.colorToString());
    }

    return "#"+hexRed+""+hexGreen+""+hexBlue+"";	
}

function _Incr() {
    this.currRed   += this.deltaRed;
    this.currGreen += this.deltaGreen;
    this.currBlue  += this.deltaBlue;
}

function _Decr() {
    this.currRed   -= this.deltaRed;
    this.currGreen -= this.deltaGreen;
    this.currBlue  -= this.deltaBlue;
}

function _Object(name) {
    var object = document.all ? eval("document.all."+name) : document.getElementById(name);
    this.object = object;
}


function _Fadein() {
        this.currStep ++;
        if (this.currStep <= this.numSteps)
        {
	    this.incr();
		this.playEffect();
	    return true;
	}	
	return false;
}

function _Fadeout() {
        this.currStep --;
        if (this.currStep > 0) {
	    
	    this.playEffect();
	    this.decr();
	    return true;
	} 
	
	if (this.currStep == 0) {
	    
	    this.init();
	    this.playEffect();

	}
	return false;
}


function fadein(name) {
    var object = eval(name);
    if (object.lock) {
	clearTimeout(object.timerID);
	object.timerID = 0;
	object.lock = false;
    }
    _fadein(name);
}

function _fadein(name) {
    var object = eval(name);
    object.lock = true;
    var test = object.fadein();
    if (test) {
	object.timerID = setTimeout("_fadein('"+name+"')", object.duration);
    } 
}


function fadeout(name) {
    var object = eval(name);
    if (object.lock) {
	clearTimeout(object.timerID);
	object.timerID = 0;
	object.lock = false;
    }
    _fadeout(name);
}

function _fadeout(name) {
    var object = eval(name);
    object.lock = true;
    var test = object.fadeout();
    if (test) {
	object.timerID = setTimeout("_fadeout('"+name+"')", object.duration);
    }
}





////////////////////////////////////////
// convert decimal to hexadecimal number
////////////////////////////////////////
function decToHex(decNum)
{
	decNum=Math.floor(decNum);
	if (decNum < 0) {
	    decNum=0;
	}
	var decString=""+decNum;
	// make sure the number is valid
	for (var i=0; i<decString.length; i++) {
		
	    if (decString.charAt(i)>='0' && decString.charAt(i)<='9')
		{
		}
	    else
		{
		    //alert(decString+" is not a valid decimal number because it contains "+decString.charAt(i));
		    alert(decNum);
		    return -1;
		}
	}
	var result=decNum;
	var remainder="";
	// use string because math operation won't work with hex alphabet
	var hexNum="";
	
	var hexAlphabet=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
	//	alert("converting "+decNum+" to "+hexNum);
	while (result>0)
	    {
		result=Math.floor(decNum/16);
		remainder=decNum%16;
		decNum=result;
		
		/*		if (remainder>=10)
				{
				// use double quotes because Netscape 3 will give error if using single quote
				if (remainder==10)
				remainder="A";
				if (remainder==11)
				remainder="B";
				if (remainder==12)
				remainder="C";
				if (remainder==13)
				remainder="D";
				if (remainder==14)
				remainder="E";
				if (remainder==15)
				remainder="F";
				}*/
		// just append the next remainder to the beginning of the string
		hexNum=""+hexAlphabet[remainder]+""+hexNum;
	    };
	//	alert("converting "+decNum+" to "+hexNum);
	// make sure to have at least 2 digits
	if (hexNum.length==1)
	    hexNum="0"+hexNum;
	else if (hexNum.length==0)
	    hexNum="00";
	return hexNum;
}   
