 
     page.render() for page in pagesToRender
     page.remove() for page in pagesToRemove
+
+    return # To prevent CoffeScript returning something

    #@_showSegments()
    #@_showTextSegments()

  _distance: (position, area) =>
    distanceXLeft = position.left - area.left
    distanceXRight = position.left - (area.left + area.width)

    distanceYTop = position.top - area.top
    distanceYBottom = position.top - (area.top + area.height)

    distanceX = if Math.abs(distanceXLeft) < Math.abs(distanceXRight) then distanceXLeft else distanceXRight
    if position.left > area.left and position.left < area.left + area.width
      distanceX = 0

    distanceY = if Math.abs(distanceYTop) < Math.abs(distanceYBottom) then distanceYTop else distanceYBottom
    if position.top > area.top and position.top < area.top + area.height
      distanceY = 0

    distanceX * distanceX + distanceY * distanceY

  _findClosestSegment: (position) =>
    closestSegmentIndex = -1
    closestDistance = Number.MAX_VALUE

    for segment, i in @textSegments
      distance = @_distance position, segment.boundingBox
      if distance < closestDistance
        closestSegmentIndex = i
        closestDistance = distance

    [closestSegmentIndex, Math.sqrt(closestDistance)]

  _findClosestSegmentFromEvent: (e) =>
    $canvas = @$displayPage.find('canvas')

    offset = $canvas.offset()
    left = e.pageX - offset.left
    top = e.pageY - offset.top
    @_findClosestSegment
      left: left
      top: top

  padTextSegment: (e) =>
    [closestSegmentIndex, closestDistance] = @_findClosestSegmentFromEvent e
    $closestSegmentDom = @textSegments[closestSegmentIndex].$domElement
    angle = @textSegments[closestSegmentIndex].angle
    scale = @textSegments[closestSegmentIndex].scale

    padding = closestDistance + 10

    # 2D vector rotation: http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm
    # x' = x cos(f) - y sin(f), y' = x sin(f) + y cos(f)
    # We scale x because we use scaling transformation along x-axis as well
    left = padding * (scale * Math.cos(angle) - Math.sin(angle))
    top = padding * (scale * Math.sin(angle) + Math.cos(angle))

    @$displayPage.find('.text-layer-segment').css
      padding: 0
      margin: 0
    $closestSegmentDom.css
      marginLeft: -left
      marginTop: -top
      padding: padding

  render: =>
    assert @highlightsEnabled

class @Annotator
  constructor: ->
    @_pages = []
    @mouseDown = false

    $(window).on 'scroll.annotator', @checkRender
    $(window).on 'resize.annotator', @checkRender

    $(document).on 'mousedown.annotator', =>
      @mouseDown = true
      return # To prevent CoffeScript returning something
    $(document).on 'mouseup.annotator', =>
      @mouseDown = false
      return # To prevent CoffeScript returning something

  destroy: =>
    $(document).off 'mousedown.annotator'
    $(document).off 'mouseup.annotator'

    $(window).off 'scroll.annotator'
    $(window).off 'resize.annotator'

