// Each RaceCell is one cell in the RaceTrack table

function RaceCell()
{
 this.init();
}

RaceCell.prototype.init = RaceCell_init;
RaceCell.prototype.clickHandler = RaceCell_clickHandler;
RaceCell.prototype.setClassNameIf = RaceCell_setClassNameIf;
RaceCell.prototype.getCode = RaceCell_getCode;
RaceCell.prototype.getX = RaceCell_getX;
RaceCell.prototype.getY = RaceCell_getY;
RaceCell.prototype.setX = RaceCell_setX;
RaceCell.prototype.setY = RaceCell_setY;
RaceCell.prototype.attachDefaultClickHandler = RaceCell_attachDefaultClickHandler;
RaceCell.prototype.setClassNameFromCode = RaceCell_setClassNameFromCode;

function RaceCell_init()
{
	this.className = "road";
	this._x = -1;
	this._y = -1;
}

function RaceCell_attachDefaultClickHandler()
{
 	// the click handler is for changing the current css class of the cell
 	// when designing the track
 	this.onclick = function(){ this.clickHandler(); }
}

function RaceCell_clickHandler()
{
 	// circulate through the available css classes when designing the track
	switch(this.className)
	{
		case "road":
			this.className = "wall";
			break;
		case "wall":
			this.className = "carHoriz road";
			tbl.setOnly(this, "carHoriz road", "road");
			break;
		case "carHoriz road":
			this.className = "finish";
			tbl.setOnly(this, "finish", "road");
			break;
		case "finish":
			this.className = "road";
			break;
	}
}

function RaceCell_setClassNameIf(cell, strClassOld, strClassNew)
{
	// if this cell has the same css class as strClassOld then change to strClassNew
	// used to ensure only one finish point and one car on the track when designing the track
	if(this!=cell && this.className==strClassOld)
		this.className = strClassNew;
}

function RaceCell_getCode()
{
 	// used for serialising the cell in the querystring....first letter of css class is the code!
	return this.className.substring(0,1);
}

// member variables x and y store location of cell within the racetrack table

function RaceCell_setX(x)
{
	this._x = x;
}

function RaceCell_setY(y)
{
	this._y = y;
}

function RaceCell_getX()
{
	return this._x;
}

function RaceCell_getY()
{
	return this._y;
}

function RaceCell_setClassNameFromCode(code)
{
	// used for de-serialising cell from querystring
	switch(code)
	{
		case "r":
			this.className = "road";
			break;
		case "w":
			this.className = "wall";
			break;
		case "c":
			this.className = "carHoriz road";
			break;
		case "f":
			this.className = "finish";
			break;
	}
}
