// Javascript animation program by Andrew J. Hardwick
//  Written 2007/9/14-2007/9/19.
//  Updated to HTML 4.01 Strict 2008/9/24-2008/10/3.
// Released under GPL.

// Grid creating function
Cells=new Array();
function CreateGrid()
{	// Create table
	document.write('<TABLE CLASS="Centred" BORDER=0 ',
			'CELLPADDING=0 CELLSPACING=0>');
	for(Row=0;Row<GridHeight;Row++)
	{	document.write('<TR>');
		for(Column=0;Column<GridWidth;Column++)
		{	document.write('<TD STYLE="',
					'width: ',Column%2?EvenCellWidth:OddCellWidth,'px;',
					'height: ',Row%2?EvenCellHeight:OddCellHeight,'px;',
					'" ID="Cell-',Column,':',GridHeight-Row-1,'"><\/TD>');}
		document.write('<\/TR>');}
	document.write('<\/TABLE>');
	// Set table cells initial colour (& cache Ids as IE 6 Id look-up is slow)
	for(x=0;x<GridWidth;x++)
	{	Cells[x]=new Array();
		for(y=0;y<GridHeight;y++)
		{	Cells[GridWidth*y+x]=document.getElementById("Cell-"+x+":"+y);
			Cells[GridWidth*y+x].style.backgroundColor=BackgroundColour;}}}

// Path colouring function
function ColourPath(ColourOffset)
{	for(CellIndex=0;CellIndex<PathCells.length;CellIndex++)
	{	ColourIndex=(CellIndex-ColourOffset+PathColours.length)%PathColours.length;
		Cells[PathCells[CellIndex]].style.backgroundColor=PathColours[ColourIndex];}}

// Animating function
function Animate(ColourOffset)
{	StartTime=new Date();
	ColourPath(ColourOffset);
	ColourOffset=(ColourOffset+StepLength)%PathCells.length;
	TimeToNext=StepPeriod-((new Date())-StartTime);
	setTimeout('Animate('+ColourOffset+')',TimeToNext>0?TimeToNext:0);}

