 
   showPane:(pane)->
 
-    lastOpenPaneIndex = @getPaneIndex @getActivePane()
-
     # this is to hide stale static tabs
     @$("> .kdtabpaneview").removeClass "active"
     @$("> .kdtabpaneview").addClass "kdhiddentab"
 
     super pane
 
-    # FIXME: SY
-    # paneMainView = pane.getMainView()
-    # if paneMainView.data?.constructor.name is 'FSFile'
-    #   @getSingleton('mainController').emit "SelectedFileChanged", paneMainView
-
     @emit "MainTabPaneShown", pane
 
     return pane
 
   removePane: (pane) ->
+    # we don't want to use ::showPane
+    # to show the previousPane when a pane
+    # is removed, that's why we override it to use
+    # kodingrouter
     pane.emit "KDTabPaneDestroy"
-    index        = @getPaneIndex pane
+    index = @getPaneIndex pane
     isActivePane = @getActivePane() is pane
-    @panes.splice(index,1)
+    @panes.splice index, 1
     pane.destroy()
     handle = @getHandleByIndex index
-    @handles.splice(index,1)
+    @handles.splice index, 1
     handle.destroy()
+    @emit "PaneRemoved"
+    if isActivePane
+      if prevPane = @getPaneByIndex @lastOpenPaneIndex
+        appInstance = @appManager.getByView prevPane.mainView
+        @appManager.showInstance appInstance
+      else
+        @router.back()
 
-    appPanes = []
-    for pane in @panes
-      appPanes.push pane if pane.options.type is "application"
-
-    if isActivePane
-      if @getPaneByIndex(lastOpenPaneIndex)?
-        @showPane @getPaneByIndex(lastOpenPaneIndex)
-      else if firstPane = @getPaneByIndex 0
-        @showPane firstPane
-
-    @emit "PaneRemoved"
-
-    if appPanes.length is 0
-      @emit "AllApplicationPanesClosed"
 
   createTabPane:(options = {}, mainView)->
 

    if pane.getOption("behavior") is "application"
      mainView.setClass 'application-page'
    pane.setMainView mainView
    mainView.on "KDObjectWillBeDestroyed", =>
      @removePane pane

  rearrangeVisibleHandlesArray:->
    @visibleHandles = []

        @showHandleContainer()


  handleClicked:(index, event)->
    pane        = @getPaneByIndex index
    appView     = pane.getMainView()
    appInstance = @appManager.getByView appView
    options     = appInstance.getOptions()

    if $(event.target).hasClass "close-tab"
      @appManager.quit appInstance
      return no
    else
      @appManager.showInstance appInstance

  showHandleContainer:->
    @tabHandleContainer.$().css top : -25
