     'surface:frame-changed': "onFrameChanged"
     'level-set-letterbox': 'onSetLetterbox'
     'tome:thang-list-entry-popover-shown': 'onThangListEntryPopoverShown'
+    'surface:coordinates-shown': 'onSurfaceCoordinatesShown'
 
   events:
     'click': 'onClick'


  onMouseEnter: (e) ->
    return unless @controlsEnabled and @spells.length
    @showSpells()

  onMouseLeave: (e) ->
    return unless @controlsEnabled and @spells.length
    clearTimeout @hideSpellsTimeout if @hideSpellsTimeout
    @hideSpellsTimeout = _.delay @hideSpells, 100

  onThangListEntryPopoverShown: (e) ->
    # I couldn't figure out how to get the mouseenter / mouseleave to always work, so this is a fallback

    return if e.entry is @
    @hideSpells()

  onSurfaceCoordinatesShown: (e) ->
    # Definitely aren't hovering over this.
    @hideSpells()

  showSpells: =>
    @clearTimeouts()
    @sortSpells()
    @$el.data('bs.popover').options.content = @getSpellListHTML()
    @$el.popover('setContent').popover('show')
    @$el.parent().parent().parent().i18n()
    @popover = @$el.parent().parent().parent().find('.popover')
    @popover.off 'mouseenter mouseleave'
    @popover.mouseenter (e) => @showSpells() if @controlsEnabled
    @popover.mouseleave (e) => @hideSpells()
    thangID = @thang.id
    @popover.find('code').click (e) ->
      Backbone.Mediator.publish "level-select-sprite", thangID: thangID, spellName: $(@).data 'spell-name'
    Backbone.Mediator.publish 'tome:thang-list-entry-popover-shown', entry: @

  hideSpells: =>
    @clearTimeouts()
    @$el.popover('hide')

  getSpellListHTML: ->
    spellsPopoverTemplate {spells: @spells}
