   # Used to e.g. implement a redirect.
   REPLACE_URL: "REPLACE_URL"
 
-# Stores the initial length of the browser history. Used to figure out if
-# invoking noughts.state.back() will take us off-site.
-noughts.state.initialHistoryLength = window.history.length
-
 # Returns true if there's a previous state in the state history to go back to.
-noughts.state.hasPreviousState = ->
-  window.history.length > noughts.state.initialHistoryLength
+noughts.state.hasPreviousState = -> return noughts.state.stateHistory.length > 1
 
 # Possibly modifies the current browser URL to the to provided path, based on
 # the behavior requested in the 'urlBehavior' parameter (a
 # noughts.state.UrlBehavior).
 noughts.state.updateUrl = (urlBehavior, path) ->
-  # TODO(dthurn): No point in doing this if we're inside the Facebook iframe
+  # No point in doing this if we're inside an iframe:
+  return if noughts.inIframe()
   if urlBehavior == noughts.state.UrlBehavior.PUSH_URL
     window.history.pushState({}, "", path)
   else if urlBehavior == noughts.state.UrlBehavior.REPLACE_URL
     window.history.replaceState({}, "", path)
   # state.UrlBehavior.PRESERVE_URL is a no-op.
 
-# Navigates back in the browser history, but throws an error if the navigation
-# would take you off-site.
-noughts.state.back = ->
-  unless noughts.state.hasPreviousState()
-    displayError("Tried to invoke state.back() with no more states available")
-  # TODO(dthurn): Make this work inside the Facebook iframe.
-  window.history.back()
-
 # Displays a short message to the user. The optional duration parameter controls
 # how many milliseconds the message appears for.
 noughts.displayToast = (text, duration) ->
+  duration ||= 3000
   $(".nToast").text(text)
   $(".nToast").css({top: 0})
-  duration ||= 3000
   setTimeout((-> $(".nToast").css({top: "-5rem"})), duration)
 
 # Displays a modal dialog over the game.

  FACEBOOK_INVITE: "FACEBOOK_INVITE" # Game state for showing new game menu
  LOADING: "LOADING" # Game loading state

# Changes the current game state to 'newState'. The optional "urlBehavior"
# parameter shoud be a noughts.state.UrlBehavior, and the browser URL will be
# modified accordingly. The default urlBehavior is noughts.state.UrlBehavior.
# PUSH_URL. Any additional arguments are also past through to the onEnterState
# of the new state.
noughts.state.changeState = (newState, urlBehavior) ->
  # TODO(dthurn): Handle a "cancel button" type of state transition which
  # takes the current state out of history.
  if me.state.currentStateConstant() != -1 and me.state.current().onExitState?
    me.state.current().onExitState()
  urlBehavior ||= noughts.state.UrlBehavior.PUSH_URL
  Session.set("state", newState)
  newScreen = noughts.state.stateMap[newState]
  # Arguments should be [urlBehavior, args[2], args[3], etc]
  newArgs = [urlBehavior].concat(_.toArray(arguments).slice(2))
  newScreen.enterState.apply(newScreen, newArgs)

# What a new state should do to the browser URL when entered.
noughts.state.UrlBehavior =


SPRITE_Z_INDEX = 2 # The Z-Index to add new sprites at

noughts.inIframe = -> return window != window.top

noughts.state =
  PLAY: "PLAY"
  INITIAL_PROMO: "INITIAL_PROMO" # Initial game description state
