     @tabView = new ApplicationTabView
       delegate           : @
       tabHandleContainer : @tabHandleContainer
+
+    @tabView.on "PaneAdded", (pane) =>
+      {tabHandle} = pane
+      tabHandle.on "click", =>
+        newIndex = @tabView.getPaneIndex @tabView.getActivePane()
+        return  if newIndex is @activeTabIndex
+
+        @workspaceRef.child("ActiveTabIndex").set newIndex
+        @activeTabIndex = newIndex
 
   createEditorInstance: (file, content, sessionKey) ->
     if file

    @workspaceRef     = @workspace.firepadRef.child @sessionKey
    @isJoinedASession = @getOptions().sessionKey
    @openedFiles      = []

    log "joined an old session again, creating new tabbed editor" if @isJoinedASession



    pane.addSubView editor
    @tabView.addPane pane
    @activeTabIndex = @tabView.panes.length

    pane.on "KDTabPaneDestroy", =>
      @workspaceRef.once "value", (snapshot) =>
