
/**
 *  javascript functions for dhtml tabs
 *  global.js
 *  $Revision: 1.6 $
 */

var dhtml_tabs_current_active=""; // global saves which one was active
var otherUrlParams=""; // global to save other info in #
if (contenttype == "undefined") var contenttype="";
if (contentgroup == "undefined") var contentgroup="";
if (typeof(vs_pageview) == "undefined") var vs_pageview = function(){};
var vlc=0; // need to prevent logging of mousedowns

function initTabs2 (){
	// find our tabs on the page
	var divList = document.getElementsByTagName("div");
	
	for (var i=0;divList[i];i++){
		if (divList[i].className == 'pagetabs'){
			initTab(divList[i]);
			break;
		}
	}
}

function initTab (tabWrapper){
	var tabRow = document.createElement('tr');
	var subtabDiv = document.createElement('div');
	subtabDiv.id = "subtabs";

	var firstTab = "";
	var foundActive = false;
	var hadTabs = false; 
	var hadSubTabs = false;
	
	if (! dhtml_tabs_current_active ) {
		var myHash = window.location.hash;	
		var myre;
		if (myHash.indexOf('~')!= -1) {
			myre = /#~[^~^,]+$/;  // tab is the only one
			dhtml_tabs_current_active =  myre.exec(myHash);
			if (!dhtml_tabs_current_active) {
				myre = /#~[^~^,]+,/; // tab is the first one
				dhtml_tabs_current_active= myre.exec(myHash);
				myre = /,~[^~^,]+/; // tab is not the first one
				dhtml_tabs_current_active= myre.exec(dhtml_tabs_current_active);
			} 
			otherUrlParams = myHash.replace(dhtml_tabs_current_active,"");
			myre = /[^#^~^,]+/;
			dhtml_tabs_current_active = myre.exec(dhtml_tabs_current_active);
		} 
	}

	var divList = tabWrapper.getElementsByTagName("div");
	for (var i=0; divList[i];i++){
		// if the div is a main tab do this
		if (divList[i].className == "tab"){
			var hadTabs = true;
			if ( firstTab == "") firstTab=divList[i].id;
			// Put the tab in a table cell - 
			var cell = document.createElement('td');
			var corner = document.createElement('span');
			corner.className = "tabcorner";
			corner.appendChild(document.createElement('span'));
			cell.appendChild(corner);
				
			cell.id = divList[i].id;
			// save id of first one so all related can be marked as active 
			if ( !dhtml_tabs_current_active  || dhtml_tabs_current_active == cell.id ) { 
				dhtml_tabs_current_active = cell.id; // save it for later
				cell.className = "tab-active";
				foundActive = true;
			} else {
				cell.className = "tab";
			}

			// Find the link and do needed manipulations to it
			var myLink = divList[i].getElementsByTagName("a")[0];

			// params for the metrics
			//myLink.rel = "exit"; // we don't use this, we call makeExit directly
			myLink.name = "&amp;lpos=" + cell.id + "_Tab_";
			
      		// put the js call on link for tab switching
			function addTabSwitcher(){
				var tabId = cell.id;
				return function (){
					vs_pageview(tabId,contentgroup,'tab',contenttype);
					showMe(tabId);
					return false;
				}
			}
			myLink.onclick = addTabSwitcher();
			cell.appendChild(myLink);

			// add the new tab to the table row
			tabRow.appendChild(cell);

			// hide the original
			divList[i].style.display = "none";
			divList[i].innerHTML = "";
		} // end if it's a tab
		// if it's the active one when the page loads trigger a pageview call
		if ( cell.id == dhtml_tabs_current_active ) vs_pageview(cell.id,contentgroup,'tab',contenttype);

		// if the div is a subtab do this
		if ( divList[i].className == "subtab" ) {
			hadSubTabs = true;
			// create a div for that particular subtab 
			var mySubtab = document.createElement("div");
			mySubtab.innerHTML = divList[i].innerHTML;
			mySubtab.className = "subtab";
			mySubtab.id = divList[i].id;
			if ( mySubtab.id != "sub-"+dhtml_tabs_current_active ) {
				mySubtab.style.display = "none";	
			}
			subtabDiv.appendChild(mySubtab);

			// hide the original
			divList[i].id ="";
			divList[i].style.display="none";
			divList[i].innerHTML="";
		} // end if it's a subtab
//alert(divList[i].class);
		// if the div is a content div do this
		if (divList[i].className && divList[i].className.indexOf("infotab") != -1) {
			// hide the inactive ones, 
			if ( divList[i].id != "info-"+dhtml_tabs_current_active) {
				divList[i].style.display="none";
			}
/*   ----------------------------------------------------------------------  
	This is probably not going to be real as such, because it raises 
	too many questions about what to do with the subtabs
			// if we do the show all on first tab thing, 
			else if (firstShowsAll) {
				// append everything into first one
			}
-------------------------------------------------------------------------  */
		} 
	} // end for divs in tabList

// might want to only append these things if there is content? or collapse somehow if not?
	if (hadTabs) {
		var tableNode = document.createElement('table');
		tableNode.appendChild(document.createElement('tBody'));
		tableNode.tBodies[0].appendChild(tabRow);

		var tabDiv = document.createElement('div');
		tabDiv.className = "tabset";
		tabDiv.appendChild(tableNode);
	}
	if (hadSubTabs) {
		tabWrapper.insertBefore(subtabDiv,tabWrapper.childNodes[0]);
	}
	tabWrapper.insertBefore(tabDiv,tabWrapper.childNodes[0]);

	// handle case of anchor link not in any tabs so it initializes to first tab still
	if ( ! foundActive  ) {
		dhtml_tabs_current_active = firstTab;
		document.getElementById(firstTab).className="tab-active";
		document.getElementById('info-'+firstTab).style.display="block";
	}

}


function showMe(tabToShow){
 	var newActive = tabToShow; 
	var oldActive = dhtml_tabs_current_active; // our global var
	dhtml_tabs_current_active = newActive; // save the new value

	oldID = "sub-"+oldActive;
	oldOne = document.getElementById(oldID);
	if (oldOne) oldOne.style.display = "none";
	newID = "sub-"+newActive;
	newOne = document.getElementById(newID);
	if (newOne) newOne.style.display = "block";

	oldID = "info-"+oldActive;
	oldOne = document.getElementById(oldID);
	if (oldOne) oldOne.style.display = "none";
	newID = "info-"+newActive;
	newOne = document.getElementById(newID);
	if (newOne) newOne.style.display = "block";

	var oldOne = document.getElementById(oldActive);
	if (oldOne) oldOne.className = "tab";
	var newOne = document.getElementById(newActive);
	if (newOne) newOne.className = "tab-active";

	window.location.hash = '#~'+tabToShow+otherUrlParams;
}
