jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages("images/left_paper.jpg", "images/right_paper.jpg", "images/left_scissors.jpg", "images/right_scissors.jpg", "images/left_stone.jpg", "images/right_stone.jpg");

function startGame(){
	
	game = new Object();
	
	game.countdown = 3;
	game.players = 1;
	
	game.status = "active";
	game.timerActive = "active";
	
	game.player1 = new Object();
	game.player2 = new Object();
	
	game.player1.score = 0;
	game.player2.score = 0;
	
	game.player1.choice = "stone";
	game.player2.choice = "stone";
	
	deselectAll();
}

///////////////////////////

function newGame(){
	$('#playagain').fadeOut("fast");
	
	$('#player1 .player_choice').removeClass(game.player1.choice);
	$('#player2 .player_choice').removeClass(game.player2.choice);
	
	game.player1.choice = "stone";
	game.player2.choice = "stone";
	
	$('#timer').html("3");
	game.status = "active";
	
	deselectAll();
	$('#player1 .'+game.player1.choice).fadeTo("fast", 1);
	$('#player1 .player_controls').fadeIn("fast");
	
	startTimer();
}

///////////////////////////

function deselectAll(){
	$('.option').fadeTo("fast", 0.5);
}

///////////////////////////

function makeChoice(thechoice){
	deselectAll();
	$('#player1 .'+thechoice).fadeTo("fast", 1);
	
	game.player1.choice = thechoice;
}

///////////////////////////

function winLoseDraw(theState){
	
	var gameStatus = "";
	
	if(theState == "Win"){
		gameStatus = "Win";
		game.player1.score = game.player1.score +1;
	}else if(theState == "Lose"){
		gameStatus = "Lose";
		game.player2.score = game.player2.score +1;
	}else if(theState == "Draw"){
		gameStatus = "Draw";
		game.player1.score = game.player1.score +1;
		game.player2.score = game.player2.score +1;
	}
	
	$('#timer').html("You "+gameStatus);
	
	$('.player1_score').html(game.player1.score);
	$('.player2_score').html(game.player2.score);
	
	window.setTimeout(function() {
		$('#playagain').fadeIn("fast");
	}, 1000);
	
	$('#playagain').click(function() {
		if(game.status == "inactive"){
			game.status = "active";
			newGame();
		}
		
	});
}

///////////////////////////

function calculateWinner(){
	var computerChoice = new Array();
	computerChoice[0] = "paper";
	computerChoice[1] = "scissors";
	computerChoice[2] = "stone";
	
	$('.player_controls').fadeOut("fast");
	
	var randomPic = Math.floor(Math.random()*3);
	game.player2.choice	= computerChoice[randomPic];
	
	$('#player1 .player_choice').addClass(game.player1.choice);
	$('#player2 .player_choice').addClass(game.player2.choice);
	
	if(game.player1.choice == "paper" && game.player2.choice == "paper"){
		winLoseDraw("Draw");
	}else if(game.player1.choice == "paper" && game.player2.choice == "scissors"){
		winLoseDraw("Lose");
	}else if(game.player1.choice == "paper" && game.player2.choice == "stone"){
		winLoseDraw("Win");
	}else if(game.player1.choice == "scissors" && game.player2.choice == "paper"){
		winLoseDraw("Win");
	}else if(game.player1.choice == "scissors" && game.player2.choice == "scissors"){
		winLoseDraw("Draw");
	}else if(game.player1.choice == "scissors" && game.player2.choice == "stone"){
		winLoseDraw("Lose");
	}else if(game.player1.choice == "stone" && game.player2.choice == "paper"){
		winLoseDraw("Lose");
	}else if(game.player1.choice == "stone" && game.player2.choice == "scissors"){
		winLoseDraw("Win");
	}else if(game.player1.choice == "stone" && game.player2.choice == "stone"){
		winLoseDraw("Draw");
	}
}

///////////////////////////

function startTimer(){
	if(game.timerActive == "active"){
		game.timerActive = "inactive";
		$('#windowBlind').fadeOut("fast");
		$('#windowBlind').css("display", "none");
		
		$('#timer').html("3");
		
		window.setTimeout(function() {
			$('#timer').html("2");
		}, 2000);
		
		window.setTimeout(function() {
			$('#timer').html("1");
		}, 4000);
		
		window.setTimeout(function() {
			$('#timer').html("0");
			calculateWinner();
			game.timerActive = "active";
			game.status = "inactive";
		}, 6000);
	}
}

///////////////////////////

$(document).ready(function() {
	
	startGame();	
	
	$('#playagain').hide();
	$('#player1 .'+game.player1.choice).fadeTo("fast", 1);

	$('#player1 .paper').click(function() {
		makeChoice("paper");
	});

	$('#player1 .scissors').click(function() {
		makeChoice("scissors");
	});
	
	$('#player1 .stone').click(function() {
		makeChoice("stone");
	});
	
	$('#windowBlind').click(function() {
		startTimer();
	});
	
	$("select#othergames").change(function () {
		var selectValue = $("#othergames option:selected").attr("value");
		window.location.href = "http://jedidiah.eu/"+selectValue+"/";
	});
	
});