###
@mixin
Provides implementation for a controller that can activate
###
prevent = (e) -> e.preventDefault()
class Scroll
	defaults:
		preventPageScrolling: true
		disableResize: true #SHOULD TRY WITH IT ON FALSE
		contentClass: 'scroll-content'
		alwaysVisible: false
		paneClass: 'scroll-pane'

	constructor: (@el, options={}) ->
		@render()

		@callbacks = options.callbacks
		delete options.callbacks

		@$box.nanoScroller @options = $.extend true, {}, @defaults, options
		@bindEvents @callbacks

	render: ->
		$content = $ '<div class="scroll-content"></div>'
		@$box = $ '<div class="nano scrolling-container">'

		@el.append(@$box.append $content.append @el.children()).addClass 'scroll-box'
		@container = $content

	update: ->
		@$box.nanoScroller()
		@
	scrollTo: (to, opts={}) ->
		to =  if typeof to is 'number' then scrollTop: to else scrollTo: to
		@$box.nanoScroller(to)
		@
	detach:->
		# @el.mCustomScrollbar 'destroy'
		@
	disable:->
		@$box.nanoScroller stop: true
		@container.bind 'mousewheel', prevent
		@
	enable: ->
		@container.unbind 'mousewheel', prevent
		@update()
	offset: ->
		top: @container.scrollTop(), left: @container.scrollLeft()

	bindEvents: (events) =>
		@$box.bind 'update', events.whileScrolling
		@$box.bind 'update', events.onScroll

Scrollable =
	onScroll: ->
	whileScrolling: ->

	scroll: ->
		scrollOptions =
			callbacks: {@onScroll, @whileScrolling}
		unless @_scroll
			@_scroll = new Scroll @scrollContainer or @el, $.extend true, {}, scrollOptions, @scrollOptions
			@bind 'release', => @delay => @_scroll.detach()
			# unless @containerClass
			@container = @_scroll.container
		@_scroll

module.exports = Scrollable