/**
 * @author Dinmore
 */
// TODO: Make drag speed relative to distance to target, maybe replace tween with this
var SmoothScroller = Class.create({
	
	scrollTime:		0.5,
	dragSpeed:		20,
	dragInterval:	30,
	
	dragDirection:	null,
	dragModifier:	null,
	dragTarget:		null,
	effect:			null,
	interval:		null,
	container:		null,
	onStop:			null,
	
	initialize: function( container, onStop )
	{
		this.container	= $(container);
		this.onStop		= onStop;
	},
	
	scrollTo: function( value )
	{
		if (this.interval)
		{
			clearInterval(this.interval);
			this.interval = null;
		}
		
		if (this.effect)
			this.effect.cancel();
		
		this.effect = new Effect.Tween( this.container, this.container.scrollTop, value, {
			transition:		Effect.Transitions.easeOutQuad,
			duration:		this.scrollTime,
			afterFinish:	this.onStop
		}, 'scrollTop');
	},
	
	jumpTo: function( value )
	{
		this.container.scrollTop = value;
	},
	
	dragTo: function( value )
	{
		if (this.effect)
		{
			this.effect.cancel();
			this.effect = null;
		}
		
		this.dragTarget = value;
		
		if( Math.abs(this.container.scrollTop - value) < this.dragSpeed )
			this.pauseDragging()
		else
		{
			if( value > this.container.scrollTop )
			{
				this.dragModifier	= this.dragSpeed;
				this.dragDirection	= true;
			}
			else
			{
				this.dragModifier	= -this.dragSpeed;
				this.dragDirection	= false;
			}
			
			if( !this.interval )
				this.interval = setInterval( this.dragLoop.bind(this), this.dragInterval );
		}
	},
	
	dragLoop: function()
	{
		var newVal = this.container.scrollTop + this.dragModifier;
		
		if( (this.dragDirection && newVal > this.dragTarget) || (!this.dragDirection && newVal < this.dragTarget) )
		{
			this.pauseDragging();
		}
		else
			this.container.scrollTop = newVal;
	},
	
	pauseDragging: function()
	{
		if (this.interval) {
			clearInterval(this.interval);
			this.interval = null;
		}
		this.container.scrollTop = this.dragTarget;
	},
	
	getLocation: function()
	{
		return this.container.scrollTop;
	}
});
