// Written by Michael Amor Righi
// http://www.michaelrighi.com/2008/10/28/monty-hall-problem

			var numPlaysWithSwitch = 0;
			var numWinsWithSwitch = 0;
			var numPlaysWithoutSwitch = 0;
			var numWinsWithoutSwitch = 0;

			var winningDoor = Math.floor(Math.random()*3) + 1;
			var moveNumber = 1;
			var doorToReveal = 0;
			var doorToSwitchTo = 0;
		
			function resetGame()
			{		
				winningDoor = Math.floor(Math.random()*3) + 1;
				moveNumber = 1;
				doorToReveal = 0;
				doorToSwitchTo = 0;
				updateMontyText("Please, choose a door by clicking on it!");
				resetDoor(1);
				resetDoor(2);
				resetDoor(3);
				deHighlightDoor(1);
				deHighlightDoor(2);
				deHighlightDoor(3);
			}
		
			function doorSelected(chosenDoor)
			{
				if (moveNumber == 1)
				{
					doorToReveal = 1;
					if (doorToReveal == chosenDoor || doorToReveal == winningDoor)
					{
						doorToReveal = 2;
						doorToSwitchTo = 3;
					}

					if (doorToReveal == chosenDoor || doorToReveal == winningDoor)
					{
						doorToReveal = 3;
					}

					doorToSwitchTo = 1;
					if (doorToSwitchTo == chosenDoor || doorToSwitchTo == doorToReveal)
					{
						doorToSwitchTo = 2;
					}
					if (doorToSwitchTo == chosenDoor || doorToSwitchTo == doorToReveal)
					{
						doorToSwitchTo = 3;
					}

					updateMontyText("It's a good thing you didn't pick door number " + doorToReveal 
						+ "!  Would you like to stick with door number " + chosenDoor + 
						" or would you rather switch to door number " + doorToSwitchTo + "?");
					highlightDoor(chosenDoor);

					revealDoor(doorToReveal,false);
					moveNumber = 2;
				}
				else if (moveNumber == 2)
				{
					if (chosenDoor == doorToReveal)
					{
						return;
					}
				
				
					highlightDoor(chosenDoor);
					if (chosenDoor == doorToSwitchTo)
					{
						numPlaysWithSwitch++;
						if (chosenDoor == winningDoor)
						{
							numWinsWithSwitch++;
						}
					}
					else
					{
						numPlaysWithoutSwitch++;
						if (chosenDoor == winningDoor)
						{
							numWinsWithoutSwitch++;
						}						
					}


					if (winningDoor == chosenDoor)
					{
						updateMontyText("Congratulations!  You won!  Click on any door to play the game again.");
					}
					else
					{
						updateMontyText("Sorry, you lose.  The car was behind door number " + winningDoor + ".  Click on any door to play again.");
					}

					updateStatistics();
					

					
					revealDoor(1,1==winningDoor);
					revealDoor(2,2==winningDoor);
					revealDoor(3,3==winningDoor);
					moveNumber = 3;
				}
				else if (moveNumber == 3)
				{
					resetGame();
					moveNumber = 1;
				}
			}
			
			function resetDoor(doorNumber)
			{
				var img = document.getElementById("door" + doorNumber);
				img.src = "/images/monty/door" + doorNumber + "Closed.gif";
			}
			
			function revealDoor(doorToReveal,winning)
			{
				var img = document.getElementById("door" + doorToReveal);
				if (winning)
				{
					img.src = "/images/monty/openDoorCar.gif";
				}
				else
				{
					img.src = "/images/monty/openDoorGoat.gif";
				}
			}
			
			function highlightDoor(doorNum)
			{
				deHighlightDoor(1);
				deHighlightDoor(2);
				deHighlightDoor(3);
				
				var door = document.getElementById("door" + doorNum);
				door.style.border = "2px solid #00F";
			}

			function deHighlightDoor(doorNum)
			{
				var door = document.getElementById("door" + doorNum);
				door.style.border = "2px solid #FFF";			
			}
			
			
			function updateMontyText(msg)
			{
				replaceText("monty",msg);
			}
			
			function updateStatistics()
			{
				var switchPercent = Number(numWinsWithSwitch / numPlaysWithSwitch * 100).toFixed(2);
				if (isNaN(switchPercent))
				{
					switchPercent = "0.00";
				}
				replaceText("switchStats",switchPercent + "% (" + numWinsWithSwitch + "/" + numPlaysWithSwitch + ")");


				var noSwitchPercent = Number(numWinsWithoutSwitch / numPlaysWithoutSwitch * 100).toFixed(2);
				if (isNaN(noSwitchPercent))
				{
					noSwitchPercent = "0.00";
				}

				replaceText("noSwitchStats", noSwitchPercent + "% (" + numWinsWithoutSwitch + "/" + numPlaysWithoutSwitch + ")");
			}
			
			function replaceText(elemId,newText)
			{
				var elem = document.getElementById(elemId);
				elem.removeChild(elem.firstChild);	
				elem.appendChild(document.createTextNode(newText));
			}

