    var settingsCookie = null;
		var diceTimer = null;
		var diceSpeed = 0;
		var SPEED = {ACC:0, HOLD:1, BRAKE: 2};
		var speedMode = SPEED.ACC;
		var customLetters = "";
    
    function setup() {
      orientationChanged();
      settingsCookie = new Cookie("DiceSettings");
      if (settingsCookie.get()) {
        try {
          var settings = eval("new Object({" + settingsCookie.get() + "})");
          $("CustomCheck").checked = settings.custom;
          $("CustomLetters").value = settings.letters;
        }
        catch (e) {
          debug("LoadingSettings failed: " + e);
        }  
      }  
      $("DiceSection").onmousedown = startRoll;
      $("DiceSection").onmouseup = stopRoll;
      $("DiceSection").ontouchstart = function(event) {
        event.preventDefault();
        startRoll();
      };  
      $("DiceSection").ontouchend = function(event) {
        if (event.touches.length > 0) {
          return;
        }  
        event.preventDefault();
        stopRoll();
      };  

      customChanged();
      startRoll();
      stopRoll();
      $("RollHint").style.visibility = "visible";
    }
    
    function startRoll() {
      $("RollHint").style.visibility = "hidden";
      setDiceSpeed(200);
      speedMode = SPEED.ACC;
      roll();
    }
    
    function roll() {
      if (diceTimer) {
        window.clearTimeout(diceTimer);
      }
      var ch = String.fromCharCode(65 + Math.random() * 26);
      if (customLetters.length > 0 && $("CustomCheck").checked) {
        ch = customLetters.charAt(Math.random() * customLetters.length);
      }
      $("Dice").innerHTML = ch;
      switch (speedMode) {
      case SPEED.ACC:
        setDiceSpeed(diceSpeed - 25);
        if (diceSpeed < 50) {
          setDiceSpeed(50);
          speedMode = SPEED.HOLD;
        }
        break;
      case SPEED.BRAKE:
        setDiceSpeed(diceSpeed + 25);
        if (diceSpeed > 200) {
          stopRolling();
          return;
        }
        break;
      }  
      diceTimer = setTimeout(roll, diceSpeed);
    }
    
    function stopRoll() {
      setDiceSpeed(50);
      speedMode = SPEED.BRAKE;
    }
    
    function stopRolling() {
      if (diceTimer) {
        window.clearTimeout(diceTimer);
      }      
      setDiceSpeed(200);
      speedMode = SPEED.HOLD;
      diceTimer = null;
      $("RollHistory").innerHTML = $("RollHistory").innerHTML + $("Dice").innerHTML + " ";
    }
    
    function setDiceSpeed(speed) {
      diceSpeed = speed;
      if (speed > 0) {
        $("DiceBackground").style.opacity = (200 - speed) / 200.0;
      }
      else {
        $("DiceBackground").style.opacity = 0.0;
      }  
    }
    
    function clearHistory() {
      $("RollHistory").innerHTML = "&nbsp;";
    }
    
    function showSetup() {
      $("Setup").style.display = "block";
      $("ShowSetup").style.display = "none";
    }
    
    function closeSetup() {
      $("Setup").style.display = "none";
      $("ShowSetup").style.display = "";
    }
    
    function customChanged() {
      var custom = $("CustomCheck").checked;
      if (custom) {
        $("CustomHint").innerHTML = "Roll these letter:";
        $("CustomLetters").disabled = false;
      }
      else {
        $("CustomHint").innerHTML = "Roll A...Z";
        $("CustomLetters").disabled = true;
      }
      changeLetters();
    }
    
    function changeLetters() {
      customLetters = $("CustomLetters").value;
      customLetters = customLetters.replace(/\s/g, "");
      customLetters = customLetters.toUpperCase();
      $("CustomLetters").value = customLetters;
      saveSettings();
    }
    
    function saveSettings() {
      var text = "custom:" + $("CustomCheck").checked;
      text += ",letters:'" + $("CustomLetters").value.replace(/\'/g, "") + "'";
      settingsCookie.store(text);
    }
    
    function tellFriend() {
      var body = "Hi,<br><br>I just stumbled upon this iPhone letter dice application:" +
          "<br><br>http://letterdice.speedymarks.com<br><br>" +
          "Roll the dice for a random letter." +
          "<br><br>Best regards";
      window.open("mailto:?subject=Letter Dice 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>";
    }
