// JavaScript Document for a simple tabbing system
/*
	This should be really simple to use.
	
	For a tab set, make a bunch of links in an unordered list.
	Give the links ids that begin [some prefix consistent for all of this tab set]_tab_[any identifier for this specific tab]
	
	Now, anywhere else in the document, create matching divs with your content in
	Give them ids that begin [some prefix consistent for all of this tab set]_pane_[any identifier for this specific tab]
	
	So you should have every id for the set beginning the same and then pairs of ids only varying by _tab_ and _pane_ - this variance links them both.
	
	Now create a "current" class for the parent li of the links. Use it to style however you want them turned on.
	
	Finally, onload, call somevar=new SimpleTabs("[some prefix consistent for all of this tab set]");
*/

function SimpleTabs(idPrefix, selectedTab, onClicked) {
	/* Get the tabs and the panes */
	this.idPrefix=idPrefix;
	this.allTabs=getElementsByTagAndIDPrefix("a", idPrefix+"_tab_");
	this.allPanes=getElementsByTagAndIDPrefix("div", idPrefix+"_pane_");
	this.selectedTab = 0;
	this.onClicked=onClicked;
	
	/* Warn if there are any errors - this should help in debugging */
	if (this.allTabs.length==0) {
		// window.alert("Error: SimpleTabs can't find any links beginning with the id "+idPrefix+"_tab_");
	}
	
	if (this.allPanes.length==0) {
		//window.alert("Error: SimpleTabs can't find any divs beginning with the id "+idPrefix+"_pane_");
	}
	
	if (this.allTabs.length!=this.allPanes.length) {
		// window.alert("Error: SimpleTabs found "+this.allTabs.length+" tab buttons and "+this.allPanes.length+" tab panes - they should be the same.");
	}
	
	for (stCount=0; stCount<this.allTabs.length; stCount++) {
		// Store a reference to this SimpleTabs object
		this.allTabs[stCount].stRef=this;
		
		// Hook the onclick event up
		this.allTabs[stCount].onclick=function() {
			this.stRef.tabClicked(this);
			return false;
		}
	}
	
	if (!selectedTab) {
		this.setTab(0); 			// Initialize by turning on the first tab.
	} else {
		this.setTab(selectedTab); 	// Initialize by selecting specified tab.
	}
}

SimpleTabs.prototype.setTab=function(tabIndex) {
	
	// This ensures arrays won't go out of range.
	if (this.allTabs.length!=this.allPanes.length) return false; 
	
	// Loop through the tabs and panes
	for (stSetCount=0; stSetCount<this.allTabs.length; stSetCount++) {
		currentTab=this.allTabs[stSetCount];
		currentPane=this.allPanes[stSetCount];
		parentLi=getFirstParentNodeOfTagName(currentTab, "li");
		
		// If the current one is wanted, turn it on
		if (stSetCount==tabIndex) {
			addClass(parentLi, "current");
			showObj(currentPane);
		} else { // Otherwise, turn it off
			removeClass(parentLi, "current");
			hideObj(currentPane);
		}
	}
	
	// remember the currently selected tab
	this.selectedTab = tabIndex;
}

// Get the tab index for a given object
// Return -1 if not found
SimpleTabs.prototype.getTabIndex=function(obj) {
	if (typeof obj == "string") {
		obj=document.getElementById(obj);
	}
	if (typeof obj == "undefined") return -1;
	if (obj==null) return -1;
	
	for (stGetCount=0; stGetCount<this.allTabs.length; stGetCount++) {
		if (this.allTabs[stGetCount]==obj) {
			return stGetCount;
		}
	}
	return -1;
}

SimpleTabs.prototype.getTab=function(tabIndex)
{
	return this.allTabs[tabIndex];
}

SimpleTabs.prototype.getPane=function(tabIndex)
{
	return this.allPanes[tabIndex];
}

SimpleTabs.prototype.currentTab=function()
{
	return this.selectedTab;
}

SimpleTabs.prototype.tabClicked=function(obj) {
	// Find the passed obj's index amongst the tabs.
	tabIndex=this.getTabIndex(obj);
	
	// Deal with it if it wasn't found.
	if (tabIndex==-1) {
		// window.alert("Error: SimpleTabs.tabClicked had an unrecognized tab passed.");
	} else { // Otherwise, set it.
		this.setTab(tabIndex);
		
		// call the "onClicked" method if set
		if (this.onClicked != null)
		{
			this.onClicked(tabIndex, obj);
		}
	}
}
