/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: toggleTab
Parameters: opentab - current tab we want to view.  if opennum is empty b/c no nested tabs, we'll display num instead.
				numelems - total number of tabs on page
				passthroughtab - parent tab set if nested tabs are present.  will be empty if no nested tabs.
				tabclick - if a tab was clicked, will be 1.
Description: first check for a passthroughtab and open it if set.  then, open the opentab that we want to view.
*/
function toggleTab(opentab,numelems,passthroughtab,tabclick) {
	//check for url variables is a tab was not clicked. reset opentab and passthroughtab vars.
	if(tabclick != 1) {
		if(getURLVar('tab1') != '' && getURLVar('tab2') != '') {
			var opentab = getURLVar('tab2');
			var passthroughtab = getURLVar('tab1');
		} else if(getURLVar('tab1') != '') {
			var opentab = getURLVar('tab1');
			var passthroughtab = '';
		}
	}
	if(tabclick == 1) { //don't call CloseOpenTabs on page load.
		//only close if clicked tab not already open
		Open = CheckActiveIfOpen(opentab);
		if(Open == 0) {
			CloseOpenTabs(opentab, passthroughtab);
			OpenTabs(numelems, opentab, passthroughtab);
		}
	} else { //initialize array on pageload
		$('tabscontent').OpenTabsArray = new Array();
		OpenTabs(numelems, opentab, passthroughtab);
	}
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: OpenTabs
Parameters: numelems - number of tabs
				opentab - current tab we want to view.  if opennum is empty b/c no nested tabs, we'll display num instead.
				passthroughtab - parent tab set if nested tabs are present.  will be empty if no nested tabs.
Description: first check for a passthroughtab and open it if set.  then, open the opentab that we want to view.
*/
function OpenTabs(numelems, opentab, passthroughtab) {
	if(passthroughtab) {
		//check if passthrough tab is already open.
		PassTab = 'tabContent'+passthroughtab;
		TabObject = $(PassTab);//get object
		if(TabObject.className != 'tabContentActive') {
			OpenPassthrough(PassTab);
		}
	}
	ActiveTab = 'tabContent'+opentab;
	OpenActiveTab(ActiveTab);
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: OpenPassthrough
Parameters: PassTab - id of top level tab ex: tabContent3
Description: Open the top level tab first if there are nested tabs on the page.
*/
function OpenPassthrough(PassTab) {
	SetTabHeaderActive(PassTab);
	SetTabContentActive(PassTab);
    Effect.toggle(PassTab,'Appear',{duration:0.0, queue:{scope:'menus', position:'end', limit: 3}});//pass in id
	BuildOpenTabArray(PassTab);
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: OpenActiveTab
Parameters: opentab - id of tab we want to view.
Description: Open the top level tab first if there are nested tabs on the page.
Modifications:
	5/15/2009 - added if($Activetab).... b/c I want to display a tab and its content on pageload.. not js driven, so don't modify this tab if already open.
*/
function OpenActiveTab(ActiveTab) {
	
	//open if it is not already open.
	if($(ActiveTab).style.display == 'none') {
		SetTabHeaderActive(ActiveTab);
		SetTabContentActive(ActiveTab);
	    Effect.toggle(ActiveTab,'Appear',{duration:0.0, queue:{scope:'menus', position:'end', limit: 3}});//pass in id
	}
	
	BuildOpenTabArray(ActiveTab);
}


function BuildOpenTabArray(TabContentID) {
	$('tabscontent').OpenTabsArray.unshift(TabContentID);
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: CloseOpenTabs
Parameters:
Description: get tab ids with classname tabContentActive and close them by setting class name to '' and display to none.
*/
function CloseOpenTabs(opentabNum, passthroughtabNum) {
	//narrow document size for getElementsByClassName function to speed things up.
	ContentContainer = $('tabscontent');
	OpenTabsArrayCopy = Array();
	PassTabID = 'tabContent'+passthroughtabNum;
	ClickedTabID = 'tabContent'+opentabNum;
	//NewTabRequest = Array(ClickedTabID, PassTabID);
	for(j = 0; j < ContentContainer.OpenTabsArray.length; j++){
		CurrentArrayTabID = ContentContainer.OpenTabsArray[j];
		//CheckNum = GetTabNumber(CurrentArrayTabID);
		//Open = CheckActiveIfOpen(CheckNum);
		//if(Open == 0) {
		if(CurrentArrayTabID != PassTabID && CurrentArrayTabID != ClickedTabID) {
			Effect.toggle(CurrentArrayTabID,'Appear',{duration:0.0, queue:{scope:'menus', position:'front', limit: 3}});//pass in id

			SetTabContentInactive(CurrentArrayTabID);
			SetTabHeaderInactive(CurrentArrayTabID);
		} else { //tab remains open, add back into open tab array
			OpenTabsArrayCopy.unshift(CurrentArrayTabID);
		}
	}
	ContentContainer.OpenTabsArray.length = 0; //clear array.
	ContentContainer.OpenTabsArray = OpenTabsArrayCopy.slice();
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: SetTabContentActive
Parameters: Tab - id of tab content to set active.
Description: add tabContentActive class
*/
function SetTabContentActive(ID) {
	TabObject = $(ID);
	TabObject.className = 'tabContentActive';
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: SetTabContentInactive
Parameters:
Description: remove tabHeaderActive class from active header. Get only the ID number from the content id so we can use it for the headerid.
*/
function SetTabContentInactive(ID) {
	ContentTab = $(ID);//get object
	ContentTab.className = 'tabContent';
return;
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: SetTabHeaderActive
Parameters: Tab - id of tab to set active. ID comes in as tabContentID so we need to change the name of the ID into tabHeaderID.
Description: add tabHeaderActive class
*/
function SetTabHeaderActive(ID) {
	IDNumber = GetTabNumber(ID);
	if(IDNumber != 0) {
		CurrentTabHeader = 'tabHeader'+IDNumber;
		HeaderTab = $(CurrentTabHeader);//get object
		HeaderTab.className = 'tabHeaderActive';
	}
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: SetTabHeaderInactive
Parameters:
Description: remove tabHeaderActive class from active header. Get only the ID number from the content id so we can use it for the headerid.
*/
function SetTabHeaderInactive(ID) {
	IDNumber = GetTabNumber(ID);
	if(IDNumber != 0) {
		CurrentTabHeader = 'tabHeader'+IDNumber;
		HeaderTab = $(CurrentTabHeader);//get object
		HeaderTab.className = '';
	}
return;
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/10/2007
Function: CheckActiveIfOpen
Parameters: opentab - tab number that was clicked. ex: 3
Description: Open the top level tab first if there are nested tabs on the page.
*/
function CheckActiveIfOpen(opentab) {
	IsOpen = 0;
	ContentContainer = $('tabscontent');
	CheckTabID = 'tabContent'+opentab;
	for(i = 0; i < ContentContainer.OpenTabsArray.length; i++){
		TabID = ContentContainer.OpenTabsArray[i];
		if(TabID == CheckTabID) {
			IsOpen = 1;
		}
	}
return IsOpen;
}


/*************************************************
Author: Nick Bartlett
Date Created: 04/11/2007
Function: GetTabNumber
Parameters: HeaderID - id value of the content tab being closed.  example: tabContent3
Description: Get the number from the end of the string. first check if last two digits is an integer. if not, check only the last number. return zero if neither.
*/
function GetTabNumber(HeaderID) {
	stoppos = trim(HeaderID).length
	startpos = (stoppos - 2);
	IDNum = HeaderID.substring(startpos,stoppos);

	if(IDNum.match(/^[0-9]+$/)) {
			IDNumAsInt = parseInt(IDNum);
		return IDNumAsInt;
	} else {
		startpos = (stoppos - 1);
		IDNum = HeaderID.substring(startpos,stoppos);
		if(IDNum.match(/^[0-9]+$/)) {
			IDNumAsInt = parseInt(IDNum);
		return IDNumAsInt;
		} else {
			return 0;
		}
	}
}


/**
 * Put a call to this function in the init() function in the top of a page with tabs
 * This function will hide tabs until the menu has drawn
 */
function showTabs() {
	if(! document.getElementById('tabs')) {
		return true;
	}

	document.getElementById('tabs').style.display = 'none';

	if(document.getElementById('tabs2')) {
		document.getElementById('tabs2').style.display = 'none';
	}

	if(document.getElementById('accordion')) {
		document.getElementById('tabs').style.display = 'inline';

		if(document.getElementById('tabs2')) {
			document.getElementById('tabs2').style.display = 'inline';
		}

		return true;
	}

	var t = setTimeout(showTabs, 250);
}
// end - function showTabs()


/*************************************************
Author: Nick Bartlett
Date Created: 04/11/2007
Function: getURLVar
Parameters: urlVarName - name of the url variable
Description: Gets the value of the passed in variable name
*/
function getURLVar(urlVarName) {
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;
}



/*************************************************
Author: Nick Bartlett
Date Created: 04/11/2007
Function: trim
Parameters: str - string to trim
Description: uses regular expression to trim white space from front and end of a string and return the new string.
*/
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}



/*************************************************
Author: Nick Bartlett
Date Created: 08/01/2007
Function: IsNumeric
Parameters: IDNum - variable
Description: uses regular expression to check if the variable is numeric.
*/
function IsNumeric(IDNum) {
	if(IDNum.match(/^[0-9]+$/)) {
		return true;
	} else {
		return false;
	}
}


