/*

	The javascript that controls the DHTML tab functionality

*/

var selfTabObject;

TabObject = function() {
	this.variables = new Object();
	document.selfTabObject = this; 
}

TabObject.prototype = {

	setVariable: function(name, value){
		this.variables[name] = value;
	},
	getVariable: function(name){
		return this.variables[name];
	},
	getVariables: function(){
		return this.variables;
	},
	
	initTabs: function() {
		var allAnchors = document.getElementsByTagName("a");
		var firstValidAnchor;
		
	    for (var i=0; i<allAnchors.length; i++) {
			var anchor = allAnchors[i];
			
			if(anchor.className != "tabpage") {
				continue;
			};
			
			if(firstValidAnchor == undefined) {
				firstValidAnchor = anchor;
			}
			
			// see function on global.js
			__attachEvent(anchor, "click", this.onTabClick);
		}
		
		this.doTabClick(firstValidAnchor);
	},
	
	onTabClick: function(e) {
		var newTab = e.target ? e.target : e.srcElement; 
		var _this = document.selfTabObject;
		
		_this.doTabClick(newTab);
	},
	
	doTabClick: function(e) {
		var newTab = e;
		var currentTableId, newTableId;
		var currentTab = this.getVariable("currentTab");
		
		if(currentTab == newTab) {
			return;
		};
		
		if(currentTab != undefined) {
			var currentTabLi = currentTab.parentNode;
			currentTabLi.className = "";
			currentTableId = currentTab.id.substr(currentTab.id.indexOf("table"));
		};
		
		var newTabLi = newTab.parentNode;
		newTabLi.className = "current-tab";
		
		newTableId = newTab.id.substr(newTab.id.indexOf("table"));
		
		this.toggleTable(newTableId, currentTableId);
		this.setVariable("currentTab", newTab);
	},
	
	toggleTable: function(newTableId, currentTableId) {
		document.getElementById(newTableId).style.display = "inline";
		if(currentTableId) {
			document.getElementById(currentTableId).style.display = "none";
		}
	}
	
}