    var settingsCookie = new Cookie("MontySettings");
    var stage = 0;
    var marked = 0;
    var carNr = 0;
    var revealed = 0;
    var stayedCount = 0;
    var switchedCount = 0;
    var switchWinCount = 0;
    var stayWinCount = 0;
    var doorWins = [0, 0, 0];
    var isAutomatic = false;
    
    function setup() {
      orientationChanged();
    }
    
    function showGame() {
      $("Content").style.display = "block";
      $("Help").style.display = "none";
      $("FavIcon").style.display = "none";
      $("BackButton").style.display = "inline";
      window.scrollTo(0, 0);
      startGame();
    }
    
    function hideGame() {
      $("Content").style.display = "none";
      $("Help").style.display = "block";      
      $("FavIcon").style.display = "inline";
      $("BackButton").style.display = "none";
      window.scrollTo(0, 0);
    }
    
    function toggleAutoGame() {
      isAutomatic = !isAutomatic;
      if (isAutomatic) {
        startGame();
        $("AutoButton").innerHTML = "Stop";
        $("StartButton").style.display = "none";
      }
      else {
        $("Buttons").style.display = "none";
        window.setTimeout(function() {
          startGame();
          $("AutoButton").innerHTML = "Start Simulation";
          $("StartButton").style.display = "";
        }, 1000);                  
      }  
    }
    
    function startGame() {
      stage = 1;
      for ( var i = 0; i < 3; i++) {
        $("Curtain" + i).src = "curtain2.png";        
        $("Curtain" + i).style.opacity = "1.0";        
      }
      $("Hint").innerHTML = "Closing Doors...";
      if (!isAutomatic) {
        $("Buttons").style.display = "none";
      }  
      window.setTimeout(function() {
        stage = 0;
        for ( var i = 0; i < 3; i++) {
          $("Goat" + i).src = "goat.png";        
        }        
        carNr = Math.floor(Math.random() * 3);
        $("Goat" + carNr).src = "car2.png";
        $("Hint").innerHTML = "Select a Door";
        if (isAutomatic) {
          doorClicked(Math.floor(Math.random() * 3));
        }  
      }, 1000);  
    }
    
    function clicked(nr) {
      if (isAutomatic) {
        return;
      }  
      doorClicked(nr);
    }
    
    function doorClicked(nr) {  
      switch (stage) {
      case 0:
        $("Curtain" + nr).src = "curtain3.png";
        if (!isAutomatic) {
          selectSound();
        }  
        marked = nr;
        $("Hint").innerHTML = "";
        stage = 1;
        $("Hint").innerHTML = "Monty opens one door...";
        window.setTimeout(function() {
          reveal();
          stage = 2;
          if (isAutomatic) {
            doorClicked(Math.floor(Math.random() * 3));
          }  
        }, 1000);  
        break;
      case 2:
        if (nr == revealed) {
          $("Hint").innerHTML = "Select a closed door";
          if (isAutomatic) {
            doorClicked(Math.floor(Math.random() * 3));
          }
          else {
            errorSound();
          }  
          return;
        }
        $("Curtain" + nr).style.opacity = "0.0";
        var win = nr == carNr;
        var switched = nr != marked;
        gameOver(win, switched);
        stage = 3;
        if (isAutomatic) {
          window.setTimeout(function() {
            if (isAutomatic) {
              startGame();
            }  
          }, 1000);
        }  
      }
    }
    
    function reveal() {
      var nr = carNr;
      while (nr == carNr || nr == marked) {
        nr = Math.floor(Math.random() * 3);
      }
      revealed = nr;
      $("Curtain" + nr).style.opacity = "0.0";
      $("Hint").innerHTML = "Tap Door to <b>Switch</b> or <b>Stay</b>";
      if (!isAutomatic) {
        montySound();
      }  
    }
    
    function gameOver(win, switched) {
      var text = "You";
      if (switched) {
        switchedCount++;
        if (win) {
          switchWinCount++;
        }  
        text += " switched";
      }  
      else {
        stayedCount++;
        if (win) {
          stayWinCount++;
        }  
        text += " stayed";
      }  
      if (win) {
        text += " and won!";
        if (!isAutomatic) {
          carSound();
        }  
      }  
      else {
        text += " and lost";
        if (!isAutomatic) {
          goatSound();
        }  
      }
      doorWins[carNr]++;
      $("Hint").innerHTML = text;
      updateStats();
      $("Buttons").style.display = "block";
    }
    
    function updateStats() {
      $("StayWins").innerHTML = stayWinCount;
      $("SwitchWins").innerHTML = switchWinCount;
      $("StayTotal").innerHTML = stayedCount;
      $("SwitchTotal").innerHTML = switchedCount;
      if (stayedCount > 0) {
        $("StayPercent").innerHTML = Math.round(stayWinCount / stayedCount * 100) + "%";
      }
      else {
        $("StayPercent").innerHTML = "";
      }  
      if (switchedCount > 0) {
        $("SwitchPercent").innerHTML = Math.round(switchWinCount / switchedCount * 100) + "%";
      }  
      else {
        $("SwitchPercent").innerHTML = "";
      }
      var total = stayedCount + switchedCount;
      $("Total").innerHTML = total;
      $("TotalWins").innerHTML = stayWinCount + switchWinCount;
      //$("TotalLost").innerHTML = stayedCount - stayWinCount + switchedCount - switchWinCount;
      if (stayedCount + switchedCount > 0) {
        $("WinPercent").innerHTML = Math.round((stayWinCount + switchWinCount) / (total) * 100) + "%";
        //$("LostPercent").innerHTML = 100 - Math.round((stayWinCount + switchWinCount) / (stayedCount + switchedCount) * 100) + "%";
      }
      else {
        $("WinPercent").innerHTML = "";
        //$("LostPercent").innerHTML = "";
      }
      for ( var i = 0; i < doorWins.length; i++) {
        $("Door" + i + "Wins").innerHTML = doorWins[i];
        if (total > 0) {
          $("Door" + i + "Percent").innerHTML = Math.round(doorWins[i] / total * 100) + "%";
        }  
        else {
          $("Door" + i + "Percent").innerHTML = "";
        }
      }
    }
    
    function reset() {
      stayWinCount = 0;
      switchWinCount = 0;
      stayedCount = 0;
      switchedCount = 0;
      for ( var i = 0; i < doorWins.length; i++) {
        doorWins[i] = 0;
      }
      updateStats();
    }
    
    function tellFriend() {
      var body = "Hi,<br><br>I just stumbled upon this iPhone week number application:" +
          "<br><br>http://weeknumber.speedymarks.com<br><br>" +
          "Calculates the number of the week for any date in the past and the future." +
          "<br><br>Best regards";
      window.open("mailto:?subject=Week number on the iPhone&body=" + body, "_self");  
    }
  
		function $(id) {
		  return document.getElementById(id);
		}
    
  	function orientationChanged() {
      if (window.orientation != undefined) {
        landscape = window.orientation != 0 && window.orientation != 180;
      }
      else {
        landscape = window.innerWidth > window.innerHeight;
      }  
      setTimeout(function() {window.scrollTo(0,1)}, 1);
    }
	
    function debug(msg) {
      var e = $("Debug");
      e.innerHTML += msg + "<br>";
    }
