/* A simple tab menu which shows and hides div elements, the usage is:
1.A unordered list with an id: The li item id links to the div which has 
  the same id prepending to "_content":
		<ul id="locationNav" class="js-ultabnav">
			<li class="active" id="loc_favorites">Link Text</li>
		</ul>
			
  2. divs with content to be shown/hidden:	
  <div class="locationDiv" id="loc_favorites_content" style="DISPLAY: none"></div>
  
  3. a hidden field with the ul id prepending to _selectedtab:
   <INPUT id=locationNav_selectedtab type=hidden> 		
*/
// global tab variables
var contentDiv = "_content";
var activeClass = "active";	// style class for the active tab
// Initialization
function initLocTabs(ulName) {
	// Hide error message
	/*
	var errorDiv = eval(getDocObj("LocationSelection_errorsDiv"));
	if (errorDiv != null && errorDiv.style.display != "block") {
		showLayer(errorDiv, false);	
	}
	*/
	// Add the javascript to each tab to call the changeTab() function.
	var currentTabLink = addJSEvent(ulName);
	// run the changeTab() function to hide the hidden layers
	if (currentTabLink != null) {
		changeTab(currentTabLink);
		//alert('changing to the current tab '+currentTabLink.parentNode.id);
	}
}
function changeTab(tabLink) {
	// get the parent &lt;li&gt;
	var showID = tabLink.parentNode.id;
	// loop through and hide all tabs
	// get the &lt;ul&gt; list
	var ul=tabLink.parentNode.parentNode;
	var hd_sel=document.getElementById(ul.id+"_selectedtab");
	if (hd_sel == null)
		hd_sel=document.getElementsByName(ul.id+"_selectedtab")[0];
	if (hd_sel != null) hd_sel.value = showID;
	if (ul.hasChildNodes()) {
		// get array of &lt;li&gt;'s.
		var lis = ul.getElementsByTagName('li');
		for(var i=0;i<lis.length;i++) {
			tabDiv = document.getElementById(lis[i].id + contentDiv);
			if (lis[i].id == showID) {
				cssjs('add', lis[i], activeClass);		// set the tab as "hidden"
				showLayer(tabDiv, true);				// show the associated DIV
			} else {
				cssjs('remove', lis[i], activeClass);		// set the tab as "current"
				showLayer(tabDiv, false);				// hide the associated DIV
			}
		}
	}
}

function showLayer(obj, showLayer) {
	if (showLayer) {
		obj.style.display = "block";
		obj.style.visibility = "visible";
	} else {
		// This funny bit of code is to fix a Firefox bug... (yes, really)
		// see https://bugzilla.mozilla.org/show_bug.cgi?id=97506
		obj.style.display = "none";
		obj.style.display = "";
		obj.style.display = "none";
	}
}

function addJSEvent(ulName) {
	// get the &lt;ul&gt; list
	var ul=document.getElementById(ulName);
	var hd_sel=document.getElementById(ulName+"_selectedtab");
	// 16.11.06 OI in order to support the HiddenJavaScriptField control
	if (hd_sel == null)
		hd_sel=document.getElementsByName(ulName+"_selectedtab")[0];
	var currentTab = null;
	var lastTab = null;
	if (hd_sel == null || hd_sel.value == "undefined") hd_sel = null;
	//if (hd_sel != null) 
	if (ul.hasChildNodes()) {
		// get array of &lt;li&gt;'s.
		var lis = ul.getElementsByTagName('li');
		var ahref;
		var tabDiv;
		for(var i=0;i<lis.length;i++) {
			tabDiv = document.getElementById(lis[i].id + contentDiv);
			ahref = lis[i].getElementsByTagName('a').item(0);
			if (ahref) {
				ahref.onclick = function() {changeLocator(this);};
				ahref.onKeyPress = function() {changeLocator(this);};
				// if this is the current tab, save it so we can return it from this function
				if (hd_sel != null && hd_sel.value != "") {
				  if (lis[i].id == hd_sel.value) currentTab = ahref;
				} else {
				  if (currentTab == null && cssjs('check',lis[i], activeClass)) {
					currentTab = ahref;
				  }
				}
				lastTab = ahref;
			}
		}
	}
	if (currentTab == null) return lastTab;
	return currentTab;
}
function changeLocator(tabLink) {
	var tabLinkID = tabLink.parentNode.id;
	// Hide error message
	//var errorDiv = eval(getDocObj("LocationSelection_errorsDiv"));
	//if (errorDiv != null) showLayer(errorDiv, false);
	changeTab(tabLink);
}
/* Code below taken from Unobtrusive Javascript
http://www.onlinetools.org/articles/unobtrusivejavascript/index.html */
function cssjs(a,o,c1,c2) {
	switch (a){
		case 'swap':
			o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
		case 'add':
			if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(o.className);
		break;
	}
}

/* ############## Helper Functions */
function getDocObj(elem,parent) {
	if (document.layers) {
	    if (parent) return "document."+parent+".document."+elem;
	    else 		return "document."+elem;
	} 
	else if (document.all) return "document.all."+elem;
	else if (document.getElementById) return "document.getElementById('"+elem+"')";
}
// nonobstrusive adding of tabbing function. Specify css class js-ultabnav to the unordered list, 
// (added by innobit-ML)
function initULTabNavigation() {
  if (!document.getElementsByTagName) return false;
  var elements = document.getElementsByTagName('ul');
  for (var i=0;i<elements.length;i++) {
    if (elements[i].className.match(/\bjs-ultabnav\b/)) {
	  var ultab = elements[i];
	  initLocTabs(ultab.id);
    }
  }
}

addOnloadEvent(initULTabNavigation);