//tab_switch_control.js

function CTabSwitchInfo( strId, strSelectedClassName, strNotSelectedClassName, strListControlId)
{
	this.m_strId = strId;
	this.m_strSelectedClassName = strSelectedClassName;
	this.m_strNotSelectedClassName = strNotSelectedClassName;
	this.m_strListControlId = strListControlId;

	this.Id = function()
	{
		return this.m_strId;
	}
	this.SelectedClassName = function()
	{
		return this.m_strSelectedClassName;
	}
	this.NotSelectedClassName = function()
	{
		return this.m_strNotSelectedClassName;
	}
	this.ListControlId = function()
	{
		return this.m_strListControlId;
	}

	this.Activate = function()
	{
		var div = GetIdItem( this.Id(), null);
		if ( div)
			div.className = this.SelectedClassName();
		var listControl = FindListControl( this.ListControlId());
		if ( listControl)
		{
			listControl.SetVisible();
			listControl.SetFocus();
		}
	}
	this.Deactivate = function()
	{
		var div = GetIdItem( this.Id(), null);
		if ( div)
			div.className = this.NotSelectedClassName();
		var listControl = FindListControl( this.ListControlId());
		if ( listControl)
			listControl.SetNotVisible();
	}
}

function CTabSwitchControl( strId, strActiveTabId, strActiveTabHiddenInputName)
{
	this.Init = CHash;
	this.Init();

	this.m_strId = strId;
	this.m_strActiveTabId = strActiveTabId;
	this.m_strActiveTabHiddenInputName = strActiveTabHiddenInputName;

	this.Id = function()
	{
		return this.m_strId;
	}

	this.ActiveTab = function()
	{
		return this.m_strActiveTabId;
	}

	this.SetNewActiveTab = function( strTabId)
	{
		if ( this.m_strActiveTabId != strTabId)
		{
			var newTabInfo = this.Item( strTabId);
			if ( newTabInfo)
			{
				var oldTabInfo = this.Item( this.m_strActiveTabId);
				if ( oldTabInfo)
					oldTabInfo.Deactivate();

				newTabInfo.Activate();
				this.m_strActiveTabId = strTabId;
			}
		}
	}
	this.ActiveTabHiddenInputName = function()
	{
		return this.m_strActiveTabHiddenInputName;
	}

	this.AddTab = function( strId, strSelectedClassName, strNotSelectedClassName, strListControlId)
	{
		this.Set( strId, new CTabSwitchInfo( strId, strSelectedClassName, strNotSelectedClassName, strListControlId));
	}

	this.OnClick = function( strTabId)
	{
		this.SetNewActiveTab( strTabId);
	}
	this.SaveActiveTabPositionInHidden = function()
	{
		if ( this.ActiveTabHiddenInputName())
			SetItemListValue( GetNamedItems( this.ActiveTabHiddenInputName(), null), this.ActiveTab());
	}
}

var s_hashTabSwitchControl;
s_hashTabSwitchControl = new CHashWithKeys();

function FindTabSwitchControl( strControlId)
{
	return s_hashTabSwitchControl.Item( strControlId);
}

function CreateTabSwitchControl( strControlId, strActiveTabId, strActiveTabHiddenInputName)
{
	var tabControl = new CTabSwitchControl( strControlId, strActiveTabId, strActiveTabHiddenInputName);
	s_hashTabSwitchControl.Set( strControlId, tabControl);
	return tabControl;
}

function OnClickActivateTab( strControlId, strTabId)
{
	var tabControl = FindTabSwitchControl( strControlId);
	if ( tabControl)
		tabControl.OnClick( strTabId);
}

function ActiveTab( strControlId)
{
	var strTabId = '';
	var tabControl = FindTabSwitchControl( strControlId);
	if ( tabControl)
		strTabId = tabControl.ActiveTab();
	return strTabId;
}

function SaveAllActiveTabPositionInHidden()
{
	var arKey = s_hashTabSwitchControl.Keys();
	var i;
	for ( i = 0; ( i < arKey.Count()); i++)
	{
		var tabControl = s_hashTabSwitchControl.Item( arKey.Item( i));
		tabControl.SaveActiveTabPositionInHidden();
	}
}