/****************
Author: Khalifah Shabazz

Date started: 09/21/2006

Date Updated: 09/21/2006

Script purpose:
	The function of this script it to hide and show tabs with a transitional affect. As oppsosed 
to clicking a link and haveing the info pop-up out of seemingly nowhere.

Credits & copy rights:
	This script was created by myself from scratch, so if you intend to use it as is, or modify it, 
Please give credit where it is due, and leave my info above intact when using, or distributing.
*****************/

var debugSwitch = true;	// easily turn you own personal debug messages on or off.
var tabsArray = new Array(); // array containg names of tabs for a page.
var vFlag = 'visible';	// the visivility flag and value;
var visibleTab = "";	// tab that is currently visible;
var imageDir = "images/";

/* Better way to detect browser methof and retrieve html objects,
	without putting redundant if statment all through your code.
	How to use:
		1. Call this method/function, replacing idString with an HTML elements id.
		2. Checks to make sure the string is not null and that the element exist
			ex: getObject("debugWindow");
*/
function GetObject(idString)
{
	var theObj = null;
	
	if(idString == null) return false;
	//else
	if (document.getElementById)
		theObj = document.getElementById(idString);
	else if (document.all)
		theObj = document.all[idString];
	else if (document.layers)
		theObj = document.layers[idString];

	if (theObj) return theObj;
	
	return false;
}


function SwapBGImage(theElement, fileName)
{
	//alert("SBGI:theElement = " + theElement);
	var elementObj = GetObject(theElement);
	if (elementObj)
	{
		var theLink = "url('" + imageDir + fileName + "')";
		elementObj.style.backgroundImage = theLink;
		
		return  theLink;
	}
}

/* Add a tab on-the-fly */
function addTab(aTab)
{
	if (debugSwitch)	// what is the initial value of length, I'm curious to know (it's BEST NOT to aassume).
		addMsg(tabsArray.length);
	// Dynamically add elements at the end.
		// logic: Since arrays start at zero, and the length = 0 when it's empty, or length = arrayIndex +  1,
		// arrayIndex being equal to the index number of the last element in the array.
		// ex: arrayIndex = null then -> length = 0
		// ex: arrayIndex = 0 then -> length = 1
		// ex: arrayIndex = 1 then -> length = 2
		// so using the arrays own length will always place elements at the end.
	tabsArray[tabsArray.length] = aTab;

	// If the above statement is too confusing, then by all means, comment it out and use the commented out code statements below instead!
	/* // make sure there is an elemnt in 0 slot by increasing ONLY if their is more than 1 element.
	var numElements = 0;
	if (tabsArray.length > 0)
		numElements = tabsArray.length + 1;

	// add the tab to the array
	tabsArray[numElements] = aTab;  */

	if (debugSwitch)
		addMsg(tabsArray.length);

	// return the position the element was placed at.
	return tabsArray.length;
}


/* hide all visble tabs, except the calling tab */
function VisibleSwitch(theElement)
{
	theElement = GetObject("theBox");

	if (theElement != null)
	{
		if (vFlag == "visible")
		{
			vFlag = "hidden";
			theElement.style.visibility = vFlag;
		}
		else
		{
			vFlag = "visible";
			theElement.style.visibility = vFlag;
		}
	}

	return false;	//return falses is above fails.
}


function TabSwitcher(theTabPrefix)
{
	var tabSuffix = "_container";
	var bImg =  theTabPrefix + "_button";
	
	if (theTabPrefix != visibleTab) // Prevent tab from rendering its own content invisible.
	{
		// Get the objects.
		tab2Show = GetObject(theTabPrefix + tabSuffix);
		tab2Hide = GetObject(visibleTab + tabSuffix);
		
		// verify there are two objects before procceding.
		if (tab2Show != null && tab2Hide != null)
		{//I hope this is self explanatory.
			//This code is extra and optional, it changes tab buttons background to something more flashy
			SwapBGImage(visibleTab  + "_button", "tab_default.gif");
			SwapBGImage(bImg, bImg + ".gif");	//Button
			SwapBGImage(theTabPrefix + "_top", theTabPrefix + "_top.gif");		//top
			SwapBGImage(theTabPrefix + "_bg", theTabPrefix + "_bg.gif");		//top
			SwapBGImage(theTabPrefix + "_bottom", theTabPrefix + "_bottom.gif");//top
			
			//Hide current tab, then show the newly selected one
			tab2Hide.style.display = "none";
			tab2Show.style.display = "block";
			//Store cuurent visible tab in global var, for refrence next time.
			visibleTab = theTabPrefix;
			
			return true;
		}
	}
	return false;	//return falses is above fails.
}
