scroll_handler = (on_scroll_start = (->), on_scroll_end = (->), scroll_end_timeout = 50)->
  now_scrolling = false
  scroll_stopped = true
  last_event = null
  _interval_id = 0

  interval_fn = ->
    if !scroll_stopped and !now_scrolling
      on_scroll_end(last_event)
      clearInterval(_interval_id)
      scroll_stopped = true
    now_scrolling = false

  on_scroll = (e)->
    last_event = e
    if !now_scrolling and scroll_stopped
      on_scroll_start(last_event)
      _interval_id = setInterval(interval_fn, scroll_end_timeout)
    now_scrolling = true
    scroll_stopped = false

  window.addEventListener 'scroll', on_scroll
module.exports = scroll_handler
