﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference path="jquery-1.2.6.pack.js" />

Type.registerNamespace('Nyas.ClassLibrary.Behaviors');

Nyas.ClassLibrary.Behaviors.TabbedContent = function(element) {
	Nyas.ClassLibrary.Behaviors.TabbedContent.initializeBase(this, [element]);

	this._onTabClickDelegate = null;
	this._onNextClickDelegate = null;
	this._onPreviousClickDelegate = null;

	this._nextButton = null;
	this._separator = null;
	this._previousButton = null;
	this._redirectToLogin = false;
	this._loginUrl = "";
	this._imagePanel = null;
	this._noImagePanel = null;
	this._selectedTab = "selected";
	this._selectedLink = "selectedIndex";
	this._mainTabContainer = ".ContentDetailTabs.ui-tabs-nav";
	this._mainTabContainerListItem = ".ContentDetailTabs.ui-tabs-nav li";
	this._mainContentContainer = ".ContentDetailTabs-Panels";
	this._contentDetailPanel = ".ContentDetailPanel";
	this._tabClass = ".ui-ContentDetailTabs";
	this._contentClass = ".contentDiv";
	this._contentClassFilter = this._contentClass + ":first";
	this._tabLink = "li a";
	this._hoverClass = "sfHover";
	this._showContentHeaderOnFirstSubItem = true;
	this._showContentHeaderOnAllSubItems = true;
}

Nyas.ClassLibrary.Behaviors.TabbedContent.prototype = {
	initialize: function() {
		Nyas.ClassLibrary.Behaviors.TabbedContent.callBaseMethod(this, 'initialize');

		this._onTabClickDelegate = Function.createDelegate(this, this._onTabClick);
		this._onNextClickDelegate = Function.createDelegate(this, this._onNextClick);
		this._onPreviousClickDelegate = Function.createDelegate(this, this._onPreviousClick);

		var elts = this._findTabLinks();
		for (i = 0; i < elts.length; i++) {
			$addHandler(elts[i], "click", this._onTabClickDelegate);
		}

		$addHandler(this._findNextButton()[0], "click", this._onNextClickDelegate);
		$addHandler(this._findPreviousButton()[0], "click", this._onPreviousClickDelegate);

		var hoverClass = this._hoverClass;
		var panelClass = this._contentDetailPanel;

		$(this._mainTabContainerListItem).hover(
            function() { $(this).find(panelClass).fadeIn("fast"); },
            function() { $(this).find(panelClass).fadeOut("fast"); });

		$(this._mainTabContainerListItem).each(function() {
			$(this).hover(
                function() { $(this).addClass(hoverClass); },
                function() { $(this).removeClass(hoverClass); }
                );
		});

		$(".cmsListing .divider").hide();

		this._selectTabAndContent(0, 0);

	},

	dispose: function() {
		Nyas.ClassLibrary.Behaviors.TabbedContent.callBaseMethod(this, 'dispose');

		this._onTabClickDelegate = null;
		this._onNextClickDelegate = null;
		this._onPreviousClickDelegate = null;

		var elts = this._findTabLinks();
		for (i = 0; i < elts.length; i++) {
			$clearHandlers(elts[i]);
		}
		$clearHandlers(this._findNextButton());
		$clearHandlers(this._findPreviousButton());

		$(this._mainTabContainerListItem).unbind('mouseenter mouseleave');

		this._selectedTab = null;
		this._selectedLink = null;
		this._mainTabContainer = null;
		this._mainContentContainer = null;
		this._nextButton = null;
		this._separator = null;
		this._previousButton = null;
		this._redirectToLogin = false;
		this._loginUrl = "";
		this._imagePanel = null;
		this._noImagePanel = null;
		this._imageNull = true;
		this._contentDetailPanel = null;
		this._tabClass = null;
		this._contentClass = null;
		this._tabLink = null;
		this._showContentHeaderOnFirstSubItem = null;
		this._showContentHeaderOnAllSubItems = null;
	},

	_selectTabAndContent: function(linkIndex, tabIndex) {
		if (this._isHeaderContent(linkIndex)) {
			linkIndex++;
		}
		this._selectTab(linkIndex, tabIndex);
		this._selectContent(linkIndex);
		this._showNextButton();
		this._showPreviousButton();
		this._showSeparator();
		this._showImagePanel(tabIndex);
		this._showNoImagePanel(tabIndex);
		return;
	},

	_selectTab: function(index, tabIndex) {
		this._findTabs().removeClass(this._selectedTab);
		this._findTabLinks().removeClass(this._selectedLink);
		this._findTabs().eq(tabIndex).addClass(this._selectedTab);
		this._findTabLinks().eq(index).addClass(this._selectedLink);
	},

	_selectContent: function(index) {
		this._findContentContainers().hide();
		var $content = this._findContentContainers().eq(index);
		if (this._isHeaderContent(index)) {
			// item with a subitem
			$content.find(this._contentClass).eq(0).show();
		}
		this._showParentContent(index);
		$content.show();
	},

	_showParentContent: function(index) {
		var $content = this._findContentContainers().eq(index);
		$content.parents(this._contentClassFilter).show();
		//if (this._showContentHeaderOnAllSubItems && this._isSubContent(index)) {
		//    $content.parents(this._contentClassFilter).show();
		//} else if (this._showContentHeaderOnFirstSubItem && this._isFirstSubContent(index)) {
		//    $content.parents(this._contentClassFilter).show();
		//}
	},

	_findParentTabIndex: function(elt) {
		var idToFind = "#" + elt.id;
		var elts = this._findTabs();
		for (i = 0; i < elts.length; i++) {
			if (elts.eq(i).find(idToFind).length > 0) {
				return i;
			}
		}
		return null;
	},

	_findLinkIndex: function(elt) {
		var idToFind = elt.id;
		var elts = this._findTabLinks();
		for (i = 0; i < elts.length; i++) {
			if (elts[i].id == idToFind) {
				return i;
			}
		}
		return null;
	},

	_onTabClick: function(evt) {
		if (this._redirectToLogin) {
			this._goToLogin();
		}
		else {
			evt.stopPropagation();
			evt.preventDefault();
			var tabIndex = this._findParentTabIndex(evt.target);
			var linkIndex = this._findLinkIndex(evt.target);
			this._selectTabAndContent(linkIndex, tabIndex);
			scroll(0, 0);
		}
		return false;
	},

	_goToLogin: function() {
		window.location = this._loginUrl;
	},

	_onNextClick: function(evt) {
		if (this._redirectToLogin) {
			this._goToLogin();
		}
		else {
			evt.stopPropagation();
			evt.preventDefault();
			var info = this._getNextStep();
			if (info != null) {
				this._selectTabAndContent(info.linkIndex, info.tabIndex);
			}
			scroll(0, 0);
		}
		return false;
	},

	_onPreviousClick: function(evt) {
		if (this._redirectToLogin) {
			this._goToLogin();
		}
		else {
			evt.stopPropagation();
			evt.preventDefault();
			var info = this._getPreviousStep();
			if (info != null) {
				this._selectTabAndContent(info.linkIndex, info.tabIndex);
			}
			scroll(0, 0);
		}
		return false;
	},

	_showNextButton: function() {
		var info = this._getNextStep();
		var button = this._findNextButton();
		if (info == null) {
			button.hide();
		} else {
			button.show();
		}
	},

	_showSeparator: function() {
		var next = this._getNextStep();
		var previous = this._getPreviousStep();
		var separator = this._findSeparator();
		// using style.display to avoid having the separator
		// show up as a block element for display purposes
		if (next == null || previous == null) {
			separator[0].style.display = 'none';
		} else {
			separator[0].style.display = 'inline';
		}
	},

	_showPreviousButton: function(tabIndex) {
		var info = this._getPreviousStep();
		var button = this._findPreviousButton();
		if (info == null) {
			button.hide();
		} else {
			button.show();
		}
	},

	_showImagePanel: function(tabIndex) {
		var panel = this._findImagePanel();
		if (!this._imageNull && tabIndex == 0) {
			panel.show();
		} else {
			panel.hide();
		}
	},

	_showNoImagePanel: function(tabIndex) {
		var panel = this._findNoImagePanel();
		if (this._imageNull || tabIndex != 0) {
			panel.show();
		} else {
			panel.hide();
		}
	},

	_getNextStep: function() {
		var info = this._getCurrentState();
		var lastIndex = this._findTabLinks().length - 1;
		if (info == null || info.linkIndex >= lastIndex) {
			return null;
		}
		if (this._isHeaderContent(info.linkIndex)) {
			info.linkIndex = info.linkIndex + 2;
			if (info.linkIndex >= lastIndex) {
				return null;
			}
		} else {
			info.linkIndex++;
		}
		var nextLink = this._findTabLinks()[info.linkIndex];
		info.tabIndex = this._findParentTabIndex(nextLink);
		return info;
	},

	_getPreviousStep: function() {
		var info = this._getCurrentState();
		var totalIndexes = this._findTabLinks();
		if (info == null || info.linkIndex <= 0) {
			return null;
		}
		if (this._isFirstSubContent(info.linkIndex)) {
			info.linkIndex = info.linkIndex - 2;
			if (info.linkIndex < 0) {
				return null;
			}
		} else {
			info.linkIndex--;
		}
		var nextLink = this._findTabLinks()[info.linkIndex];
		info.tabIndex = this._findParentTabIndex(nextLink);
		return info;
	},

	_getCurrentState: function() {
		var info = this._findCurrentSelectedTabIndex();
		return info;
	},

	_findCurrentSelectedTabIndex: function() {
		var info = null;
		var elts = this._findTabs();
		if (elts.length > 0) {
			info = [];
		}
		for (i = 0; i < elts.length; i++) {
			if (elts.eq(i).hasClass(this._selectedTab)) {
				info.tabIndex = i;
			}
		}
		var elts2 = this._findTabLinks();
		for (i = 0; i < elts2.length; i++) {
			if (elts2.eq(i).hasClass(this._selectedLink)) {
				info.linkIndex = i;
			}
		}
		return info;
	},

	_isHeaderContent: function(index) {
		var $content = this._findContentContainers().eq(index);
		if ($content.find(this._contentClass).length > 0) {
			// header item, i.e. item with a subitem
			return true;
		}
		return false;
	},

	_isFirstSubContent: function(index) {
		var $content = this._findContentContainers().eq(index);
		var $parent = $content.parents(this._contentClassFilter);
		var isEqual = !$parent.find(this._contentClassFilter).not($content).length;
		if ($parent.length > 0 && isEqual) {
			// first subitem
			return true;
		}
		return false;
	},

	_isSubContent: function(index) {
		var $content = this._findContentContainers().eq(index);
		var $parent = $content.parents(this._contentClassFilter);
		if ($parent.length > 0) {
			return true;
		}
		return false;
	},

	_findContentContainers: function() {
		return $(this._findMainContentPanel()).find(this._contentClass);
	},

	_findTabs: function() {
		return $(this._findMainTabContainer()).find(this._tabClass);
	},

	_findTabLinks: function() {
		return $(this._findMainTabContainer()).find(this._tabLink);
	},

	_findPanels: function() {
		return $(this._findMainTabContainer()).find(this._contentDetailPanel);
	},

	_findNextButton: function() {
		return $("#" + this._nextButton).eq(0);
	},

	_findSeparator: function() {
		return $("#" + this._separator).eq(0);
	},

	_findPreviousButton: function() {
		return $("#" + this._previousButton).eq(0);
	},

	_findImagePanel: function() {
		return $("#" + this._imagePanel).eq(0);
	},

	_findNoImagePanel: function() {
		return $("#" + this._noImagePanel).eq(0);
	},

	_findMainTabContainer: function() {
		return $(this._mainTabContainer);
	},

	_findMainContentPanel: function() {
		return $(this._mainContentContainer);
	},

	get_NextButton: function() {
		return this._nextButton;
	},

	set_NextButton: function(val) {
		this._nextButton = val;
	},

	get_Separator: function() {
		return this._separator;
	},

	set_Separator: function(val) {
		this._separator = val;
	},

	get_PreviousButton: function() {
		return this._previousButton;
	},

	set_PreviousButton: function(val) {
		this._previousButton = val;
	},

	get_RedirectToLogin: function() {
		return this._redirectToLogin;
	},

	set_RedirectToLogin: function(val) {
		this._redirectToLogin = val;
	},

	get_LoginUrl: function() {
		return this._loginUrl;
	},

	set_LoginUrl: function(val) {
		this._loginUrl = val;
	},

	get_ImagePanel: function() {
		return this._imagePanel;
	},

	set_ImagePanel: function(val) {
		this._imagePanel = val;
	},
	get_NoImagePanel: function() {
		return this._noImagePanel;
	},

	set_NoImagePanel: function(val) {
		this._noImagePanel = val;
	},
	get_ImageNull: function() {
		return this._imageNull;
	},

	set_ImageNull: function(val) {
		this._imageNull = val;
	}
}

Nyas.ClassLibrary.Behaviors.TabbedContent.registerClass('Nyas.ClassLibrary.Behaviors.TabbedContent', AjaxControlToolkit.BehaviorBase);
if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();