/*
** a quick hack to demonstrate Langton's Ant
** Works for Internet Explorer, may not work for other browsers
** Doesn't work for Firefox - can't find an easy way to update
** the background colour of a single cell. You can use styles
** but you have to have a style for each cell which is ludicrous
** (and triples the size of the file)
** If anyone knows how to deal with this, let me know.
** requires a fixed grid table called theTable in the calling page
** which is ROWS by COLS in size
** creating the table in javascript seems to be a bit slow
**
** Feel free to modify
** Preferably leave this header in
** Stephen Hart 2005-2006
*/


// set defaults and constants
var ROWS = 50;
var COLS = 60;

var currow = ROWS/2;
var curcol = COLS/2;
var curcell = null;
var tbl = null;

var NORTH = 0;
var EAST = 1;
var SOUTH = 2;
var WEST = 3;
var direction=NORTH;

var WHITE = "#ffffff";
var BLACK = "#000000";
var INSECT = "#33aa33";

var rememberedColor = WHITE;

var count = 0;
var speed = 64;
var moveTimeout = null;

// update any page element defined by an id
function updateById(id, newval)
{
	var tmp = document.getElementById(id);
	tmp.firstChild.nodeValue = newval;
}


// start again
function reset()
{
	currow = ROWS/2;
	curcol = COLS/2;
	direction = NORTH;
	rememberedColor = WHITE;
	count = 0;
	speed = 64;
	if (moveTimeout != null)
		clearTimeout(moveTimeout);

	for (var j = 0; j < ROWS; ++j)
	{
		for (var i = 0; i < COLS; ++i)
			tbl.rows[j].cells[i].bgColor = WHITE;
	}

	curcell = tbl.rows[currow].cells[curcol];
	curcell.bgColor=INSECT;

	updateById('tmout', speed);
	updateById('info', count);

}

// move the insect one square
function move()
{
	// change direction based on colour of new square - turn right on black, left on white
	if ((direction == NORTH && --currow < 0)
	|| (direction == EAST && ++curcol == COLS)
	|| (direction == SOUTH && ++currow == ROWS)
	|| (direction == WEST && --curcol < 0))
		return edgeOfWorld();

	// replace the insect with the correct color
	curcell.bgColor = rememberedColor;

	curcell = tbl.rows[currow].cells[curcol];
	if (curcell.bgColor == BLACK)
	{
		if (--direction < NORTH)
			direction = WEST;
		rememberedColor = WHITE;
	}
	else
	{
		if (++direction > WEST)
			direction = NORTH;
		rememberedColor = BLACK;
	}

	// show the insect
	curcell.bgColor = INSECT;

	updateById('info', ++count);

	moveTimeout = setTimeout("move();", speed);
	return 0;

}

function edgeOfWorld()
{
	alert("Edge of the World!");
	return 1;
}

function setSpeed(change)
{
	if (change == 'max')
		speed = 0;
	else if (change == 'slower')
		speed = (speed == 0)? 1: speed * 2;
	else if (change == 'faster' && speed > 1)
		speed /= 2;

	updateById('tmout', speed);
}

function toggleColor(event)
{
	if (event.srcElement.bgColor == INSECT)
		alert("Ouch!");     // clicked on insect
	else
		event.srcElement.bgColor = (event.srcElement.bgColor == BLACK)? WHITE: BLACK;
}

function run()
{
	if (document.getElementById('theTable'))
		move();
	else
		alert("theTable does not exist");
}

/*
** note, we use a static-sized table here as building it cell by cell is too slow
*/
function buildTable(cellz)
{
	tbl = document.getElementById('theTable');
	tbl.width=COLS * cellz;
	tbl.height=ROWS * cellz;

	curcell = tbl.rows[currow].cells[curcol];
	curcell.bgColor=INSECT;
}


