
/* ----- NOTES --------------------------------------------------------------------

THIS SCRIPT WILL NOT DEAL WITH NESTED OR MULTIPLE TABSETS
ALTHOUGH IT COULD PROBABLY BE ADJUSTED TO DO SO...

- Script generates a tab-set from a set of DIV.tab-content within a DIV.tab-panel.
- Will remove the first H4 from each DIV.tab-content and use its text as the title
  for the corresponding tab. If no H4 will set title to "Tab n".
- If URL# is in the form of "tab[1-9]" or is equal to any tab's title text without
  spaces, the corresponding tab will be selected when the page is loaded.
- If script fails to run, all tabs are shown with H4s unaltered.

-------------------------------------------------------------------------------- */


/* ----- INITIALISE ------------------------------------------------------------ */
if ((W3CDOM) && (!formatForPrint)) {
	window.addOnload(tsSetUp);
}

/* ----- SETUP ----------------------------------------------------------------- */
function tsSetUp() {
//alert('tsSetup start');
	//if (!W3CDOM) return;			// exit if not dom-compliant browser

	// initialise vars
	tsCurrTabIndex = 0;
	tsArrTabContent = new Array();
	tsArrTabTitles = new Array();
	tsTabHeader = 0;

	// get and set entire tab-panel div as global object ref
	var tsTabsetContainers = getElementsByClassName('tab-panel');
	
	// exit if no tab XHTML present
	if (tsTabsetContainers.length == 0  ) return;
	// exit if no tab-content present - javt - in case there is a tab-panel with no tab-content DIVs
	if(getElementsByClassName('tab-content').length == 0) return;
	
	
	tsTabsetContainer = tsTabsetContainers[0];								// assume only one found
	
	tsTabsetContainer.style.display = 'none';								// hide initially so processing not visible to user
	
	// store tab contents, and tab-header element if present
	
	for (var i=0;i<tsTabsetContainer.childNodes.length;i++) {
		elem = tsTabsetContainer.childNodes[i];
		
		if (elem.className != undefined) {		// have to do this for FF, Safari, etc or script terminates prematurely
		
			if (elem.className.indexOf('tab-header') != -1) {
				tsTabHeader = elem;
			} 
			if (elem.className.indexOf('tab-content') != -1) {
				tsArrTabContent.push(elem);			// store tab contents
			}	
		}
	}

	// add IDs to tabContents (needed so they can be replaced) and hide tabContents initially
	for (var i=0;i<tsArrTabContent.length;i++) {
		tsArrTabContent[i].id = 'tsTabContent' + i;
		tsArrTabContent[i].style.display = 'none';	
	}

	// get and save tab titles
	tsProcessTabTitleH4s();

	// initialise first visible tab
	tsSetInitialActiveTab();

	// dynamically generate tabs UL
	if(tsTabHeader != 0) {
		tsTabsetContainer.insertBefore(tsCreateTabs(),tsTabHeader);
	} else {
		tsTabsetContainer.insertBefore(tsCreateTabs(),tsArrTabContent[0]);	
	}
	
	// show first tab		
	tsArrTabContent[tsCurrTabIndex].style.display = 'block';
	ssCurrTab = tsArrTabContent[tsCurrTabIndex];

	// set active tab
	tsSetActiveTab(tsCurrTabIndex);

	// re-display container
	tsTabsetContainer.style.display = 'block';



}

function tsProcessTabTitleH4s() {
	// store text of first H4 on each tab as tab title, and hide H4s
	// if no H4 use arbitrary text

	for (var i=0; i<tsArrTabContent.length; i++) {
		var arrH4s = tsArrTabContent[i].getElementsByTagName('H4');		// might be more than one, so use only first in following lines
		if (arrH4s.length > 0) {										// if at least one H4 found...
			tsArrTabTitles.push(arrH4s[0].innerHTML);					// store H4 text as tab name
			arrH4s[0].style.display = 'none';							// hide H4
		} else {
			tsArrTabTitles.push('Tab ' + (i+1));						// store arbitrary text as tab name
		}
	}
}

function tsSetInitialActiveTab() {
	// reset tsCurrTabIndex (initialised at 0) if location.hash = tab[1-9]
	// or location.hash = item in tsArrTabTitles (with spaces removed)

	// check for url# = tab[1-9] and not higher than number of tabs
	if (location.hash.substring(1,4).toLowerCase() == 'tab') {
		char4 = location.hash.charAt(4);
		if ((!isNaN(char4)) && ((char4-1) < tsArrTabTitles.length)) {
			tsCurrTabIndex = char4 - 1;
		}
	}

	// check for hash matching tab name in tsArrTabTitles (remove spaces from latter)
	for (var i=0; i<tsArrTabTitles.length; i++) {
		if (location.hash.substring(1).toLowerCase() == tsArrTabTitles[i].replace(' ','').toLowerCase()) {
			tsCurrTabIndex = i;
		}
	}
}

function tsCreateTabs() {
	var tsSelector = document.createElement('UL');			// create UL.tabs
	tsSelector.className = 'tabs';

	for (i=0;i<tsArrTabContent.length;i++) {			// create individual tabs
		var bool = (i == tsCurrTabIndex);
		tsSelector.appendChild(tsCreateTab(i, bool));
	}
	return tsSelector;
}

function tsCreateTab(index, status) {
// status: true (current tab) or false (clickable tab link)

	liTag = document.createElement('LI');		// create LI tag

	aTag = document.createElement('A');			// create A tag
	aTag.id = index;							// IDEALLY THIS NEEDS A MORE UNIQUE IDENTIFIER - miks - 03/2009
	aTag.innerHTML = tsArrTabTitles[index];

	if (status) {								// if current tab...
		liTag.className = 'tab-active';
	}

	liTag.appendChild(aTag);
	return liTag;
}

/* ----- OPERATE -------------------------------------------------------------- */
function tsSetActiveTab(index) {
	// loop through UL.tabs and give corresponding tab class "tab-active"
	// and set onclick attribute for other tabs...

	// get UL.tabs
	tsArrTemp = getElementsByClassName('tabs');
	tsTabs = tsArrTemp[0];								// this assumes only one tabset per page...
	tsArrTabs = tsTabs.getElementsByTagName('LI');

	for (var j=0;j<tsArrTabs.length;j++) {
		if (index == j) {
			tsArrTabs[j].className = 'tab-active';
			tsArrTabs[j].firstChild.onclick = '';
		} else {
			tsArrTabs[j].className = '';
			tsArrTabs[j].firstChild.id = 'tsTab' + j;
			tsArrTabs[j].firstChild.onclick = function() { tsSelectTabContent(this.id); };
		}
	}
}

function tsSelectTabContent(tabID) {
	// replace current tab-content with new tab-content

	// get index from ID - used to have index = ID, but this conflicts with slideshow script which uses very similar approach
	var index = tabID.substring(5);
	
	tsArrTabContent[tsCurrTabIndex].style.display = 'none';	
	tsArrTabContent[index].style.display = 'block';	
	
	tsCurrTabIndex = index;
	tsSetActiveTab(index);
}