//timer start

function display()
{ 
	if (seconds > 0)
	{
		seconds--;
		document.timerForm.timer.value=seconds;
		setTimeout("display()",1000);
	}
}

function makeObj(type, x, y) {
	// <div class="type" style="top: xpx; left: ypx" />

	var name;

	if (type=="wall") 
	{
		name = type+"_"+x+"_"+y;
	} 
	else if (type=="coin") 
	{
		name = type+"_"+x+"_"+y;
	} 
	else if (type=="lava") 
	{
		name = type+"_"+x+"_"+y;
	} 
	else if (type=="exit") 
	{
		name = type+"_"+x+"_"+y;
	} 
	else if (type=="cactus") 
	{
		name = type+"_"+x+"_"+y;
	} 
	else 
	{
		name = type+"_"+objCount;
		playerNum = objCount;
	}

	writeThis = "<div id='"+name+"' class='"+type+"' style='left: "+Number(x*tileSize+xOffset)+"px; top: "+Number(y*tileSize+xOffset)+"px'></div>";

	objCount++;
	document.write(writeThis);

	document.getElementById(name).x = x;
	document.getElementById(name).y = y;

	return document.getElementById(name);
}

function assessMap() {
	for (var y=0; y<map.length; y++) {
		for (var x=0; x<map[0].length; x++) {
			if (map[y][x] == 1) {
				makeObj("wall", x, y);
			}
			if (map[y][x] == 2) {
				coinArray[currCoin] = makeObj("coin", x, y);
				coinArray[currCoin].active = true;
				currCoin++;
			}
			if (map[y][x] == 3) {
				makeObj("lava", x, y);
			}
			if (map[y][x] == 4) {
				makeObj("exit", x, y);
			}
			if ((map[y][x] == 5) || (map[y][x] == 6)) {
				makeObj("cactus", x, y);
			}
		}
	}
}

function testDirections(currObj) {
	var left = Number(String(currObj.style.left).substring(0, String(currObj.style.left).length-2));
	var top = Number(String(currObj.style.top).substring(0, String(currObj.style.top).length-2));

	var bottom = Math.floor((top+tileSize-1-yOffset)/tileSize);
	top = Math.floor((top-yOffset)/tileSize);
	var right = Math.floor((left+tileSize-1-xOffset)/tileSize);
	left = Math.floor((left-xOffset)/tileSize);

	currObj.canUp = true;
	currObj.canDown = true;
	currObj.canLeft = true;
	currObj.canRight = true;
	currObj.maxTop;
	currObj.maxBottom;
	currObj.maxLeft;
	currObj.maxRight;

	onCoin  = false;

	if((map[top-1][left] == 1) || (map[top-1][right] == 1)) 
	{
		currObj.canUp = false;
		currObj.maxTop = top*tileSize+yOffset;
	}
	if((map[bottom+1][left] == 1) || (map[bottom+1][right] == 1)) {
		currObj.canDown = false;
		currObj.maxBottom = bottom*tileSize+yOffset; 
	 }
	if((map[top][left-1] == 1) || (map[bottom][left-1] == 1)) {
		currObj.canLeft = false;
		currObj.maxLeft = left*tileSize+xOffset;
	}
	if((map[top][right+1] == 1) || (map[bottom][right+1] == 1)) {
		currObj.canRight = false;
		currObj.maxRight = right*tileSize+xOffset;
	}

	//tests wether you're on a coin or not.
	if (map[top][right] == 2)
		{if(removeCoin(top, right)){onCoin = true;numCoins++;document.coinForm.counter.value++;}}
	if (map[top][left] == 2)
		{if(removeCoin(top, left)){onCoin = true;numCoins++;document.coinForm.counter.value++;}}
	if (map[bottom][right] == 2)
		{if(removeCoin(bottom, right)){onCoin = true;numCoins++;document.coinForm.counter.value++;}}
	if (map[bottom][left] == 2)
		{if(removeCoin(bottom, left)){onCoin = true;numCoins++;document.coinForm.counter.value++;}}

	//tests wether you're on lava or not.
	if (	(map[top][right] == 3) ||
		(map[top][left] == 3) ||
		(map[bottom][right] == 3) ||
		(map[bottom][left] == 3)
	) {killPlayer();}

	//tests wether you're on the exit or not.
	if (	(map[top][right] == 4) ||
		(map[top][left] == 4) ||
		(map[bottom][right] == 4) ||
		(map[bottom][left] == 4)
	) {onExit();}
	//tests wether you're on a cactus or not.
	if (	(map[top][right] == 5) ||
		(map[top][left] == 5) ||
		(map[bottom][right] == 5) ||
		(map[bottom][left] == 5)
	) {ySpeed = -15;decreaseHP(10);}
	//tests wether you're on a top cactus or not.
	if (	(map[top][right] == 6) ||
		(map[top][left] == 6) ||
		(map[bottom][right] == 6) ||
		(map[bottom][left] == 6)
	) {ySpeed = 15;decreaseHP(10);}

	/*document.title = "Up: "+currObj.canUp+", Down: "+currObj.canDown+", Right: "+currObj.canRight+", Left: "+currObj.canLeft + "on coin: "+onCoin+" coins: "+numCoins;*/
}

function decreaseHP(num)
{
	HP -= num;
	document.hpForm.hp.value = HP;
	if(HP <= 0){killPlayer();}
}

//key 40 is down.
//these two functions MAY be expanded to other games.
function assessKeysDown(evt) {
	var key = evt.keyCode;
	if (key == 37) {
		keyLeft = true;
		mainChar.style.backgroundImage  =  "url('images/mariosmallleft.png')";
	}
	if (key == 39) {
		keyRight = true;
		mainChar.style.backgroundImage  =  "url('images/mariosmall.png')";
	} 
	if (key == 38) {
		keyUp = true;
		evt.preventDefault();
	}
	if (key == 40) {
		evt.preventDefault();
	}
}
function assessKeysUp(evt) {
	key = evt.keyCode;
	if (key == 37) {
		keyLeft = false;
	}
	if (key == 39) {
		keyRight = false;
	} 
	if (key == 38) {
		keyUp = false;
	}
}


function removeCoin(y, x)
{
//finds the coin that is on the x, y that youare on.  removes it.

	for(var i = 0; i < coinCount; i++)
	{
		if ((coinArray[i].x == x) && (coinArray[i].y == y))
		{
			if(coinArray[i].active == true)
			{			
				coinArray[i].style.backgroundColor = "#FFFFFF";	
				coinArray[i].style.borderColor = "#FFFFFF";	
				coinArray[i].active = false;
				return true;
			}
			return false;
		}
	}
	return false;
}

function loop() {

	var left = Number(String(mainChar.style.left).substring(0, String(mainChar.style.left).length-2));
	var top = Number(String(mainChar.style.top).substring(0, String(mainChar.style.top).length-2));
	
	if (keyRight) {
		xSpeed += xSpeed < 5 ? 1 : 0;
	} else if (keyLeft) {
		xSpeed -= xSpeed > -5 ? 1 : 0;
	} else {
		xSpeed -= xSpeed != 0 ? xSpeed/Math.abs(xSpeed) : 0;
	}
	
	left += xSpeed;
	
	//test that the object is still within bounds
	if (!mainChar.canLeft && left < mainChar.maxLeft) {
		left = mainChar.maxLeft;
	}
	if (!mainChar.canRight && left > mainChar.maxRight) {
		left = mainChar.maxRight;
	}
	//if you're pressing up AND you can't go down, you are resting on a surface.  so jump.
	if (keyUp && !mainChar.canDown) {
		ySpeed = -10; //increase negatively to increase jump height
	}
	
	ySpeed += ySpeed <= 50 ? 1 : 0; //gravitational effect
	if (!mainChar.canDown && top + ySpeed > mainChar.maxBottom) {
		ySpeed = 0;
		top = mainChar.maxBottom;
	}
	
	if (!mainChar.canUp && top + ySpeed < mainChar.maxTop) {
		ySpeed = 0;
		top = mainChar.maxTop;
	}

	top += ySpeed;

	if(!resetVar){
	mainChar.style.left = left;
	mainChar.style.top = top;}
	else {resetVar = false;}
	testDirections(mainChar);

	if(youWin)//if you have all the coins, say you win
	{winFunc();window.location = "index.php?place=mall";return;}
	else if(numLives == 0) 
	{gameOver();return;}
	else	
	{setTimeout("loop()", runSpeed);}
}

function winFunc()
{
	var score = (numCoins*seconds) * (HP / 100);
	alert("You win!  Coins collected: "+numCoins+" Time left: "+seconds+" Total score: "+score);
	seconds = 0;
}

function killPlayer()
{
	numLives--;
	document.livesForm.lives.value = numLives;
	document.hpForm.hp.value = 100;
	HP = 100;
	resetFunc();
}

function gameOver()
{
	alert("Game Over!");
	
}

function onExit()
{
	youWin = true;
}

//resets the player to the starting location
function resetFunc()
{
	var left = Number(startTile.x*tileSize+xOffset)+"px";
	var top = Number(startTile.y*tileSize+yOffset)+"px";

	mainChar.style.left = left;
	mainChar.style.top = top;  

	//resetvar is used to skip the first loop() call, fixing the 
	//bug where it won't update the new location.
	resetVar  = true;
	
	//setTimeout("loop()", runSpeed);
	
}


