var roll_dice_obj = $("roll_dice");
var roll_img = $("roll_img")
var die_1 = $("die_1");
var die_2 = $("die_2");
var die_1_value;
var die_2_value;
var your_turn;
var current_total;
var used;
var game_over;
var highscore;
var submit_score_obj = $("submit_score");
var won_game = false;

window.onload = new_game;

function new_game() {
	submit_score_obj.style.display = "none";
	highscore = 0;
	die_1_value = 0;
	die_2_value = 0;
	your_turn = false;
	current_total = 0;
	used = [0,0,0,0,0,0,0,0,0];
	game_over = false;
	won_game = false;
	for (i = 1; i <= 9; i++) {
		$("used_" + i).style.display = "none";
		$("block_" + i).style.display = "";
	}
	roll_dice_obj.style.display = "";
}

function flip_block(obj) {
	if (!your_turn)
		return;
	value = obj.innerHTML * 1;
	if (obj.style.display == "none" || current_total + value > die_1_value + die_2_value)
		return;
	used[value - 1] = 1;
	current_total += value;
	obj.style.display = "none";
	$("used_" + value).style.display = "";
	setTimeout("check_moves()", 500);
}

function roll_dice() {
	if (game_over)
		new_game();
	roll_dice_obj.style.display = "none";
	roll_img.src = "images/roll.png";
	animate_dice();
	die_1_value = rand(1, 6);
	die_2_value = rand(1, 6);
	setTimeout("update_die_1(" + die_1_value + ")", 2000);
	setTimeout("update_die_2(" + die_2_value + ")", 2000);
	setTimeout("update_your_turn()", 2000);
	setTimeout("check_moves()", 2000);
}

function animate_dice() {
	for (i = 1; i <= 10; i++) {
		setTimeout("update_die_1(" + rand(1, 6) + ")", 200 * i);
		setTimeout("update_die_2(" + rand(1, 6) + ")", 200 * i);
	}
}

function won_game() {
	highscore = get_highscore();
	submit_score_obj.style.display = "";
	game_over = true;
	roll_img.src = "images/win.png";
	roll_dice_obj.style.display = "";
	won_game = true;
}

function lost_game() {
	highscore = get_highscore();
	submit_score_obj.style.display = "";
	game_over = true;
	roll_img.src = "images/lose.png";
	roll_dice_obj.style.display = "";
}

function get_highscore() {
	score = 0;
	for (i = 0; i < used.length; i++)
		if (used[i] == 0)
			score += i + 1;
	return score + die_1_value + die_2_value - current_total;
}

function check_moves() {
	amt_left = die_1_value + die_2_value - current_total;
	won = true;
	for (i = 0; i < used.length; i++)
		if (used[i] == 0)
			won = false;
	if (amt_left == 0 && won) {
		won_game();
		return;
	}
	if (amt_left == 0) {
		current_total = 0;
		your_turn = false;
		roll_dice_obj.style.display = "";
		return;
	}
	can_go = false;
	for (i = (amt_left > 8 ? 8 : amt_left - 1); i >= 0; i--)
		if (used[i] == 0)
			can_go = true;
	if (!can_go)
		lost_game();
}

function update_your_turn() {
	your_turn = !your_turn;
}

function update_die_1(num) {
	die_1.style.backgroundImage = "url(images/die" + num + ".png)";
}

function update_die_2(num) {
	die_2.style.backgroundImage = "url(images/die" + num + ".png)";
}

function rand(low, high) {
	return Math.floor(Math.random() * high) + low;
}

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