var player1_turn = false;

for (i = 0; i < 9; i++) {
	loadImage("images/bowl-" + i + ".jpg");
	if (i < 7)
		loadImage("images/pit-" + i + ".jpg");
}

function loadImage(name) {
	img = new Image();
	img.src = name;
}

function loadBoard() {
	for (i = 1; i < 15; i++) {
		if (i == 7 || i == 14)
			setPitValue(i, 0);
		else  {
			setPitValue(i, 4);
			if (i < 7) {
				$("pit_" + i).onclick = function() {
					playerMove(parseInt(this.id.split("_")[1]), 1);
				};
			} else {
				$("pit_" + i).onclick = function() {
					playerMove(parseInt(this.id.split("_")[1]), 2);
				};
			}
		}
	}
	player1_turn = true;
	updateStatus("Player 1, choose a pit to begin.");
}

function gameOver() {
	p1 = getPitValue(7);
	p2 = getPitValue(14);
	if (p1 < p2)
		text = "Game Over. Player 2 wins " + p1 + "-" + p2 + "!";
	else if (p1 > p2)
		text = "Game Over. Player 1 wins " + p1 + "-" + p2 + "!";
	else
		text = "Game Over. It was a draw!";
	updateStatus(text);
	setTimeout("loadBoard()", 5000);
}

function checkEnd() {
	if (getPitValue(1) == 0 && getPitValue(2) == 0 && getPitValue(3) == 0 && getPitValue(4) == 0 && getPitValue(5) == 0 && getPitValue(6) == 0) {
		setPitValue(7, getPitValue(7) + getPitValue(8) + getPitValue(9) + getPitValue(10) + getPitValue(11) + getPitValue(12) + getPitValue(13));
		gameOver();
		return true;
	}
	
	if (getPitValue(8) == 0 && getPitValue(9) == 0 && getPitValue(10) == 0 && getPitValue(11) == 0 && getPitValue(12) == 0 && getPitValue(13) == 0) {
		setPitValue(14, getPitValue(14) + getPitValue(1) + getPitValue(2) + getPitValue(3) + getPitValue(4) + getPitValue(5) + getPitValue(6));
		gameOver();
		return true;
	}
	
	return false;
}

function getBestMove() {
	// If we can get a free move
	for (i = 13; i > 7; i--)
		if (getPitValue(i) == 14 - i)
			return i;
	// If we can take their stones
	/*
	for (i = 12; i > 7; i--) {
		pit_value = getPitValue(i);
		if (i + pit_value < 14 || i + pit_value > 21) {
			pit = i + pit_value;
			while (pit > 14)
				pit -= 14;
			if (getPitValue(pit) == 0)
				if (getPitValue(14 - pit) > 0)
					return i
		}
	}
	*/
	// Otherwise, just do something
	for (i = 13; i > 7; i--)
		if (getPitValue(i) > 0)
			return i;
	return 13;
}

function computerMove(id) {
	max = doMove(id, 14);
	if (!checkEnd()) {
		if (max != 14) {
			player1_turn = true;
			updateStatus("It's your turn.");
		} else {
			updateStatus("Computer's going again.");
			setTimeout("computerMove(" + getBestMove() + ")", 1000);
		}
	}
}

function playerMove(id, player) {
	if ((!player1_turn && player == 1) || (player1_turn && player == 2) || getPitValue(id) == 0)
		return;
	bowl = (player == 1) ? 7 : 14;
	max = doMove(id, bowl);
	if (!checkEnd()) {
		if (max != bowl) {
			player1_turn = !player1_turn;
			if ($("numplayers").value == 1) {
				bestmove = getBestMove();
				updateStatus("Computer's turn...");
				setTimeout("computerMove(" + bestmove + ")", 1500);
			} else {
				updateStatus(player1_turn ? "Player 1, it's your turn." : "Player 2, it's your turn.");
			}
		} else {
			updateStatus("It's your turn still.");
		}
	}
}

function doMove(id, bowl) {
	otherbowl = (bowl == 7) ? 14 : 7;
	max = id + getPitValue(id);
	setPitValue(id, 0);
	for (i = id + 1; i <= max; i++) {
		if (i == otherbowl || i == otherbowl + 14) {
			i++;
			max++;
		}
		setPitValue(i, getPitValue(i) + 1);
	}
	if (max != 7 && max != 14 && onbowlside(bowl, max)) {
		if (getPitValue(max) == 1 && getPitValue(14 - max) > 0) {
			setPitValue(max, 0);
			setPitValue(bowl, getPitValue(bowl) + getPitValue(14 - max) + 1);
			setPitValue(14 - max, 0);
		}
	}
	return max;
}

function onbowlside(bowl, id) {
	while (id > 14)
		id -= 14;
	if (bowl == 7)
		return player1_turn && id < 7;
	else
		return !player1_turn && id > 7;
}

function updateStatus(msg) {
	setBGColor("status", "#ff0");
	$("status").innerHTML = msg;
	setTimeout("setBGColor('status', '#eee')", 500);
}

function setBGColor(id, color) {
	$(id).style.backgroundColor = color;
}

function setColor(id, color) {
	$(id).style.color = color;
}

function setPitValue(id, value) {
	while (id > 14)
		id -= 14;
	margin = (id == 7 || id == 14) ? '45px' : '13px';
	$("pit_" + id).innerHTML = '<p style="font-size: 9pt; text-align: left; margin: 0; color: #ff0" id="pit_p_' + id + '">' + value + '</p>';
	//$("pit_" + id).innerHTML = '<p style="margin: 0; text-align: left; font-size: 8pt; color: #ff0" id="pit_p_' + id + '"><span style=" background-color: #000; padding: 2px;">' + value + '</span></p>';
	if (id == 7 || id == 14) {
		img = (value < 8) ? "bowl-" + value + ".jpg" : "bowl-8.jpg";
		$("pit_" + id).style.backgroundImage = "url(images/" + img + ")";
	} else {
		img = (value < 5) ? "pit-" + value + ".jpg" : "pit-6.jpg";
		$("pit_" + id).style.backgroundImage = "url(images/" + img + ")";
	}
	setTimeout("setColor('pit_p_" + id + "', '#fff')", 400);
}

function getPitValue(id) {
	while (id > 14)
		id -= 14;
	return parseInt(stripHTML($("pit_" + id).innerHTML));
}

function $(id) {
	return document.getElementById(id);
}

function stripHTML(str) {
	return str.replace(/<\/?[^>]+(>|$)/g, "");
}

window.onload = loadBoard;
