     })
 
     @showingVisualEditor = true
-    @element.on('click', '.editor-tabs .tab', @onSwitchEditor)
+    @element.on('click', '.editor-tabs .tab', this, @onSwitchEditor)
 
   @setupTinyMCE: (ed) ->
     ed.addButton('wrapAsCode', {

  focusVisualEditor: (visualEditor) ->
    visualEditor.focus()

  getVisualEditor: ->
    ###
    Returns the instance of TinyMCE.
    This is different from the textarea that exists in the HTML template (@tiny_mce_textarea.
    ###
    return tinyMCE.get($('.tiny-mce', this.element).attr('id'))

  save: ->
    @element.off('click', '.editor-tabs .tab', @onSwitchEditor)
    text = @advanced_editor.getValue()
    visualEditor = @getVisualEditor()
    if @showingVisualEditor and visualEditor.isDirty()
      text = visualEditor.getContent({no_events: 1})
    data: text

    e.preventDefault();

    if not $(e.currentTarget).hasClass('current')
      element = e.data.element

      $(e.currentTarget).addClass('current')
      $(element).find('table.mceToolbar').toggleClass(HTMLEditingDescriptor.isInactiveClass)
      $(@advanced_editor.getWrapperElement()).toggleClass(HTMLEditingDescriptor.isInactiveClass)

      visualEditor = @getVisualEditor(element)
      if $(e.currentTarget).attr('data-tab') is 'visual'
        $(element).find('.html-tab').removeClass('current')
        @showVisualEditor(visualEditor)
      else
        $(element).find('.visual-tab').removeClass('current')
        @showAdvancedEditor(visualEditor)

  # Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing.
