var high_score = get_cookie("high_score");
if (high_score == null || high_score == "")
	high_score = 0;
window.onload = updHigh;

pre = (new Image()).src = "images/ul_d.gif";
pre = (new Image()).src = "images/ur_d.gif";
pre = (new Image()).src = "images/rl_d.gif";
pre = (new Image()).src = "images/rr_d.gif";

var press_time = .5;
var move;
var comp_moves;
var comp_button;
var player_button;
var player_turn;
var len;

function start_game() {
	player_turn = false;
	comp_moves = [];
	comp_button = null;
	player_button = null;
	move = 0;
	len = 0;
	update_status("Starting game...pay attention!");
	setTimeout("comp_turn(" + move + ")", 1000);
}

function comp_turn(i) {
	if (i > len) {
		move = 0;
		player_turn = true;
		update_status("It's your turn!");
	} else {
		update_status("Its Simon's turn...");
		if (i == len)
			comp_moves[i] = new_move(Math.floor(Math.random() * 4) + 1);
		comp_button = comp_moves[i];
		press_button_c();
	}
}

function player_move(str) {
	if (!player_turn)
		return;
	player_button = str;
	press_button_h();
	comp_button = comp_moves[move];
	move++;
	if (comp_button != player_button)
		lost_game();
	if (move > len) {
		player_turn = false;
		move = 0;
		len++;
		update_status("Great job! Simon's thinking...");
		setTimeout("comp_turn(" + move + ")", 800);
	}
}

function press_button_c() {
	$(comp_button).src = "images/" + comp_button + "_d.gif";
	setTimeout("depress_button_c()", press_time * 1500);
}

function depress_button_c() {
	$(comp_button).src = "images/" + comp_button + ".gif";
	setTimeout("comp_turn(" + ++move + ")", 600);
}

function press_button_h() {
	window.focus();
	$(player_button).src = "images/" + player_button + "_d.gif";
	setTimeout("depress_button_h()", press_time * 1000);
}

function depress_button_h() {
	$(player_button).src = "images/" + player_button + ".gif";
}

function new_move(num) {
	switch (num) {
		case 1: return "ul";
		case 2: return "ur";
		case 3: return "dl";
		case 4: return "dr";
	}
}

function update_status(msg) {
	$("status").innerHTML = msg;
}

function lost_game() {
	update_status("Game over. You made it through " + len + " rounds!");
	updateHighScore(len);
	move = 0;
	len = 0;
}

function updHigh() {
	updateHighScore(high_score);
}

function updateHighScore(num) {
	if (num > parseInt($("high_score").innerHTML)) {
		$("high_score").innerHTML = num;
		set_cookie("high_score", num, 365);
	}
}

function set_cookie(c_name, value, expiredays)	{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ";path=/" + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function get_cookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) { 
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return '';
}

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