// JavaScript Document

function STabPane(pName, pHover, pCookie){
	
	this.name           = pName ? pName : "";
	this.hover          = pHover ? pHover : false;
	this.tabRow         = null;
	this.tabs           = new Array();
	this.pagesContainer = null;
	this.pages          = new Array();
	this.currentPage    = -1;
	
	this.tabStatusHidden = null;
	this.cookieManager   = pCookie ? pCookie : "";
	
	/*--*/
	
	STP_TAB_ROW_ELEMENT_NAME = "LI";
	STP_PAGE_ELEMENT_NAME = "DIV";
	
	/*--*/
	
	this.setTabRowContainer = function(pTabRow, elementsClasseName){
		
		if((typeof pTabRow) == "string")
			this.tabRow = document.getElementById(pTabRow);
		else if((typeof element) == "object")
			this.tabRow = pTabRow;
		
		var auxTab;
		var auxCont = 0;
		
		/* Capturando as tabs */
		if(elementsClasseName && elementsClasseName == ""){
			for (var i =0; i<this.tabRow.childNodes.length; i++){
				node = this.tabRow.childNodes[i];
				if(node.className == elementsClasseName){
					auxTab = new STab(node,true,this.hover);
					auxTab.main = this;
					auxTab.index = auxCont++;
					this.tabs.push( auxTab );
				}
			}
		}else{
			for (var i =0; i<this.tabRow.childNodes.length; i++){
				node = this.tabRow.childNodes[i];
				if(node.nodeName == STP_TAB_ROW_ELEMENT_NAME){
					auxTab = new STab(node,true,this.hover);
					auxTab.main = this;
					auxTab.index = auxCont++;
					this.tabs.push( auxTab );
				}
			}
		}
		
	}
	
	this.setPagesContainer = function (pPagesContainer, elementsClasseName){
		
		if((typeof pPagesContainer) == "string")
			this.pagesContainer = document.getElementById(pPagesContainer);
		else if((typeof element) == "object")
			this.pagesContainer = pPagesContainer;
		
		if(elementsClasseName && elementsClasseName == ""){
			for(var i=0; i<this.pagesContainer.childNodes.length; i++){
				node = this.pagesContainer.childNodes[i];
				if(node.className == elementsClasseName){
					node.style.display = "none";
					this.pages.push(node);
				}
			}	
		} else {
			for(var i=0; i<this.pagesContainer.childNodes.length; i++){
				node = this.pagesContainer.childNodes[i];
				if(node.nodeName == STP_PAGE_ELEMENT_NAME){
					node.style.display = "none";
					this.pages.push(node);
				}
			}
		}
		
	}
	
	this.config = function(pTabRow, pPagesContainer, TabRowClasseName, PagesClasseName){
		this.setTabRowContainer(pTabRow, TabRowClasseName);
		this.setPagesContainer(pPagesContainer, PagesClasseName);
	}
	
	this.showPage = function(pageNumber){
		if(this.tabs[pageNumber].enabled){
			for(var i = 0; i < this.tabs.length; i++){
				this.tabs[i].setShowing(false);
				this.pages[i].style.display = "none";
			}
			this.tabs[pageNumber].setShowing(true);
			this.pages[pageNumber].style.display = "";
		}
		this.currentPage = pageNumber;
		this.store();
	}
	
	this.store = function(){
		if(this.tabStatusHidden){
			this.tabStatusHidden = this.currentPage;
		}
		if(this.cookieManager){
			this.cookieManager.set(this.name, this.currentPage, 1);
		}
	}
	
	/* -- */
	
	this.normalize = function(){
		if(this.tabStatusHidden){
			if(this.tabStatusHidden.value == "") this.tabStatusHidden.value = 0;
			var page = parseInt(parseFloat(this.tabStatusHidden.value));
			if(page < 0) page = 0;
		}
		if(this.cookieManager && this.cookieManager.get(this.name)){
			page = this.cookieManager.get(this.name);
		}
		
		if(!page || page < 0)
			page = 0
			
		this.showPage(page);
	}
	
	this.disableTab = function(ind){
		tab = this.tabs[ind]
		tab.setEnabled(false);
		tab.setClassName('disabledTab');
	}
	
	this.enableTab = function(ind){
		tab = this.tabs[ind]
		tab.setEnabled(true);
		tab.setClassName('');
	}	
}


/*#########################################################################*/

function STab(el,en, pHover){
	this.index = 0;
	this.element = el;
	this.innerLink = null;
	this.enabled = en;
	this.showing = false;
	this.main = null;
	this.hover = pHover ? pHover : false;
	
	for(var i=0; i<node.childNodes.length; i++){
		child = node.childNodes[i];
		if(child.nodeName == 'A'){
			this.innerLink = child;
			if(this.innerLink.href.charAt(this.innerLink.href.length-1) == "#")
				this.innerLink.href ="javascript:void(0);";
		}
	}
	
	this.setEnabled = function(tf){
		this.enabled = tf;
	}
	
	this.getElement = function(){
		return this.element;	
	}
	
	this.setClassName = function(cn){
		this.element.parentNode.className = cn;	
	}
	
	this.setShowing = function(pStatus){
		this.showing = pStatus;
		if(this.showing){
			addClassProperty('activeTab', this.element);
		} else {
			removeClassProperty('activeTab', this.element);
		}
	}
	
	this.element.main = this;
	
	if(this.hover){
		this.element.onmouseover = function(){
			this.main.main.showPage(this.main.index);
		}
	} else {
		this.element.onclick = function(){
			this.main.main.showPage(this.main.index);
		}
	}
	
}
