﻿Type.registerNamespace("Telerik.Web.UI");

Telerik.Web.UI.TabScroller = function (owner, orientation)
{
	this._owner = owner;
	Telerik.Web.UI.TabScroller.initializeBase(this, [owner.get_childListElement(), owner.get_levelElement(), orientation]);
}

Telerik.Web.UI.TabScroller.prototype = {
	_scrollTo : function (position)
	{
		var scrollProperty = "marginLeft";
		if (this._orientation == Telerik.Web.UI.ScrollerOrientation.Vertical)
			scrollProperty = "marginTop";
		
		this._currentPosition = position;
		this._scrolledElement.style[scrollProperty] = -position + "px";
		
		this._raiseEvent('positionChanged', Sys.EventArgs.Empty);
	},
	
	_createArrow : function (className)
	{
		var arrow = document.createElement("a");
		arrow.className = className;
		arrow.href = "#";
		arrow.innerHTML = "&nbsp;";
		if (!$telerik.isIE)
		{
			arrow.style.position = "relative";
		}
		return arrow;
	},
	
	_applyFloat : function (arrow, floatValue)
	{
		if ($telerik.isIE)
			arrow.style.styleFloat = floatValue;
		else
			arrow.style.cssFloat = floatValue;
	},
	
	_preventDefault : function (e)
	{
		e.preventDefault();
	},
	
	_scrollForward : function (e)
	{
		if (this._owner._perTabScrolling)
			this._scrollToTab(1);
		else
			this.startScroll(Telerik.Web.UI.ScrollerSpeed.Fast, 2);
	},
	
	_stopScroll : function (e)
	{
		this.stopScroll();
		this._owner._getControl()._updateScrollState(this._owner, this._currentPosition);
	},
	
	_scrollBackward: function (e)
	{
		if (this._owner._perTabScrolling)
			this._scrollToTab(-1);
		else
			this.startScroll(Telerik.Web.UI.ScrollerSpeed.Fast, -2);
	},
	
	_positionChanged : function ()
	{
		this._updateArrows();
	},
	
	_updateArrows : function ()
	{
		var className = "rtsPrevArrow";
		
		if (this.isAtMinPosition())
			className = "rtsPrevArrowDisabled";
		
		if (this._previousArrow.className != className)
			this._previousArrow.className = className;
			
		className = "rtsNextArrow"
		
		if (this.isAtMaxPosition())
			className = "rtsNextArrowDisabled";
		
		if (this._nextArrow.className != className)
			this._nextArrow.className = className;
	},
	
	_positionArrowsHorizontally : function (scrollButtonsPosition)
	{
		if (!$telerik.isIE)
		{
			this._nextArrow.style.position = "absolute";
			this._previousArrow.style.position = "absolute";
			this._previousArrow.style.top = "0";
			this._nextArrow.style.top = "0";
		}
		else
		{
			this._nextArrow.style.marginTop = this._previousArrow.style.marginTop = -this._element.offsetHeight + "px";
		}
		
		if (scrollButtonsPosition == Telerik.Web.UI.TabStripScrollButtonsPosition.Right)
		{
			this._applyFloat(this._nextArrow, "right");
			this._applyFloat(this._previousArrow, "right");
			this._element.appendChild(this._nextArrow);
			this._element.appendChild(this._previousArrow);
			if (!$telerik.isIE)
			{
				this._nextArrow.style.right = "0";
				this._previousArrow.style.right = this._nextArrow.offsetWidth + "px";
			}
		}
		else if (scrollButtonsPosition == Telerik.Web.UI.TabStripScrollButtonsPosition.Left)
		{
			this._applyFloat(this._nextArrow, "left");
			this._applyFloat(this._previousArrow, "left");
			this._element.appendChild(this._previousArrow);
			this._element.appendChild(this._nextArrow);
			if (!$telerik.isIE)
			{
				this._previousArrow.style.left = "0";
				this._nextArrow.style.left = this._previousArrow.offsetWidth + "px";
			}
		}else
		{
			this._applyFloat(this._nextArrow, "right");
			this._applyFloat(this._previousArrow, "left");
			this._element.appendChild(this._previousArrow);
			this._element.appendChild(this._nextArrow);
			if (!$telerik.isIE)
			{
				this._previousArrow.style.left = "0";
				this._nextArrow.style.right = "0";
			}
		}
	},

	_positionArrowsVertically : function (scrollButtonsPosition)
	{
		this._element.style.position = "relative";
		this._nextArrow.style.position = "absolute";
		this._previousArrow.style.position = "absolute";
		this._nextArrow.style.left = "0";
		this._previousArrow.style.left = "0";
		this._element.appendChild(this._previousArrow);
		this._element.appendChild(this._nextArrow);
		
		if (scrollButtonsPosition == Telerik.Web.UI.TabStripScrollButtonsPosition.Right)
		{
			this._nextArrow.style.bottom = "0";
			this._previousArrow.style.bottom = this._nextArrow.offsetHeight + "px";
		}
		else if (scrollButtonsPosition == Telerik.Web.UI.TabStripScrollButtonsPosition.Left)
		{
			this._previousArrow.style.top = "0";
			this._nextArrow.style.top = this._previousArrow.offsetHeight + "px";
		}
		else
		{
			this._previousArrow.style.top = "0";
			this._nextArrow.style.bottom = "0";
		}
	},	
	_positionArrows : function (scrollButtonsPosition)
	{
		if (this._orientation == Telerik.Web.UI.ScrollerOrientation.Horizontal)
			this._positionArrowsHorizontally(scrollButtonsPosition);
		else
			this._positionArrowsVertically(scrollButtonsPosition);
	},
	
	_hideArrows : function ()
	{
		this._nextArrow.style.display = "none";
		this._previousArrow.style.display = "none";
	},
	_showArrows : function ()
	{
		this._nextArrow.style.display = "";
		this._previousArrow.style.display = "";
	},
	
	_nextScrollPosition : function (direction)
	{
		var tabs = this._owner.get_tabs();
		
		var sizeAttribute = "offsetWidth";
		
		if (this._orientation == Telerik.Web.UI.ScrollerOrientation.Vertical)
			sizeAttribute = "offsetHeight";
		var targetElement = direction < 0 ? tabs.getTab(this._currentTabIndex + direction).get_element() : 
				tabs.getTab(this._currentTabIndex).get_element();
		
		if (targetElement)
			return this._currentPosition + direction * targetElement[sizeAttribute];
		
		return this._currentPosition;
	},
	
	setScrollingLimits : function (min, max)
	{
		if (!this._owner._perTabScrolling)
		{
			var sizeAttribute = "offsetWidth";
			
			if (this._orientation == Telerik.Web.UI.ScrollerOrientation.Vertical)
				sizeAttribute = "offsetHeight";
			max += this._getScrollImageSize(sizeAttribute);
				
		}
		Telerik.Web.UI.TabScroller.callBaseMethod(this, "setScrollingLimits", [min, max]);
	},
	
	_getScrollImageSize : function (sizeAttribute)
	{
		if (this._owner._scrollButtonsPosition == Telerik.Web.UI.TabStripScrollButtonsPosition.Right)
			return this._nextArrow[sizeAttribute] + this._previousArrow[sizeAttribute];
		
		if (this._owner._scrollButtonsPosition == Telerik.Web.UI.TabStripScrollButtonsPosition.Middle)
			return this._nextArrow[sizeAttribute];
		return 0;
	},
	
	_scrollToTab : function (direction)
	{
		if (direction > 0 && this.isAtMaxPosition()) return;
		if (direction < 0 && this.isAtMinPosition()) return;
		
		var nextPosition = this._nextScrollPosition(direction);
		
		if (nextPosition == this._currentPosition) return;
		this._scrollTo(nextPosition);
		this._currentTabIndex += direction;
	},
	
	_calculateInitialTab : function ()
	{
		if (!this._owner._perTabScrolling) return;
		var size = 0;
		var tabs = this._owner.get_tabs();
		var sizeAttribute = "offsetWidth";
		
		if (this._orientation == Telerik.Web.UI.ScrollerOrientation.Vertical)
			sizeAttribute = "offsetHeight";
		while (size < this._currentPosition)
		{
			size += tabs.getTab(this._currentTabIndex).get_element()[sizeAttribute];
			this._currentTabIndex ++;
		}
	},
	
	initialize : function ()
	{
		Telerik.Web.UI.TabScroller.callBaseMethod(this, "initialize");
		
		if (this._owner._perTabScrolling)
			this._currentTabIndex = 0;
		
		this._positionChangedDelegate = Function.createDelegate(this, this._positionChanged);
		this.add_positionChanged(this._positionChangedDelegate);
		
		this._nextArrow = this._createArrow("rtsNextArrow");
		this._previousArrow = this._createArrow("rtsPrevArrow");
		
		this._positionArrows(this._owner._scrollButtonsPosition);
		
		this._nextArrowClickDelegate = Function.createDelegate(this, this._preventDefault);
		this._scrollForwardDelegate = Function.createDelegate(this, this._scrollForward);
		this._nextArrowMouseUpDelegate = Function.createDelegate(this, this._stopScroll);
		
		$addHandler(this._nextArrow, "click", this._nextArrowClickDelegate);
		$addHandler(this._nextArrow, "mousedown", this._scrollForwardDelegate);
		$addHandler(this._nextArrow, "mouseup", this._nextArrowMouseUpDelegate);
		
		
		this._previousArrowClickDelegate = Function.createDelegate(this, this._preventDefault);
		this._scrollBackwardDelegate = Function.createDelegate(this, this._scrollBackward);
		this._previousArrowMouseUpDelegate = Function.createDelegate(this, this._stopScroll);
		
		$addHandler(this._previousArrow, "click", this._previousArrowClickDelegate);
		$addHandler(this._previousArrow, "mousedown", this._scrollBackwardDelegate);
		$addHandler(this._previousArrow, "mouseup", this._previousArrowMouseUpDelegate);
	},
	
	dispose : function ()
	{
		Telerik.Web.UI.TabScroller.callBaseMethod(this, "dispose");
		
		$removeHandler(this._nextArrow, "click", this._nextArrowClickDelegate);
		$removeHandler(this._nextArrow, "mousedown", this._scrollForwardDelegate);
		$removeHandler(this._nextArrow, "mouseup", this._nextArrowMouseUpDelegate);
		
		$removeHandler(this._previousArrow, "click", this._previousArrowClickDelegate);
		$removeHandler(this._previousArrow, "mousedown", this._scrollBackwardDelegate);
		$removeHandler(this._previousArrow, "mouseup", this._previousArrowMouseUpDelegate);
		
		this._nextArrow = null;
		this._previousArrow = null;
	}
}

Telerik.Web.UI.TabScroller.registerClass("Telerik.Web.UI.TabScroller", Telerik.Web.UI.Scroller);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();