 # input elements. This mechanism allows us to decide whether to scroll a div or to scroll the whole document.
 #
 activatedElement = null
-settings = null
 
 scrollProperties =
   x: {


Scroller =
  init: (frontendSettings) ->
    settings = frontendSettings
    handlerStack.push DOMActivate: -> activatedElement = event.target

  # scroll the active element in :direction by :amount * :factor.
  # :factor is needed because :amount can take on string values, which scrollBy converts to element dimensions.

#     continues scrolling at least until its corresponding keyup event is received.  We never initiate a new
#     animator on keyboard repeat.

CoreScroller =
  init: (frontendSettings) ->
    @settings = frontendSettings
    @time = 0
    @lastEvent = null
    @keyIsDown = false
    handlerStack.push
      keydown: (event) =>
        @keyIsDown = true
        @lastEvent = event
      keyup: =>
        @keyIsDown = false
        @time += 1

  # Scroll element by a relative amount (a number) in some direction.
  scroll: (element, direction, amount) ->
    return unless amount

    unless @settings.get "smoothScroll"
      # Jump scrolling.
      performScroll element, direction, amount
      checkVisibility element
      return

    # We don't activate new animators on keyboard repeats.
    return if @lastEvent?.repeat

    activationTime = ++@time

    isMyKeyStillDown = =>
      @time == activationTime and @keyIsDown

    # Store amount's sign and make amount positive; the logic is clearer when amount is positive.
    sign = Math.sign amount
