// These variables must be set for every implementation of this javascript on a page
// The Tabset param can be ignored if this javascript is used only once on a page .
// If not ignored, append the tabset to the end of all variables below.

// req tabName				name of each tab = "<tabName>+ID"
// req layerName			name of each layer = "<layerName>+ID"
// req tabStyle				supports "image", "class", or "char"
// req tabDefaultName		className or imageVar when tab is in default state
// req tabSelectName		className or imageVar when tab is selected
// opt tabMouseoverName		className or imageVar when tab is hovered over (optional)
// opt isMenu				set to true to allow more than one tab to select at once (optional)

var aSelected = new Array();
var aHighlighted = new Array();
aSelected[""] = -1;
aHighlighted[""] = -1;

// Should be called for any tabSet
function initTabSet(tabSet) {
	aSelected[tabSet] = -1;
	aHighlighted[tabSet] = -1;
}

function init(tabNumber, tabSet) {
	// Set the tab to the default state
	tabSet = checkTabSet(tabSet);
	changeTab(eval("tabName" + tabSet), eval("tabDefaultName" + tabSet), tabNumber, tabSet);
}

function Highlight (tabNumber, tabSet) {
	// High
	tabSet = checkTabSet(tabSet);
	// Unhighlight previously highlighted tab
	UnHighlight(tabSet);
	if (!isSelected(tabNumber, tabSet)) {
		changeTab(eval("tabName" + tabSet), eval("tabMouseoverName" + tabSet), tabNumber, tabSet);
		aHighlighted[tabSet] = tabNumber;
	}
}
function UnHighlight (tabSet) {
	tabSet = checkTabSet(tabSet);
	if (aHighlighted[tabSet] > -1) {
		init(aHighlighted[tabSet], tabSet);
		aHighlighted[tabSet] = -1;
	}
}

function TurnTabOn (tabNumber, tabSet) {
	// Selects a tab if not already selected
	tabSet = checkTabSet(tabSet);
	isMulti = checkVar("isMenu" + tabSet, false);
	if (!isSelected(tabNumber, tabSet)) {
		// If this isn't a menu, can only select 1 tab at once.  Turn off previously selected tab.
		if (!isMulti) TurnTabOff(aSelected[tabSet], tabSet);
		aSelected[tabSet] = tabNumber;
		aHighlighted[tabSet] = -1;
		changeTab(eval("tabName" + tabSet), eval("tabSelectName" + tabSet), tabNumber, tabSet);
		//alert("TurnOn:" + eval("layerName" + tabSet) + tabNumber);
		document.getElementById(eval("layerName" + tabSet) + tabNumber).style.display = "block";
		return true;
	}
	return false;
}
function TurnTabOff (tabNumber, tabSet) {
	tabSet = checkTabSet(tabSet);
	if (tabNumber > -1) {
		aSelected[tabSet] = -1;
		init(tabNumber, tabSet);
		document.getElementById(eval("layerName" + tabSet) + tabNumber).style.display = "none";
	}
}

function isSelected (tabNumber, tabSet) {
	return (document.getElementById(eval("layerName" + tabSet) + tabNumber).style.display == "block");
}

// Never call these functions directly
function changeTab (tabName, newLook, tabNumber, tabSet) {
	var thisTab = document.getElementById(tabName + tabNumber);
	var thisStyle = eval("tabStyle" + tabSet);
	// Replace the string <id> with the tabNumber
	newLook = newLook.replace('<id>', tabNumber);

	// This can be expanded if the tab style does not work with any of these or does other things
	if (thisStyle == "image") {
		// With images, we will treat the newLook as a variable name containing the image
		thisTab.src = eval(newLook);
	} else if (thisStyle == "class") {
		thisTab.className = newLook;
	} else if (thisStyle == "char") {
		thisTab.innerHTML = newLook;
	}
}
function checkTabSet(tabSet) {
	if (tabSet == undefined) return "";
	return tabSet;
}
function checkVar(varName, varDefault) {
	// Checks if a variable exists, else returns a default value
	try {
		return eval(varName);
	} catch (e) {
		return varDefault;
	}
}