-class IconButton
-  destroy: =>
-    $(window).off 'resize', @_updatePosition
-    clearInterval @_interval
-    @$boundingBox?.remove()
+updateIconButtons = ->
+  FactlinkJailRoot.trigger 'updateIconButtons'
 
-  # TODO: do all updates of different buttons in one pass
-  _updatePositionRegularly: ->
-    $(window).on 'resize', @_updatePosition
-    @_interval = setInterval @_updatePosition, 1000
-    @_updatePosition()
+FactlinkJailRoot.loaded_promise.then ->
+  $(window).on 'resize', updateIconButtons
+  setInterval updateIconButtons, 1000
+  FactlinkJailRoot.on 'factlink.factsLoaded factlinkAdded', updateIconButtons
 
 
-class FactlinkJailRoot.ShowButton extends IconButton
+class FactlinkJailRoot.ShowButton
   content: '<div class="fl-icon-button"><span class="icon-comment"></span></div>'
 
   constructor: (highlightElements, factId) ->

      @$boundingBox = FactlinkJailRoot.drawBoundingBox contentBox, 'red'


class ParagraphButton extends IconButton
  content: '<div class="fl-icon-button"><span class="icon-comment"></span>+</div>'

  constructor: (paragraphElement) ->

    $el.on 'click', @_onClick

    @$paragraph = $(paragraphElement)
    @frame.fadeIn()
    FactlinkJailRoot.on 'updateIconButtons', @_update
    @_update()

  destroy: ->
    @$boundingBox?.remove()
    @_robustHover.destroy()
    @frame.destroy()
    FactlinkJailRoot.off 'updateIconButtons', @_update

  _update: =>
    @_updatePosition()
    @_destroyIfContainsFactlink()

  _destroyIfContainsFactlink: =>
    if @$paragraph.find('.factlink').length > 0
