 # Functions to run as soon as possible on startup. Defines the state map
 # and adds a melonjs callback.
 Meteor.startup ->
-  me.state.set(noughts.state.PLAY, new noughts.PlayScreen())
-  me.state.set(noughts.state.NEW_GAME_MENU, new noughts.NewGameMenu())
-  me.state.set(noughts.state.INITIAL_PROMO, new noughts.InitialPromo())
-  me.state.set(noughts.state.FACEBOOK_INVITE, new noughts.FacebookInviteMenu())
-  me.state.set(noughts.state.LOADING, new noughts.LoadingScreen())
+  noughts.state.stateMap = {
+    PLAY: new noughts.PlayScreen()
+    NEW_GAME_MENU: new noughts.NewGameMenu()
+    INITIAL_PROMO: new noughts.InitialPromo()
+    FACEBOOK_INVITE: new noughts.FacebookInviteMenu()
+    LOADING: new noughts.LoadingScreen()
+  }
+
   noughts.state.changeState(noughts.state.LOADING,
       noughts.state.UrlBehavior.PRESERVE_URL)
+
+  me.state.set(me.state.PLAY, new me.ScreenObject())
+  me.state.change(me.state.PLAY)
 
   window.onReady(-> initialize())
   $.when(noughts.melonDeferred, noughts.facebookDeferred).done ->

# Displays a short informative message to the user.
displayNotice = (msg) -> $(".nNotification").text(msg)

noughts.Screen = me.ScreenObject.extend
  onResetEvent: (urlBehavior) ->
    debugger
    @onEnterState.apply(this, arguments)

noughts.LoadingScreen = me.ScreenObject.extend
  onResetEvent: ->
    $(".nMain").children().hide()
    $(".nScreenLoading").show()


    $(".nMain").children().hide()
    $(".nScreenNewGame").show()

noughts.FacebookInviteMenu = noughts.Screen.extend
  onEnterState: (urlBehavior) ->
    # TODO(dthurn): Log user into facebook here if they aren't yet.
    noughts.state.updateUrl("/facebookInvite", urlBehavior)
    $(".nMain").children().hide()
