/*
 * Manages Tab Display Functionality
 *
 * Creates the Tabs in the interface.
 */

function createTab(name, title) {
	var tab = document.createElement('a');
	tab.id = "tab-"+name;
	tab.href="javascript:Events.triggerEvent('changetab','"+name+"')";
	tab.appendChild(document.createTextNode(title));
	return tab;
}

function addTabPanel(parent) {
	if(TABS_SHOW_PANEL) {
		for(var i = 0; i < TABS_TITLES.length; i++) {
			parent.appendChild(createTab(TABS_TITLES[i], TABS_TITLES[i]));
		}
		parent.getElementsByTagName('a')[0].className = 'selected';
		parent.style.visibility = 'visible';
	}
	
}


function TabManager(titleBarObj, hiddenClassName, visibleClassName, EventManager) {
	var tabs = new Array();
	var MySelf = this;
	var lastTab;
	var currentTab;

	this.getFirstTab = function() {
		for(var t in tabs) {
			return t;
		}
	}
	

	this.addTab = function(tabName, tabObject) {
		tabs[tabName] = tabObject;
	}

	this.removeTab = function(tabName) {
		MySelf.hideAllTabs();
		if(tabs[tabName]) {
			delete tabs[tabName];
		}
		MySelf.showLastTab();
	}
	
	this.hideAllTabs = function() {
		for(var t in tabs) {
			/*tabs[t].scrollTop = 0;
			tabs[t].scrollLeft = 0;*/
			var tab = document.getElementById(tabs[t]);	
			tab.className = hiddenClassName;
			tab.scrollTop = 0;
			tab = null;
		}
	}

	this.getLastTab = function() {
		return lastTab;
	}

	this.getCurrentTab = function() {
		return currentTab;
	}

	this.showTab = function(tabName) {
		if(tabName == currentTab) { return true; }
		MySelf.hideAllTabs();
		if(tabs[tabName]) {
			document.getElementById(tabs[tabName]).className = visibleClassName;
			document.getElementById(titleBarObj).innerHTML =new String(tabName).toUpperCase();
			lastTab= currentTab;
			currentTab = tabName;
		}
	}

	this.showLastTab = function() {
		if(!tabs[lastTab]) {
			lastTab = MySelf.getFirstTab();
		}
		EventManager.triggerEvent('changetab', lastTab);
	}	

	this.checkTab = function(tName) {
		return (tabs[tName]);
	}

	this.alertTabs = function () {
		var message = '';
		for(t in tabs) {
			message=message+t+' '+tabs[t]+'\n';
		}
		alert(message);
	}
	
}
