// A Car instance maintains the position of the car and handles moving the car
// and checking for crashes and finishing

function Car(track, varName, speedometer)
{
 this.init(track, varName, speedometer);
 
 // pre-load alternative car image
 var carVert = new Image();
 carVert.src="images/carVert.gif";
}

Car.prototype.init = Car_init;
Car.prototype.move = Car_move;
Car.prototype.isCrashed = Car_isCrashed;
Car.prototype.isFinished = Car_isFinished;
Car.prototype.steer = Car_steer;
Car.prototype.startCar = Car_startCar;
Car.prototype.stopCar = Car_stopCar;
Car.prototype.changeSpeed = Car_changeSpeed;
Car.prototype.reset = Car_reset;
Car.prototype.recoverFromCrash = Car_recoverFromCrash;

function Car_init(track, varName, speedometer)
{
	this._track = track;
	this._cell = this._track.findCell("carHoriz road");
	this._startCell = this._cell;

	this._direction = "";
	this._timerId = null;
	this._varName = varName;
	this._speedometer = speedometer;
	this._currentCarClass = "carHoriz";
}

// move the car one cell in the current direction
function Car_move()
{
	// set css class of current cell back to road before we make the move
	this._cell.className = "road";

 	switch(this._direction)
 	{
 		case 'U':
 			this._cell = this._track.getRaceCell(this._cell._x, this._cell._y - 1);
 			break;
 		case 'L':
 			this._cell = this._track.getRaceCell(this._cell._x - 1, this._cell._y);
 			break;
 		case 'D':
 			this._cell = this._track.getRaceCell(this._cell._x, this._cell._y + 1);
 			break;
 		case 'R':
 			this._cell = this._track.getRaceCell(this._cell._x + 1, this._cell._y);
 			break;
 	}

	// change the css class of the new location so that it displays our car
	this._cell.className += " " + this._currentCarClass;
	
	if(this.isCrashed())
	{
		this.stopCar();
		window.setTimeout(this._varName + ".recoverFromCrash();" , 1000);
	} else if(this.isFinished())
	{
		alert("Nice driving!");
		this._cell.className = "finish";
		this.reset();
	}
}

function Car_isCrashed()
{
	return (this._cell.className.indexOf("wall")!=-1);
}

function Car_isFinished()
{
	return (this._cell.className.indexOf("finish")!=-1);
}

// called when a direction button is clicked
function Car_steer(direction)
{
 	switch(direction)
 	{
 		case 'U':
 		case 'D':
 			this._currentCarClass = "carVert";
 			break;
 		default:
                        this._currentCarClass = "carHoriz";
			break;
 	}

	this._direction = direction;
 	
	 // if car not currently moving then start the car!
	 if(!this._timerId)
		this.startCar();
}

function Car_startCar()
{
	// get the car going and store the id so we can stop the car later!
	if(!this.isCrashed())
		this._timerId = window.setInterval(this._varName + ".move();", this._speedometer.getSpeed());
}

function Car_stopCar()
{
	clearInterval(this._timerId);
}

function Car_changeSpeed()
{
	// to implement a change of speed, we stop the car and then re-start it at the new speed
	if(this._timerId)
	{
		this.stopCar();
		this.startCar();
	}
}

function Car_reset()
{
	this.stopCar();
	this._timerId = null;
	this._cell = this._startCell;
	this._cell.className = "carHoriz road";
	this._currentCarClass = "carHoriz";
}

function Car_recoverFromCrash()
{
	this._cell.className = "wall";
	this.reset();
}


