 # The "new game" menu
 noughts.NewGameMenu = noughts.Screen.extend
   init: ->
-    $(".nNewGameMenuCloseButton").on "click", =>
-      if noughts.state.hasPreviousState()
-        noughts.state.back(-> setStateFromPath(""))
-      else
-        noughts.state.changeState(noughts.state.INITIAL_PROMO)
-
-    $(".nUrlInviteButton").on("click",
-        _.bind(this.handleUrlInviteButtonClick_, this))
-    $(".nFacebookInviteButton").on("click",
-        _.bind(this.handleFacebookInviteButtonClick_, this))
+    Template.newGameMenu.events
+      "click .nNewGameMenuCloseButton": =>
+        if noughts.state.hasPreviousState()
+          noughts.state.back(-> setStateFromPath(""))
+        else
+          setStateFromPath("")
+      "click .nUrlInviteButton":
+          _.bind(this.handleUrlInviteButtonClick_, this)
+      "click .nFacebookInviteButton":
+          _.bind(this.handleFacebookInviteButtonClick_, this)
 
   onEnterState: (updateUrl) ->
     updateUrl("/new")

    noughts.melonDeferred.resolve()
  me.loader.preload(gameResources)

# Functions to run as soon as possible on startup. Defines the state map
# and adds a melonjs callback.
Meteor.startup ->
  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)


# The initial promo for non-players that explains what's going on.
noughts.InitialPromo = noughts.Screen.extend
  init: ->
    Template.newGamePromo.events
      "click .nNewGameButton": =>
        noughts.state.changeState(noughts.state.NEW_GAME_MENU)

  onEnterState: (updateUrl) ->
    updateUrl("/")
