 
   hide: ->
     @view.classList.add 'dontshow'
+    @emitter.emit 'didHide'
     setTimeout (=> @hide_()), 100
 
   show: ->
     @view.style.display = 'block'
+    @emitter.emit 'didShow'
     setTimeout ( => @view.classList.remove 'dontshow'), 20
 
   destroy: ->
-    document.body.removeChild @view
+    if document.body.contains @view
+      document.body.removeChild @view
+    @parent.onmouseover = @oldOnMouseOver
+    @parent.onmouseout = @oldOnMouseOut
 
   tooltipView: (content) ->
     tt = document.createElement 'div'
     tt.classList.add 'ink-tooltip', 'dontshow'
+    if @clas then tt.classList.add @clas
     tt.style.display = 'none'
     if content then tt.appendChild content
     tt


module.exports =
class Tooltip
  constructor: (@parent, content, {@cond, @delay}={}) ->
    @cond  = (-> true) unless @cond?
    @delay = 150       unless @delay?

    @view = @tooltipView content
    document.body.appendChild @view
    @showOnHover()
    this

  hide_: ->
    @view.classList.add 'dontshow'

    @parent.onmouseout = =>
      clearTimeout showTimer
      hideTimer = setTimeout (=> @hide()), @delay
    @view.onmouseover  = =>
      clearTimeout hideTimer
    @view.onmouseout   = =>
      hideTimer = setTimeout (=> @hide()), @delay

  positionOverlay: ->
    bounding = @parent.getBoundingClientRect()
    @view.style.bottom   = document.documentElement.clientHeight - bounding.top + 'px'
    @view.style.left     = bounding.left   + 'px'
