﻿/*--------------------------------------------------
	TABS Framework
--------------------------------------------------*/

var TABS = new Array();

// Objekt skupiny záložek
function TABSGroup(name, tagState)
{
	this.name = name;
	this.tabs = new Array();
	this.state = tagState;
	this.tagState = null;
	
	// Inicializace
	this.init = function()
	{
		if (this.state != null) this.tagState = FindTag(this.state);
		this.restore();
	}

	// Prida zalozku
	this.addTab = function(name, tagHead, tagContent)
	{
		if (this.tabs[name] != null) return this.tabs[name];
		var item = new TABSItem(this, name, tagHead, tagContent);
		this.tabs[this.tabs.length] = item;
		this.tabs[name] = item;
		return item;
	}

	// Zobrazi/zaktivni zadanou zalozku	
	this.showTab = function(name)
	{
		if (this.tabs[name] == null) return false;
		if (!this.tabs[name].hasTags()) return false;
		for (var n in this.tabs) {
			var t = this.tabs[n];
			if (t == null) continue;
			if (t.name == name) {
				t.show();
				if (this.tagState != null) {
					this.tagState.value = t.name;
				}
			} else {
				t.hide();
			}
		}
		return true;
	}

	// Povoli zadanou zalozku	
	this.enableTab = function(name)
	{
		if (this.tabs[name] == null) return false;
		if (!this.tabs[name].hasTags()) return false;
		this.tabs[name].enable();
	}

	// Zakaze zadanou zalozku	
	this.disableTab = function(name)
	{
		if (this.tabs[name] == null) return false;
		if (!this.tabs[name].hasTags()) return false;
		this.tabs[name].disable();
	}
	
	// Obnoví poslední stav záložek
	this.restore = function()
	{
		if (this.tagState != null) {
			var tabName = this.tagState.value;
			if ((tabName == null || tabName == "") && this.tabs.length > 0) tabName = this.tabs[0].name;
			return this.showTab(tabName);
		}
		return false;
	}
	
	this.getFirstEnabledTab = function()
	{
		for (var n in this.tabs) {
			var t = this.tabs[n];
			if (t != null && t.enabled === true) return t;
		}
		return null;
	}	
	
	this.init();
}

// Objekt jednotlivé záložky
function TABSItem(group, name, tagHead, tagContent)
{
	this.group = group;
	this.name = name;
	this.head = tagHead;
	this.content = tagContent;
	this.tagHead = null;
	this.tagContent = null;
	this.active = false;
	this.enabled = true;

	// Inicializace
	this.init = function()
	{
		this.initTags();
	}
	
	// Inicializace tagů
	this.initTags = function()
	{
		if (this.tagHead == null) this.tagHead = FindTag(this.head);
		if (this.tagContent == null) this.tagContent = FindTag(this.content);
	}

	// Zobrazi zalozku	
	this.show = function()
	{
		this.initTags();
		if (this.tagHead != null) this.tagHead.className = "tabHeadActive";
		if (this.tagContent != null) this.tagContent.style.display = "block";
		this.active = true;
	}

	// Schova zalozku	
	this.hide = function()
	{
		this.initTags();
		if (this.tagHead != null) this.tagHead.className = "tabHead";
		if (this.tagContent != null) this.tagContent.style.display = "none";
		this.active = false;
	}

	// Povoli zalozku
	this.enable = function()
	{
		this.initTags();
		if (this.tagHead != null) this.tagHead.style.display = "inline";
		if (this.tagContent != null) this.tagContent.style.display = (this.active === true ? "block" : "none");
		this.enabled = true;
	}

	// Zakaze zalozku	
	this.disable = function()
	{
		this.initTags();
		if (this.tagHead != null) this.tagHead.style.display = "none";
		if (this.tagContent != null) this.tagContent.style.display = "none";
		this.enabled = false;
		if (this.active === true) {
			var tmpTab = this.group.getFirstEnabledTab();
			if (tmpTab != null) this.group.showTab(tmpTab.name);
		}
	}
	
	// Je záložka obsažena na stránce
	this.hasTags = function()
	{
		this.initTags();
		return (this.tagHead != null || this.tagContent != null);
	}
	
	this.init();
}

// Registrace záložek (celek)
function RegisterTabsGroup(name, tagState)
{
	if (TABS[name] != null) return TABS[name];
	var tab = new TABSGroup(name, tagState);
	TABS[name] = tab;
	return tab;
}

// Obnoví poslední stav záložek (celek)
function RestoreTabsGroup(name)
{
	if (TABS[name] == null) return false;
	return TABS[name].restore();
}

// Registrace záložky (jedna)
function RegisterTabItem(groupName, name, tagHead, tagContent)
{
	if (TABS[groupName] == null) return null;
	return TABS[groupName].addTab(name, tagHead, tagContent);
}

// Zobrazení záložky
function ShowTab(groupName, name)
{
	if (TABS[groupName] == null) return;
	TABS[groupName].showTab(name);
}

// Povolení záložky
function EnableTab(groupName, name)
{
	if (TABS[groupName] == null) return;
	TABS[groupName].enableTab(name);
}

// Zakázání záložky
function DisableTab(groupName, name)
{
	if (TABS[groupName] == null) return;
	TABS[groupName].disableTab(name);
}
