/**
 * Scroll manager.
 */
var scrollManager = new function() {
	var _interval;
	var _src;
	var _cur;
	var _way;
	var _time;
	/**
	 * Set scroll.
	 */
	this.scrollRelative = function(posY)
	{
		if (_interval) clearInterval(_interval);
		if (posY == 0) return;
		_src 	= this.currentY();
		
		console.log(posY + " cur: " + _src);
		_src = Math.round(_src + posY);
		if (_src < 0) _src = 0;
		window.scrollTo(0, _src);
	}	
	
	/**
	 * Set scroll.
	 */
	this.scrollTo = function(posY)
	{
		if (_interval) clearInterval(_interval);
		_src 	= this.currentY();
		_cur	= _src;
		_time 	= 0;
		if (_cur == posY) return;
		_way 	  = posY - _cur;
		_interval = setInterval('scrollManager.update()', 33);
	}
	
	/**
	 * Update scroll position.
	 */
	this.update = function()
	{
		_time += 33;
		if (_time > 1000) _time = 1000;
		
		if (Math.round(_cur) == this.currentY())
		{
			_cur = this.easeOut(_time, _src, _way, 1000);
			window.scrollTo(0, Math.round(_cur));
		}
		else
			_time = 1000;
	
		// Complete.
		if (_time == 1000)
		{
			if (_interval) clearInterval(_interval);
			_interval = null;
		}
	}
	
	/**
	 * Easing
	 */
	this.easeOut = function(t, b, c, d)
	{
		return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
	}
	/**
	 * Get current scroll position.
	 */
	this.currentY = function()
	{
		if (document.body && document.body.scrollTop) return(document.body.scrollTop);
		if (document.documentElement && document.documentElement.scrollTop) return(document.documentElement.scrollTop);
		if (window.pageYOffset) return(window.pageYOffset);
		return(0);
	}
	
	return(this);
}();
