﻿// ##########################################################################
// 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=0;index<aTabs.length;index++)
		{
				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+'\')': '')
				+'" '+ ((typeof(Tab.oConfig.tabwidth) != "undefined") ? 'style="width:'+((typeof(aTabs[index].tabwidth) != "undefined") ? aTabs[index].tabwidth : 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();
		//insertEvent(window,"load",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;
		
		//tagA.style.height = tagA.offsetHeight + 1 + "px";
		//tagA.style.width = tagA.offsetWidth + 6 + "px";
		//tagLI.style.marginTop = "-1px";
		//tagLI.style.marginLeft = ((nTab==1) && document.all) ? "-1px" : "-2px";
		//tagLI.style.marginRight = "-3px";
		//tagLI.style.top = "1px";
		tagLI.style.zIndex = $("tab-menu").getElementsByTagName("li").length+1;
		tagLI.className = "clicked";
		tagDIV.className = "clicked";
		tagA.className = "active";
		//tagA.style.padding = "5px 0 6px 0px";
	},
	
 	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;
		
		//tagA.style.width = tagA.offsetWidth - 6 + "px";
		//tagA.style.height = tagA.offsetHeight - 1 + "px";
		//tagLI.style.marginTop = "0";
		//tagLI.style.marginLeft = "1px";
		tagLI.style.marginRight = "0";
		tagLI.style.top = "0";
		tagLI.style.zIndex = Tab.tabActive;
		tagLI.className = "notclicked";
		tagDIV.className = "notclicked";
		tagA.className = "over";
		//tagA.style.padding = "1px 0";
		
	},
	
	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;
	}
}

var $ = function(id) 
{
	if(!arguments[1]) return document.getElementById(id);
	else document.getElementById(id).style[arguments[1]] = arguments[2];
}		

function insertEvent(oElement,szType, oFunction, param)
{
	if (oElement.addEventListener) {
		oElement.addEventListener (szType,oFunction,false);
	} else if (oElement.attachEvent) {
		oElement.attachEvent ('on'+szType,oFunction)
	} else {
		eval ("oElement."+szType+"= oFunction");
	}
}



function showTabNum(tab)
{
	for(i=1;i<=10;i++)
		if (document.getElementById("txttab"+i) != null)
			document.getElementById("txttab"+i).style.display = 'none'
	
	if (document.getElementById("txttab"+tab) != null)
		document.getElementById("txttab"+tab).style.display = 'block'
}	