﻿Type.registerNamespace("Telerik.Web.UI");

// ---------- ScrollerOrientation Enum ----------
Telerik.Web.UI.ScrollerOrientation = function(){}
Telerik.Web.UI.ScrollerOrientation.prototype = 
{
	Vertical: 0,
	Horizontal: 1
}
Telerik.Web.UI.ScrollerOrientation.registerEnum("Telerik.Web.UI.ScrollerOrientation");

// ---------- ScrollerSpeed Enum ----------
Telerik.Web.UI.ScrollerSpeed = function(){}
Telerik.Web.UI.ScrollerSpeed.prototype = 
{
	Invalid: 0,
	Slow: 1,
	Medium: 2,
	Fast: 3
}
Telerik.Web.UI.ScrollerSpeed.registerEnum("Telerik.Web.UI.ScrollerSpeed");

// ---------- ArrowPosition Enum ----------
Telerik.Web.UI.ArrowPosition = function(){}
Telerik.Web.UI.ArrowPosition.prototype = 
{
	Top: 0,
	Bottom: 1,
	Left: 2,
	Right: 3
}
Telerik.Web.UI.ArrowPosition.registerEnum("Telerik.Web.UI.ArrowPosition");

// ---------- Scroller Class ----------
Telerik.Web.UI.Scroller = function(scrolledElement, element, orientation)
{
	// Constants
	this._timerInterval = 10;
	
	this._scrolledElement = scrolledElement;
	this._element = element;
	this._orientation = orientation;
	this._minPosition = 0;
	this._maxPosition = null;
	this._currentPosition = 0;
	this._speed = Telerik.Web.UI.ScrollerSpeed.Invalid;
	this._direction = 0;
	
	this._events = null;
	
	this._timer = null;
	this._onTickDelegate = null;
}

Telerik.Web.UI.Scroller.prototype = 
{
	initialize: function()
	{
		this._onTickDelegate = Function.createDelegate(this, this._onTick);
		
		this._timer = new Telerik.Web.Timer()
		this._timer.set_interval(this._timerInterval);
		this._timer.add_tick(this._onTickDelegate);
	},
	
	dispose: function()
	{
		if (this._timer)
			this._timer.dispose();
		
		this._onTickDelegate = null;
		this._events = null;
	},
	
	// Properties
	get_element : function ()
	{
		return this._element;
	},

	get_events: function ()
	{
		if (!this._events)
			this._events = new Sys.EventHandlerList();
		return this._events;
	},
	
	
	// Events
	add_positionChanged : function(handler)
	{
		this.get_events().addHandler('positionChanged', handler);
	},
	
	remove_positionChanged : function(handler)
	{
		this.get_events().removeHandler('positionChanged', handler);        
	},

	// Public methods
	setScrollingLimits : function (minPosition, maxPosition)
	{
		this._minPosition = Math.max(0, minPosition);
		this._maxPosition = Math.min(this._getElementSize(), maxPosition);
	},
	
	isAtMinPosition : function ()
	{
		return this._currentPosition <= this._minPosition;
	},
	
	isAtMaxPosition : function ()
	{
		return this._currentPosition >= this._maxPosition;
	},
	
	resetState : function ()
	{
		this._resetOverflowStyle();
		this._scrollTo(0);
	},
	
	startScroll : function (speed, direction)
	{
		this._speed = speed;
		this._direction = direction;
		this._timer.set_enabled(true);
	},
	
	changeScrollSpeed : function (speed)
	{
		this._speed = speed;
	},
	
	stopScroll : function ()
	{
		this._speed = Telerik.Web.UI.ScrollerSpeed.Invalid;
		this._direction = 0;
		
		this._timer.set_enabled(false);
	},
	
	// Private methods
	_onTick : function ()
	{
		var nextPosition = this._currentPosition + (this._direction * this._speed);
		nextPosition = Math.max(nextPosition, this._minPosition);
		nextPosition = Math.min(nextPosition, this._maxPosition);
		
		this._scrollTo(nextPosition);
		
		if (nextPosition == this._minPosition || nextPosition == this._maxPosition)
			this.stopScroll();
	},
	
	_scrollTo : function (position)
	{
		var scrollProperty = "left";
		if (this._orientation == Telerik.Web.UI.ScrollerOrientation.Vertical)
			scrollProperty = "top";
		
		this._currentPosition = position;
		this._scrolledElement.style[scrollProperty] = -position + "px";
		
		this._raiseEvent('positionChanged', Sys.EventArgs.Empty);
	},

	_resetOverflowStyle : function ()
	{
		if ($telerik.isIE)
		{
			this._element.style.overflow = "visible";
			if (this._orientation == Telerik.Web.UI.ItemFlow.Vertical)
			{
				this._element.style.overflowX = "visible";
				this._element.style.overflowY = "hidden";
			}
			else
			{
				this._element.style.overflowX = "hidden";
				this._element.style.overflowY = "hidden";
			}
		}
		else
		{
			this._element.style.overflow = "hidden";
		}
	},
	
	_getElementSize : function ()
	{
		if (this._orientation == Telerik.Web.UI.ScrollerOrientation.Vertical)
		{
			return this._scrolledElement.offsetHeight;
		}
		else
		{
			return this._scrolledElement.offsetWidth;
		}
	},
	
	_raiseEvent : function (eventName, eventArgs)
	{
		var handler = this.get_events().getHandler(eventName);        

		if (handler)
		{
			if (!eventArgs)
				eventArgs = Sys.EventArgs.Empty;

			handler(this, eventArgs);
		}
	}
}

Telerik.Web.UI.Scroller.registerClass('Telerik.Web.UI.Scroller', null, Sys.IDisposable);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();