// fadebg script Copyright (c) 2005 Andrew Pam <andrew@sericyb.com.au>
// This script may be used and modified in any way providing this
// copyright notice is preserved.

var DEF_RED = 252;
var DEF_GREEN = 255;
var DEF_BLUE = 180;
var DEF_STEPS = 100;

/*
// Set the starting and ending colours here:
var nowred = startred = 252;
var nowgreen = startgreen = 255;
var nowblue = startblue = 180;
var endred = 255;
var endgreen = 255;
var endblue = 255;
var startsteps = steps = 100;	// Number of steps, each 1/50 second

// Script users need not change anything after this line.
var fadetimer = null;
var redstep = (endred - startred) / steps;
var greenstep = (endgreen - startgreen) / steps;
var bluestep = (endblue - startblue) / steps;
*/
function tohex(number)
// Returns a String containing the hexadecimal representation of "number"
{
  var hexdigits = "0123456789ABCDEF";
  var result = "";
  for ( var digit = 0; number > 0; number = Math.floor(number / 16) )
  {
    result = hexdigits.charAt(number % 16) + result;
  }
  return result;
}

function colstring(red, green, blue)
// Returns a String containing the hex encoding of a colour
{
  result = "#";
  result += ("00" + tohex(red)).slice(-2);
  result += ("00" + tohex(green)).slice(-2);
  result += ("00" + tohex(blue)).slice(-2);
  return result;
}
/*
function fadebg(nd) { // modified to affect targeted node
  nd.style.backgroundColor = colstring(nowred, nowgreen, nowblue);
  if ( steps <= 0 ) {
    window.clearTimeout(fadetimer);
    nowred = startred;
    nowgreen = startgreen;
    nowblue = startblue;
    steps = startsteps;
  } else {
    nowred += redstep;
    nowgreen += greenstep;
    nowblue += bluestep;
    steps--;
    fadetimer = window.setTimeout(function () { fadebg(nd); }, 20);
  }
}
*/
function Fader(nd) {
		// initial colors
	this.sred = 252;
	this.sgreen = 255;
	this.sblue = 180;
		// end colors
	this.ered = 255;
	this.egreen = 255;
	this.eblue = 255;
		// current colors (so can be reset)
	this.cred = 252;
	this.cgreen = 255;
	this.cblue = 180;
		// calculated steps
	this.rstep = 0;
	this.gstep = 0;
	this.bstep = 0;
	
	this.steps = 100;
	
	this.node = nd;
	this.timer = null;
	
	this.calcSteps = function () {
		this.rstep = (this.ered - this.sred) / this.steps;
		this.gstep = (this.egreen - this.sgreen) / this.steps;
		this.bstep = (this.eblue - this.sblue) / this.steps;
	};
	
	this.setStartColor = function(r, g, b) { this.sred = r; this.sgreen = g; this.sblue = b; };
		
	this.setEndColor = function(r, g, b) { this.ered = r; this.egreen = g; this.eblue = b; };
	
	this.fade = function() { doFadeInner(this); };
}

function doFadeInner(obj) {
	obj.node.style.backgroundColor = colstring(obj.cred, obj.cgreen, obj.cblue);
	if (obj.steps <= 0) {
		window.clearTimeout(obj.timer);
		obj.cred = obj.sred;
		obj.cgreen = obj.sgreen;
		obj.cblue = obj.sblue;
		obj.steps = DEF_STEPS;
	} else {
		obj.cred += obj.rstep;
		obj.cgreen += obj.gstep;
		obj.cblue += obj.bstep;
		obj.steps--;
		obj.timer = window.setTimeout(function() {doFadeInner(obj);}, 20);
	}
}
