 noughts.maybeInitialize = _.after 2, ->
   cacheFacebookData() if Session.get("useFacebook")
   Meteor.subscribe("myGames", onSubscribe)
+
+  $(window).on "popstate", ->
+    setStateFromUrl()
 
 # Inspects the URL and sets the initial game state accordingly.
 setStateFromUrl = ->

        Meteor.call("facebookSetRequestId", gameId, requestId, (err) ->
          if err then throw err
          Session.set("gameId", gameId)
          noughts.state.changeState(noughts.state.PLAY))
    else
      # TODO(dthurn): Display regular invite dialog
      Session.set("gameId", gameId)
      $(".nNewGameModal").modal()
      #noughts.state.changeState(noughts.state.PLAY)

# Initializer to be called after the DOM ready even to set up MelonJS.
initialize = ->

noughts.InitialPromo = me.ScreenObject.extend
  init: ->
    $(".nNewGameButton").on "click", ->
      noughts.state.changeState(noughts.state.NEW_GAME_MENU,
          noughts.UrlBehavior.PUSH_URL)

  onResetEvent: (urlBehavior) ->
    noughts.state.updateUrl("/", urlBehavior)
    $(".nGame").children().hide()
    $(".nGame").css({border: ""})
    $(".nNewGamePromo").show()
