/*-----------------------------------------------------------+
 | addLoadEvent: Add event handler to body when window loads |
 +-----------------------------------------------------------*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

/*------------------------------------+
 | Functions to run when window loads |
 +------------------------------------*/
addLoadEvent(function () {
	if (document.getElementById && document.getElementsByTagName) {
		setMiniTabs();
	}
});

// create a global array of the content divs
var ARR_CONTENT_DIVS; // Contains the div id and the number child divs beneatch it
	
// Set the event handlers and array
function setMiniTabs() {
	var tabmenu_ul = document.getElementById("tabmenu");
	
	// Get the number of <li> tags to create an array
	var liCnt = 0;
	for (var i = 0; i < tabmenu_ul.childNodes.length; i++)
		if (tabmenu_ul.childNodes[i].nodeName.toLowerCase() == "li")
			liCnt++;
	
	// Create array
	ARR_CONTENT_DIVS = new Array(liCnt);
	
	// Make the new array a 2-d array
	for (var i = 0; i < ARR_CONTENT_DIVS.length; i++)
		ARR_CONTENT_DIVS[i] = new Array(2);
	
	// Find the list items that represent the tabs
	var idx = 0; // The current index of ARR_CONTENT_DIVS
	for (var i = 0; i < tabmenu_ul.childNodes.length; i++) {
		if (tabmenu_ul.childNodes[i].nodeName.toLowerCase() == "li") {
			var li_node = tabmenu_ul.childNodes[i];
			idx++;
			
			// Find the anchor tags and get their ids
			for(var j = 0; j < li_node.childNodes.length; j++)
				if (li_node.childNodes[j].nodeName.toLowerCase() == "a") {
					var id = li_node.childNodes[j].id.replace("Tab", ""); // Remove the tab part of this ID
					
					ARR_CONTENT_DIVS[idx-1][0] = id; // Set the id in the array
					ARR_CONTENT_DIVS[idx-1][1] = 0; // Set the default count
				}
		}
	}
	
	// Run through through all the divs and find the ones with a prefix of a content div in ARR_CONTENT_DIVS
	var allDivs = document.getElementsByTagName("div");
	for (var i = 0; i < allDivs.length; i++)
		for (var j = 0; j < ARR_CONTENT_DIVS.length; j++)
			// Count how many child divs the main div has
			if (allDivs[i].id.indexOf(ARR_CONTENT_DIVS[j][0] + "_") > -1) {
				ARR_CONTENT_DIVS[j][1]++;
				break;
			}
			
	return;
}

/* This function is for swapping the mini tabs */
function swapTab(tab) {
	// Did someone click on a tab before it was done loading?
	if (ARR_CONTENT_DIVS == null) {
		setTimeout("swapTab('" + tab + "')", 1000); // Execute swapTab() every 1 second until ARR_CONTENT_DIVS is set 
		return false;
	}
	
	// get a handle on the content bin
	var bin = document.getElementById("bin");
	
	// get a handle on the tab that is to be activated
	var	activeTabId = tab + "Tab";
	var	activeContentId = tab;
	
	// set up some vars
	var	thisTabId, thisTab, thisContentId, thisContent;
	
	// loop thru the bin
	for (var i = 0; i < ARR_CONTENT_DIVS.length; i++) {
		// get a handle on this contentId
		thisContentId = ARR_CONTENT_DIVS[i][0];
		thisContent = document.getElementById(thisContentId);
		
		// get a handle on this tab id
		thisTabId = ARR_CONTENT_DIVS[i][0] + "Tab";
		thisTab = document.getElementById(thisTabId);
		
		// if this contentId matches the active content id, show it and change the class of the tab
		// otherwise, hide the content and clear the class on the tab
		if(activeContentId == thisContentId) {
			thisTab.className = "active"; // change style for the tab
			thisContent.style.display = "block"; // show the appropriate content div
		}
		else {
			thisTab.className = ""; // change style for the tab
			thisContent.style.display = "none"; // hide the appropriate content div
		}
	}
	
	return false;
}

var MIN_POS = 0;
var CURR_POS = MIN_POS;
var MAX_POS = 0; // Each tab contains four different content displays (video, audio, image lib, resource)
var CURR_TAB_ID = ""; // Tracks the current tab we're viewing
function swapTabContent(strTabID, intPos) {
	// Did someone click on a link before tabs were done loading?
	if (ARR_CONTENT_DIVS == null) {
		setTimeout("swapTabContent('" + strTabID + "', " + intPos + ")", 1000); // Execute swapTabContent() every 1 second until ARR_CONTENT_DIVS is set 
		return false;
	}
	
	// Are we on a new tab > if so, reset global vars
	if (CURR_TAB_ID != strTabID) {
		CURR_TAB_ID = strTabID;
		CURR_POS = 0;
		
		// Find the tabID in ARR_CONTENT_DIVS to get MAX_POS
		for (var i = 0; i < ARR_CONTENT_DIVS.length; i++)
			if (ARR_CONTENT_DIVS[i][0] == strTabID) {
				MAX_POS = ARR_CONTENT_DIVS[i][1] - 1;
				break;
			}
	}
	
	// Hide the old content div
	document.getElementById(strTabID + "_" + CURR_POS).style.display = "none";
	
	if (CURR_POS + intPos < MIN_POS)
		CURR_POS = MAX_POS;
	else if (CURR_POS + intPos > MAX_POS)
		CURR_POS = MIN_POS;
	else
		CURR_POS = CURR_POS + intPos;
	
	// Display new content div
	document.getElementById(strTabID + "_" + CURR_POS).style.display = "block";
	
	return false;
}