
var doors = $(".monteHallDoor");

function playMonteHall(counter,strategy) {
	doors.find("img").attr("src","images/monte-hall-barndoor.png");
	doors.css("background-image","url(images/monte-hall-goat.jpg)").removeAttr("prize").removeAttr("chosen").removeAttr("host");

	//Now set one to the prize
	var randChoice = Math.floor(Math.random()*3);
	doors.filter(":eq("+randChoice+")").attr("prize",1).css("background-image","url(images/monte-hall-modelT.png)");
	//alert(doors.filter(":eq("+randChoice+")").length);
	
	if (counter && counter > 0) { 
		var door1 = $(".monteHallDoor:eq(0)");
		door1.trigger("click");
		if (strategy == "hold") {door1.trigger("click");}
		else {doors.filter(":not([chosen],[host])").trigger("click")};//There should only be 1
		setTimeout('playMonteHall('+(counter-1)+',"'+strategy+'")',50);
	}
	
}

resetMonteHall();

function resetMonteHall() {
	$("#monteHallGames,#monteHallWon,#monteHallLost").text("0");
	playMonteHall();
}

$("#monteHallReset").bind("click",resetMonteHall);
$("#monteHallPlay").bind("click",playMonteHall);

$("#monteHallHold").bind("click",function() {playMonteHall(50,"hold")});
$("#monteHallSwitch").bind("click",function() {playMonteHall(50,"switch")});

$(".monteHallDoor").bind("click", function() {

	var choice = $(this).find("img");
	//alert($(this).css("background-image"));
	var src = choice.attr("src").split("/").pop();
	var chosen = doors.filter("[chosen]");
	var finalChoice=0;
	
	switch (src) {
		case "monte-hall-barndoor.png":	//User chose unopened door.
			if (chosen.length==0) {//This is the first door user has chosen
				$(this).attr("chosen",1);//Mark it as chosen
				choice.attr("src","images/monte-hall-barndooropening.png");
				//Now Host chooses a goat door from remaining 2 doors
				var remainingGoatDoors = doors.filter(":not([chosen],[prize])");
				var randChoice = Math.floor(Math.random()*remainingGoatDoors.length);
				remainingGoatDoors.filter(":eq("+randChoice+")").attr("host",1).find("img").attr("src","images/monte-hall-barndooropen.png");
			}
			else {//Final choice, open door
				finalChoice=1;
			}
			break;
		case "monte-hall-barndooropening.png":	//User chose previously opened door
			finalChoice=1;
			break;
			
		case "monte-hall-barndooropen.png"://User chose door opened by host, so do nothing.
			//If all 3 doors already open, then play game again
			if (doors.filter("[chosen=2]").length==1) {playMonteHall()};
			break;

	}

	if (finalChoice) {
		$(this).attr("chosen",2);
		doors.find("img").attr("src","images/monte-hall-barndooropen.png");//opens remaining doors		

		var games = $("#monteHallGames");
		var gamesTotal = parseInt(games.text())+1;
		games.text(gamesTotal);
		var won = $("#monteHallWon");
		var wonTotal = parseInt(won.text());
		var lost = $("#monteHallLost");
		var lostTotal = parseInt(lost.text());
		if ($(this).attr("prize")) {wonTotal++}
		else {lostTotal++}
		won.text(wonTotal + " - " + parseInt(wonTotal/gamesTotal*100) + "%");
		lost.text(lostTotal  + " - " + parseInt(lostTotal/gamesTotal*100) + "%");
	}
})
