// ##########################################################################
// Tab System v.1
// Autor: Rafael Faria
// Website: http://www.rafaelfaria.com.br/
// E-mail: rafaelfaria@gmail.com
// ##########################################################################

Tab = 
{
	tabActive:-1,
	aTabs:Object,
	oConfig:Object,
	defaultTab:"",
	
	init: function(_oTag)
	{
		if (_oTag.tabs != null)
			Tab.aTabs = _oTag.tabs;
		
		Tab.oConfig = _oTag.config

		if (Tab.defaultTab == "")
			if(typeof(Tab.oConfig.defaultTab) != "undefined") Tab.defaultTab = Tab.oConfig.defaultTab;

		// Write the tabs
		Tab.writeTabs();
	},
	
	writeTabs: function() 
	{
		var HTML = "";
		HTML += '<ul id="tab-menu">';
		var aTabs = Tab.aTabs;

		for (index in aTabs)
		{
			if(isNaN(index)) continue;
			
			HTML += '<li class="notclicked"><div class="notclicked"><a href="javascript:Tab.openTab('+(parseInt(index)+1)+');'
			+((typeof(aTabs[index].func) != "undefined") ? aTabs[index].func: '')
			+((typeof(aTabs[index].link) != "undefined") ? ';Tab.gotoURL(\''+aTabs[index].link+'\',\''+aTabs[index].target+'\')': '')
			+'" style="'+((typeof(aTabs[index].width) != "undefined") ? 'width:'+aTabs[index].width : ((Tab.oConfig.tabwidth != "undefined") ? 'width:'+Tab.oConfig.tabwidth : ''))+'"'
			+'class="over">'+aTabs[index].label+'</a></div></li>';
		}
		HTML += '</ul>';
		
		if(typeof(Tab.oConfig.showcontent) != "undefined")
			if (Tab.oConfig.showcontent)
				HTML += '<div id="'+((typeof(Tab.oConfig.contentname) != "undefined") ? Tab.oConfig.contentname : 'tabcontent')+'"></div>';

		if(typeof(Tab.oConfig.width) != "undefined")
			$("tabs").style.width = Tab.oConfig.width;
		
		$("tabs").innerHTML =  HTML;
		
		if ((typeof(aTabs[Tab.defaultTab-1].func) != "undefined")  && (Tab.oConfig.runfunctions))
			eval(aTabs[Tab.defaultTab-1].func)
			
		Tab.openTab();
		//document.observe("dom:loaded",Tab.openTab);
		
	},
	
	openTab : function (nTab)
	{
		if ((typeof(nTab) == "object") || (typeof(nTab) == "undefined")) nTab = Tab.defaultTab;
		
		// take the focus out
		document.getElementById("tabs").blur();
		
		Tab.clearTabs();
		
		Tab.tabActive = nTab-1;
	
		var tagLI = $("tab-menu").getElementsByTagName("li")[Tab.tabActive];
		var tagDIV = $("tab-menu").getElementsByTagName("li")[Tab.tabActive].firstChild;
		var tagA = $("tab-menu").getElementsByTagName("li")[Tab.tabActive].firstChild.firstChild;
		
		tagLI.style.zIndex = $("tab-menu").getElementsByTagName("li").length+1;
		tagLI.className = "clicked";
		tagDIV.className = "clicked";
		tagA.className = "active";
	},
	
 	clearTabs : function()
	{
		if (Tab.tabActive == -1) return;
		
		var tagLI = $("tab-menu").getElementsByTagName("li")[Tab.tabActive];
		var tagDIV = $("tab-menu").getElementsByTagName("li")[Tab.tabActive].firstChild;
		var tagA = $("tab-menu").getElementsByTagName("li")[Tab.tabActive].firstChild.firstChild;
		
		tagLI.style.marginRight = "0";
		tagLI.style.top = "0";
		tagLI.style.zIndex = Tab.tabActive;
		tagLI.className = "notclicked";
		tagDIV.className = "notclicked";
		tagA.className = "over";
		
	},
	
	gotoURL: function(_szURL, _szTarget)
	{
		if (_szTarget != "undefined")
		{
			if (_szTarget == "top")
				top.location.href = _szURL
			else if (_szTarget == "_blank")
				window.open(_szURL)
			else
				window.frames[_szTarget].location.href = _szURL
		}
		else
			location.href = _szURL;
	}
}


function showTabNum(tab)
{
	
	if ($("tab-menu") != null)
		ntabs = $("tab-menu").getElementsByTagName("li").length;
	else
		ntabs = 10;
	
	for(i=1;i<=ntabs;i++)
		if ($("txttab"+i) != null)
			$("txttab"+i).style.display = 'none'
	
	if ($("txttab"+tab) != null)
		$("txttab"+tab).style.display = 'block'
}	