// Created by Nathan DeJong on April 18, 2005
// please keep these lines on when you copy the source
// made by: Nicolas - http://www.javascript-page.com

// hexArray Constructor for hexaDecimal()
function hexArray()
{
	this.length = 16;
	for (var i = 0; i <= 10; i++) 
	{
		this[i] = '' + i;
	}
	this[10] = "a";
	this[11] = "b";
	this[12] = "c";
	this[13] = "d";
	this[14] = "e";
	this[15] = "f";
}

// Global Variable for hexadecimal()
fader = new hexArray();

// Returns Hexadecimal value of a Decimal Number
function hexadecimal(i) 
{
	return ("" + fader[Math.floor(i/16)] + fader[i%16]);
}

// Global Variables for fade()
var iter = 0;
var steps = 0;
var delay = 0;
var r2 = 0;
var g2 = 0;
var b2 = 0;
var r1 = 0;
var g1 = 0;
var b1 = 0;
var goAhead = true;
	
// Recursive Wrap Function for fade() - sets global variables for fade()
function backgroundChanger(step_amount,delay_amount,red2,green2,blue2,red1,green1,blue1) 
{
	if ((step_amount > 0) && (goAhead))
	{
		goAhead = false;
		iter = 0;
		steps = step_amount;
		delay = delay_amount;
		r2 = red2;
		g2 = green2;
		b2 = blue2;
		r1 = red1;
		g1 = green1;
		b1 = blue1;
		fade();
	}
	else if (goAhead)
	{
		var r = hexadecimal(Math.floor(red1));
		var g = hexadecimal(Math.floor(green1));
		var b = hexadecimal(Math.floor(blue1));
		document.bgColor = "#" + r + g + b;
	}
	else
	{
		setTimeout("backgroundChanger()", (delay * steps) + delay);
	}
}

// Recursive Function to fade background color
function fade()
{
	if (iter <= steps)
	{
		var r = hexadecimal(Math.floor(r2 * ((steps-iter)/steps) + r1 * (iter/steps)))
		var g = hexadecimal(Math.floor(g2 * ((steps-iter)/steps) + g1 * (iter/steps)))
		var b = hexadecimal(Math.floor(b2 * ((steps-iter)/steps) + b1 * (iter/steps)))
		document.bgColor = "#" + r + g + b;
		iter++;
		setTimeout("fade()", delay);
	}
	else
	{
		goAhead = true;
	}
}

// Global Variables for cellFade()
var citer = 0;
var csteps = 0;
var cdelay = 0;
var cr2 = 0;
var cg2 = 0;
var cb2 = 0;
var cr1 = 0;
var cg1 = 0;
var cb1 = 0;
var cellAbove;

// Recursive Wrap Function for cellFade() - sets global variables for cellFade()
function cellChanger(cell,step_amount,delay_amount,red2,green2,blue2,red1,green1,blue1) 
{
	if (step_amount > 0)
	{
		// set global vars
		citer = 0;
		csteps = step_amount;
		cdelay = delay_amount;
		cr2 = red2;
		cg2 = green2;
		cb2 = blue2;
		cr1 = red1;
		cg1 = green1;
		cb1 = blue1;
		
		//get cell
		var table = document.getElementById('tab');
		var rowIndex = cell.parentNode.rowIndex;
		cellAbove = table.rows[rowIndex].cells[cell.cellIndex];
		
		// call cellFade()
		cellFade();
	}
	else
	{
		// get color
		var r = hexadecimal(Math.floor(red1));
		var g = hexadecimal(Math.floor(green1));
		var b = hexadecimal(Math.floor(blue1));
		
		// get cell
		var table = document.getElementById('tab');
		var rowIndex = cell.parentNode.rowIndex;
		cellAbove = table.rows[rowIndex].cells[cell.cellIndex];
		
		// set cell color
		cellAbove.style.backgroundColor = "#" + r + g + b;
	}
}

// Recursive Function to fade table cell colors
function cellFade()
{
	if (citer <= csteps)
	{
	//alert(csteps);
		//get color
		var r = hexadecimal(Math.floor(cr2 * ((csteps-citer)/csteps) + cr1 * (citer/csteps)))
		var g = hexadecimal(Math.floor(cg2 * ((csteps-citer)/csteps) + cg1 * (citer/csteps)))
		var b = hexadecimal(Math.floor(cb2 * ((csteps-citer)/csteps) + cb1 * (citer/csteps)))
		
		// set cell color
		cellAbove.style.backgroundColor = "#" + r + g + b;
		
		// call recursion
		citer++;
		setTimeout("cellFade()", cdelay);
	}
}
