﻿Type.registerNamespace("Telerik.Web.UI");

Telerik.Web.UI.RadTab = function ()
{
	Telerik.Web.UI.RadTab.initializeBase(this);
	
	this._properties = new Telerik.Web.UI.PropertyBag(this);
}

Telerik.Web.UI.RadTab.prototype = {
	/***** Private methods *****/
	_requiresScrolling : function ()
	{
		return this.get_tabStrip()._tabContainerRequiresScrolling(this);
	},
	
	_createChildControls : function ()
	{
		this._children = new Telerik.Web.UI.RadTabCollection(this);
		Telerik.Web.UI.RadTabStrip._createChildControls(this, this._children);
	},
	
	_getChildListIndex : function ()
	{
		if (!this.get_tabData()) return -1;
		
		var childListIndex = -1;
		
		//don't call get_tabs() to avoid recursion.
		var tabsFromThisLevel = [];
		
		if (this.get_parent() != this.get_tabStrip())
		{
			var parentSiblings = this.get_parent().get_parent()._children;
			parentSiblings.forEach(function (tab) {
				Array.addRange(tabsFromThisLevel, tab._children._array);
			});
		}
		else
		{
			tabsFromThisLevel = this.get_parent()._children._array;
		}
		
		var index = Array.indexOf(tabsFromThisLevel, this);
		
		for (var i = 0; i <= index; i++)
		{
			var tab = tabsFromThisLevel[i];
			if (tab.get_tabData())
				childListIndex++;
		}
		
		return childListIndex;
	},
	
	_ensureElements : function ()
	{
		if (!this.get_childListElement())
			this._createChildListElement();
	},
	
	_createChildListElement	: function ()
	{
		var childListElement = document.createElement("ul");
		childListElement.className = "rtsUL";
		
		var listElementsFromTheCurrentLevel = this._getListItemsForTheCurrentLevel();
		
		if (!listElementsFromTheCurrentLevel)
		{
			this.get_parent()._ensureElements();
			
			this.get_tabStrip()._createLevelElement(this.get_level() + 2);
			listElementsFromTheCurrentLevel = this._getListItemsForTheCurrentLevel();
		}
		this._requireChildList();
		
		this.get_levelElement().insertBefore(childListElement, listElementsFromTheCurrentLevel[this._getChildListIndex()] || null);
		
		Array.insert(listElementsFromTheCurrentLevel, this._getChildListIndex(), childListElement);
		
		return childListElement;
	},
	
	_shouldInitializeChild : function (childNode)
	{
		return true;
	},
	
	_getListItemsForTheCurrentLevel : function ()
	{
		return this.get_tabStrip()._getListElementsForLevel(this._getLevelIndex());
	},
	
	_getChildElements : function()
	{
		return $telerik.getChildrenByTagName(this.get_childListElement(), "li");
	},
	
	_requireChildList : function ()
	{
		this._itemData = [];
	},
	
	_doesNotRequireChildList : function ()
	{
		this._itemData = null;
	},
	
	_destroyChildListElement : function ()
	{
        this.get_tabStrip()._destroyChildren(this);
		this._doesNotRequireChildList();
	},
	
	_renderSeparator : function (html)
	{
		html[html.length] = "<li class='rtsLI rtsSeparator'>";
		html[html.legnth] = this.get_text();
		html[html.length] = "</li>";
	},
	
	_renderTab : function (html)
	{
		html[html.length] = "<li class='rtsLI"
		
		if (this.get_isFirst())
			html[html.length] = " rtsFirst";
		
		if (this.get_isLast())
			html[html.length] = " rtsLast";
			
		html[html.length] = "'><a "
		
		if (this.get_target())
		{
			html[html.length] = "target='";
			html[html.length] = this.get_target();
			html[html.length] = "' ";
		}
		
		html[html.length] = "href='";
		
		if (this.get_navigateUrl())
			html[html.length] = this.get_navigateUrl();
		else
			html[html.length] = "#";
		
		html[html.length] = "' class='"
		html[html.length] = this._determineCssClass(this.get_index());
		html[html.length] = "'><span class='rtsOut'><span class='rtsIn'>"
		
		var imageUrl = this._determineImage();
		if (imageUrl)
		{
			html[html.length] = "<img alt='' class='rtsImg' src='";
			html[html.length] = imageUrl;
			html[html.length] = "' />";
		}
		
		html[html.length] = "<span class='rtsTxt'>";
		html[html.length] = this.get_text();
		html[html.length] = "</span></span></span></a></li>";
	},
	
	_determineCssClass : function (index)
	{
		var cssClass = [];
		var parentSelectedIndex = this.get_parent().get_selectedIndex();
		cssClass[cssClass.length] = "rtsLink";
		
		if (this.get_cssClass())
			cssClass[cssClass.length] = this.get_cssClass();
		
		if (index == parentSelectedIndex)
		{
			cssClass[cssClass.length] = "rtsSelected";
			if (this.get_selectedCssClass())
				cssClass[cssClass.length] = this.get_selectedCssClass();
		}
		if (!this.get_enabled())
		{
			cssClass[cssClass.length] = "rtsDisabled";
			if (this.get_disabledCssClass())
				cssClass[cssClass.length] = this.get_disabledCssClass();
		}
		
		if (parentSelectedIndex > -1)
		{
			if (parentSelectedIndex - 1 == index)
				cssClass[cssClass.length] = "rtsBefore";
			if (parentSelectedIndex + 1 == index)
				cssClass[cssClass.length] = "rtsAfter";
		}
		
		return cssClass.join(" ");
	},
	
	_render : function (html)
	{
		if (this.get_isSeparator())
			this._renderSeparator(html);
		else
			this._renderTab(html);
		
		this._updateSiblings();
		
		if (this.get_tabs().get_count() > 0)
			this._renderChildren();
	},
	
	_getPreviousVisibileTab : function ()
	{
		var siblings = this.get_parent().get_tabs();
		for (var index = this.get_index() - 1; index > -1; index--)
		{
			var tab = siblings.getTab(index);
			if (tab.get_visible()) return tab;
		}
		return null;
	},
	
	_getNextVisibleTab : function ()
	{
		var siblings = this.get_parent().get_tabs();
		for (var index = this.get_index() + 1, length = siblings.get_count();index < length; index++)
		{
			var tab = siblings.getTab(index);
			if (tab.get_visible()) return tab;
		}
		return null;
	},
	
	_updateSiblings : function (updateLinkOnly)
	{
		var previous = this._getPreviousVisibileTab();
		
		if (previous)
			previous._updateAppearance(updateLinkOnly);
		
		var next = this._getNextVisibleTab();
		if (next)
			next._updateAppearance(updateLinkOnly);
	},
	
	_renderChildren : function()
	{
		var childListElement = this._createChildListElement();
		var html = [];
		this.get_tabs().forEach(function(tab){ tab._render(html); });
		
		childListElement.innerHTML = html.join("");
	},
	
	_cacheDomProperties : function ()
	{
	    this.get_text();
	    this.get_navigateUrl();
	},
		
	_cleanElements : function ()
	{
		this._cacheDomProperties();
		
		this.get_tabs().forEach(function(tab) { 
			tab._cacheDomProperties(); 
			tab._cleanElements();
		});
		
		this.get_parent().get_childListElement().removeChild(this.get_element());
		this._element = null;
		
		if ($telerik.getChildrenByTagName(this.get_parent().get_childListElement(), "li") < 1)
		      this.get_parent()._destroyChildListElement();
	},
	
	_getLevelIndex : function ()
	{
		if (this.get_tabStrip()._ascendingRendering())
			return this.get_level() + 1;
		
		return this.get_tabStrip()._getLevelElements().length - this.get_level() - 2;
	},
	
	_getFirstVisibleIndex : function ()
	{
		var siblings = this.get_parent().get_tabs();
		for (var index = 0, count = siblings.get_count(); index < count; index++)
		{
			if (siblings.getTab(index).get_visible())
				return index;
		}
		
		return siblings.get_count();
	},
	
	_getLastVisibleIndex : function ()
	{
		var siblings = this.get_parent().get_tabs();
		for (var index = siblings.get_count() - 1; index > -1; index--)
		{
			if (siblings.getTab(index).get_visible())
				return index;
		}
		return -1;
	},
	
	
	_updateAppearance : function (updateLinkOnly)
	{
		if (!this.get_element()) return;
		
		var index = this.get_index();
		
		if (this.get_linkElement())
			this._setCssClass(this.get_linkElement(), this._determineCssClass(index));
		
		this._updateImage();
		
		if (updateLinkOnly) return;
		
		var cssClass = "rtsLI";
		
		if (index == this._getFirstVisibleIndex())
			cssClass += " rtsFirst";
			
		if (index == this._getLastVisibleIndex())
			cssClass += " rtsLast";
			
		this._setCssClass(this.get_element(), cssClass);
	},
	_determineImage : function ()
	{
		var imageUrl = this.get_imageUrl();
		if (this.get_selected() && this.get_selectedImageUrl())
			imageUrl = this.get_selectedImageUrl();
		if (!this.get_enabled() && this.get_disabledImageUrl())
			imageUrl = this.get_disabledImageUrl();
		
		return imageUrl;		
	},
	_updateImage : function ()
	{
		if (!this.get_element()) return;
		
		var imageUrl = this._determineImage();
		if (!imageUrl) return;
		
		if (!this.get_imageElement())
		{
			var imageElement = document.createElement("img");
			imageElement.className = "rtsImg";
			imageElement.alt = '';
			
			this.get_innerWrapElement().insertBefore(imageElement, this.get_textElement());
		}
		
		if (this.get_imageElement().src != imageUrl)
			this.get_imageElement().src = imageUrl;
	},
	
	_setChildListDisplay : function (display)
	{
		var tabStrip = this.get_tabStrip();
		var currentTab = this;
		while (currentTab)
		{
			var childListElement = currentTab.get_childListElement();
			if (childListElement)
			{
				childListElement.style.display = display;
				if (display != "none" && tabStrip._align == Telerik.Web.UI.TabStripAlign.Justify)
					Telerik.Web.UI.RadTabStrip._justify(childListElement, tabStrip._orientation);
					
			}
			currentTab = currentTab.get_selectedTab();
		}
	},
	
	_highlight : function ()
	{
		if (this.get_hoveredCssClass())
			Sys.UI.DomElement.addCssClass(this.get_linkElement(), this.get_hoveredCssClass());
		
		if (!this.get_enabled()) return;
		if (!this.get_hoveredImageUrl()) return;
		if (!this.get_imageElement()) return;
		
		if (this.get_imageElement().src != this.get_hoveredImageUrl())
			this.get_imageElement().src = this.get_hoveredImageUrl();
	},
	
	_unhighlight : function ()
	{
		if (this.get_hoveredCssClass())
			Sys.UI.DomElement.removeCssClass(this.get_linkElement(), this.get_hoveredCssClass());
		
		this._updateImage();
	},
	
	_shouldPostBack : function ()
	{
		var tabStrip = this.get_tabStrip();
		
		if (!tabStrip) return false;
		
		return this.get_postBack() && tabStrip._postBackReference != null;
	},
	
	_initialize : function (data, element)
	{
		Telerik.Web.UI.RadTab.callBaseMethod(this, '_initialize', [data, element]);
		
		this._perTabScrolling = this._properties.getValue("perTabScrolling", false);
		this._scrollChildren = this._properties.getValue("scrollChildren", false);
		this._scrollButtonsPosition = this._properties.getValue("scrollButtonsPosition", 
			Telerik.Web.UI.TabStripScrollButtonsPosition.Right);
		this._ensureChildControls();
	},
	_dispose : function ()
	{
		Telerik.Web.UI.RadTab.callBaseMethod(this, '_dispose');
		if (this._scroller)
			this._scroller.dispose();
	},
	
	_initScrolling : function ()
	{
		if (this.get_selected() && this._requiresScrolling())
			this.get_tabStrip()._initScrollingForTabContainer(this);
	},
	
	_selectPageView : function (suppressVisualUpdate)
	{
		var pageView = this.get_pageView();
		if (pageView)
			pageView._select(suppressVisualUpdate);
		
		if (this.get_selectedIndex() > -1)
			this.get_selectedTab()._selectPageView(suppressVisualUpdate);
	},
	
	_getGlobalIndex : function () 
	{
		return Array.indexOf(this.get_tabStrip().get_allTabs(), this);
	},
	
	/***** Public API ******/
	scrollIntoView : function ()
	{
		var owner = this.get_parent();
		if (!owner) return;
		if (!owner._scroller) return;
		
		owner._scroller._scrollTo(this.get_element().offsetLeft);
		var tabStrip = this.get_tabStrip();
		tabStrip._updateScrollState(owner, owner._scroller._currentPosition);
	},
	
	get_nextTab : function ()
	{
		return this.get_nextSibling();
	},
	get_previousTab : function ()
	{
		return this.get_previousSibling();
	},
	
	click : function (e)
	{
		if (!this.get_isEnabled())
			return false;
		
		var tabStrip = this.get_tabStrip();
		if (!tabStrip) return false;
		
		if (tabStrip.get_causesValidation())
		{
			if (typeof(Page_ClientValidate) !== "undefined" && !Page_ClientValidate(tabStrip.get_validationGroup()))
				return false;
		}
		
		if (!this.select(e))
			return false;
		
		if (this._shouldNavigate())
			return true;
		
		if (this._shouldPostBack())
			tabStrip._postback(this);
		
		return false;
	},
	
	get_pageView : function ()
	{
		var multiPage = this.get_tabStrip().get_multiPage();
		if (!multiPage)
			return null;
		
		if (this.get_pageViewID())
			return multiPage.findPageViewByID(this.get_pageViewID());
			
		return multiPage.get_pageViews().getPageView(this._getGlobalIndex());
	},
		
	get_pageViewID : function ()
	{
		return this._properties.getValue("pageViewID", null);
	},
	
	set_pageViewID : function (value)
	{
		this._properties.setValue("pageViewID", value);
	},
	
	get_target : function ()
	{
		if (this.get_linkElement())
		    return this._properties.getValue("target", this.get_linkElement().target);
		
		return this._properties.getValue("target", null);
	},
	
	set_target : function (value)
	{
		this._properties.setValue("target", value, true);
		
		if (this.get_linkElement())
			this.get_linkElement().target = value;
	},
	
	get_navigateUrl : function()
	{
		return this._getNavigateUrl();
	},
	
	set_navigateUrl : function (value)
	{
		this._properties.setValue("navigateUrl", value, true);
		
		if (this.get_linkElement())
			this.get_linkElement().href = value;
	},
		
	get_postBack : function ()
	{
		return this._properties.getValue("postback", true);
	},
	
	set_postBack : function (value)
	{
		this._properties.setValue("postback", value, true);
	},
	
	get_selected : function ()
	{
		if (!this.get_parent()) return false;
		
		return this.get_index() == this.get_parent().get_selectedIndex();
	},
	
	set_selected : function (value)
	{
		if (value)
			this.select();
		else
			this.unselect();
	},
	
	selectParents : function ()
	{
		var tabs = [];
		var current = this;
		while (current != this.get_tabStrip())
		{
			tabs[tabs.length] = current;
			current = current.get_parent();
		}

		var i = tabs.length;
	
		while (i --)
		{
			tabs[i].select();
		}
	},
	
	select : function (e)
	{	
		var parent = this.get_parent();
		if (!parent)
		{
			this._cachedSelected = true;
			return true;
		}
		
		var shouldNavigate = this._shouldNavigate();
		var selectedTab = parent.get_selectedTab();
		var tabStrip = this.get_tabStrip();
		
		if (!shouldNavigate && selectedTab == this && !tabStrip.get_clickSelectedTab())
			return false;
			
		if (tabStrip._raiseCancelEvent("tabSelecting", this, e)) 
			return false;

		var suppressVisualUpdate = this._shouldPostBack() || (shouldNavigate && (!this.get_target() || this.get_target() == "_self"));
		
		if (!e)
			suppressVisualUpdate = false;
			
		if (selectedTab && selectedTab != this)
			selectedTab.unselect(suppressVisualUpdate, e);
			
		parent._setSelectedIndex(this.get_index());
		tabStrip._registerSelectedTab(this);

		if (!suppressVisualUpdate)
		{
			this._updateAppearance(true);
			this._updateSiblings(true);
			this._setChildListDisplay("");
			
			if (this._scroller)
			{
				this._scroller._showArrows();
			}
			else
			{
				tabStrip._scrollInitInProgress = true;
				this._initScrolling();
				tabStrip._scrollInitInProgress = false;
			}
			if (tabStrip._reorderTabsOnSelect)
				Telerik.Web.UI.RadTabStrip._reorderTabs(parent.get_childListElement(), this.get_element());
		}		
		
		if (tabStrip.get_multiPage())
			this._selectPageView(suppressVisualUpdate);
		
		tabStrip._raiseEvent("tabSelected", this, e);
		return true;
	},
	
	unselect : function (suppressVisualUpdate, e)
	{	
		var parent = this.get_parent();
		if (!parent) return;
		if (!this.get_selected()) return;
		
		parent._setSelectedIndex(-1);
		
		var tabStrip = this.get_tabStrip();
		tabStrip._unregisterSelectedTab(this);
		
		if (!suppressVisualUpdate)
		{
			this._setChildListDisplay("none");
			
			if (this._scroller)
				this._scroller._hideArrows();
				
			this._updateAppearance(true);
			this._updateSiblings(true);
		}
		
		var selectedTab = this.get_selectedTab();
		
		if (tabStrip.get_unselectChildren() && selectedTab)
			selectedTab.unselect(suppressVisualUpdate);
		
		tabStrip._raiseEvent("tabUnSelected", this, e);
	},
	
	get_selectedIndex : function ()
	{
		return this._properties.getValue("selectedIndex", -1);
	},
	
	_setSelectedIndex : function (value)
	{
		this._properties.setValue("selectedIndex", value);
	},
	
	set_selectedIndex : function (value)
	{
		if (value > -1)
		{
			var tab = this.get_tabs().getTab(value);
			if (tab)
				tab.select();
		}
		else
		{
			var selectedTab = this.get_selectedTab();
			if (selectedTab)
				selectedTab.unselect();
		}
	},
	
	get_selectedTab : function ()
	{
		return this.get_tabs().getTab(this.get_selectedIndex()) || null;
	},
		
	get_tabStrip : function ()
	{
		return this._getControl();
	},
	
	get_isSeparator : function ()
	{
		return this._properties.getValue("isSeparator", false);
	},
	
	set_isSeparator : function (value)
	{
		this._properties.setValue("isSeparator", value)
	},
	
	get_tabData : function ()
	{
		return this.get_itemData();
	},
	
	get_levelElement : function ()
	{
		if (!this._levelElement)
			this._levelElement = this._getControl()._getLevelElements()[this._getLevelIndex()] || null;
		
		return this._levelElement;
	},
	
	get_textElement : function ()
	{
		if (this.get_isSeparator())	return this.get_element();
		
		if (!this.get_innerWrapElement()) return null;
		
		if (!this._textElement)
			this._textElement = $telerik.getChildByClassName(this.get_innerWrapElement(), "rtsTxt");
				
		return this._textElement;
	},
	
	get_linkElement : function ()
	{
		if (!this.get_element()) return null;
		
		if (!this._linkElement)
			this._linkElement = $telerik.getChildByClassName(this.get_element(), "rtsLink");
		
		return this._linkElement;
	},
	
	get_imageElement : function ()
	{
		if (!this.get_innerWrapElement()) return null;
		if (!this._imageElement)
			this._imageElement = $telerik.getChildByClassName(this.get_innerWrapElement(), "rtsImg");
		return this._imageElement;
	},
	
	get_outerWrapElement : function()
	{
		if (!this.get_linkElement())
			return null;
			
		if (!this._outerWrapElement)
			this._outerWrapElement = $telerik.getChildByClassName(this.get_linkElement(), "rtsOut");
		
		return this._outerWrapElement;
	},
	
	get_innerWrapElement : function ()
	{
		if (!this.get_outerWrapElement())
			return null;
			
		if (!this._innerWrapElement)
			this._innerWrapElement = $telerik.getChildByClassName(this.get_outerWrapElement(), "rtsIn");
		
		return this._innerWrapElement;
	},
	
	get_childListElement : function ()
	{
		if (!this._childListElement)
		{
			var listElementsFromTheCurrentLevel = this._getListItemsForTheCurrentLevel();
			
			if (!listElementsFromTheCurrentLevel) return null;
			
			this._childListElement = listElementsFromTheCurrentLevel[this._getChildListIndex()] || null;
		}
		
		return this._childListElement;
	},
	
	get_tabs : function ()
	{
		return this._getChildren();
	},
	
	enable : function ()
	{
		this.set_enabled(true);
	},
	
	disable : function ()
	{
		this.set_enabled(false);
	},
	
	set_visible : function (value)
	{
		Telerik.Web.UI.RadTab.callBaseMethod(this, "set_visible", [value]);
		
		if (value) 
			this.show();
		else
			this.hide();
	},
	
	show : function ()
	{
		this.get_element().style.display = "";
		this._updateSiblings();
	},
	
	hide : function ()
	{
		this.get_element().style.display = "none";
		this._updateSiblings();
		this.unselect();
	},
	
	set_enabled : function (value)
	{
		Telerik.Web.UI.RadTab.callBaseMethod(this, "set_enabled", [value]);
		
		this._updateAppearance();
	},
	
	get_disabledCssClass : function ()
	{
		return this._properties.getValue("disabledCssClass", null);
	},
	
	set_disabledCssClass : function (value)
	{
		this._properties.setValue("disabledCssClass", value, true);
		this._updateAppearance();
	},
	
	get_selectedCssClass : function ()
	{
		return this._properties.getValue("selectedCssClass", null);
	},
	
	set_selectedCssClass : function (value)
	{
		this._properties.setValue("selectedCssClass", value, true);
		this._updateAppearance();
	},
	
	get_hoveredCssClass : function ()
	{
		return this._properties.getValue("hoveredCssClass", null);
	},
	
	set_hoveredCssClass : function (value)
	{
		this._properties.setValue("hoveredCssClass", value, true);
	},
	
	get_cssClass : function ()
	{
		return this._properties.getValue("cssClass", null);
	},
	set_cssClass : function (value)
	{
		this._properties.setValue("cssClass", value, true);
		this._updateAppearance();
	},
	get_imageUrl : function ()
	{
		return this._properties.getValue("imageUrl", null);
	},
	set_imageUrl : function (value)
	{
		this._properties.setValue("imageUrl", value, true);
		this._updateImage();
	},
	get_selectedImageUrl : function ()
	{
		return this._properties.getValue("selectedImageUrl", null);
	},
	set_selectedImageUrl : function (value)
	{
		this._properties.setValue("selectedImageUrl", value, true);
		this._updateImage();
	},
	get_disabledImageUrl : function ()
	{
		return this._properties.getValue("disabledImageUrl", null);
	},
	set_disabledImageUrl : function (value)
	{
		this._properties.setValue("disabledImageUrl", value, true);
		this._updateImage();
	},
	get_hoveredImageUrl : function ()
	{
		return this._properties.getValue("hoveredImageUrl", null);
	},
	set_hoveredImageUrl : function (value)
	{
		this._properties.setValue("hoveredImageUrl", value, true);
	},
	get_isBreak : function ()
	{
		return this._properties.getValue("isBreak", false);
	},
	set_isBreak : function (value)
	{
		this._properties.setValue("isBreak", value, true);
	}
}

Telerik.Web.UI.RadTab.registerClass('Telerik.Web.UI.RadTab', Telerik.Web.UI.ControlItem);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();