// JavaScript Document
/** This version uses a styled <ul> which appears as "tabs" above the various layers.
**/

//Object to sore the array of views
var ulTabbedViewIDs = new Object();

// Method to hide/show layers
function showView(activeLayer) {
	//alert(activeLayer);
	// variable to store the id of <ul> element
	var ulTabID = activeLayer.parentNode.parentNode.id; 
	// variable to store all of the list items within the <ul> tag
	var ulTabList = document.getElementById(ulTabID).getElementsByTagName("li"); 
	for (var i=0; i < ulTabList.length; i++) {
			// set class to null to deactivate all of the tabs
			ulTabList[i].className = "";
			
		// if tab content within this array index exists, hide all of the related tab layersS
		if (typeof ulTabbedViewIDs[ulTabID][i]!= "undefined") {
		document.getElementById(ulTabbedViewIDs[ulTabID][i]).style.display = "none"; 
		}
	}
	// highlight selected tab
	activeLayer.parentNode.className = "active";
	// display the active layer or "view"
	document.getElementById(activeLayer.getAttribute("rel")).style.display = "block";
	// save the active layer ID for session cookie reference
	saveActiveViewID(ulTabID, activeLayer.getAttribute("rel"));
}

// Method to store all of the ids of the related view layers in an array
function storeViewIDs(ulTabID, relattribute) {
	 // check to make sure the array doesn't already exist
	if (typeof ulTabbedViewIDs[ulTabID] == "undefined")
		ulTabbedViewIDs[ulTabID] = new Array();
		ulTabbedViewIDs[ulTabID][ulTabbedViewIDs[ulTabID].length] = relattribute;
}

// Method to return a tab link based on the ID of the associated tab content
function getTabLinksByID(ulTabID, viewID) {
	var ulTabList=document.getElementById(ulTabID).getElementsByTagName("li");
	for (var i=0; i < ulTabList.length; i++) {
		if (ulTabList[i].getElementsByTagName("a")[0].getAttribute("rel") == viewID) {
			return ulTabList[i].getElementsByTagName("a")[0];
			break;
		}
	}
}

// Method to save active ulTab and related layer or "view" in a cookie
function saveActiveViewID(ulTabID, selectedtabid) {
	if (sessionControl == 1)
	setCookie(ulTabID, selectedtabid);
}

/** INIT Method - gets called within script tag in the body of the HTML.
*** The INIT MUST be placed BELOW all of the UL tabs and associated layers (or "views")
*** for it to function properly in IE - It accepts ID value args to handle several tabbed 
*** views on one page (each <ul> tag on the page must have a unique ID).
*** Syntax is :  initULTabViews("tabbedViews1","tabbedViews2"); but ID values only
*** need to be unique names such as ("NeatStuff","MoreNeatStuff")
**/
function initULTabViews() {
	 // first loop through passed UL ids
	for (var i=0; i < arguments.length; i++) {
		// clean up cookie if it is not activated (=0)
		if (sessionControl == 0 && getCookie(arguments[i])!="") {
			setCookie(arguments[i], "")
		}
		// if there is an active cookie, retrieve ID of selected tab from it
		var selectedTab = getCookie(arguments[i]);
		// locate the "tabbed" <ul>'s 
		var uls = document.getElementById(arguments[i]);
		 // array of the <li>'s within
		var lis = uls.getElementsByTagName("li");
		
		// loop through each element to determine if and which view is shown on page load
		for (var j=0; j < lis.length; j++) { 
			var liLinks = lis[j].getElementsByTagName("a")[0];
			if (liLinks.getAttribute("rel")) {
				// store id of each tab content via method
				storeViewIDs(arguments[i], liLinks.getAttribute("rel")); 
				liLinks.onclick = function() {
					showView(this);
					return false;
				}
				// check if the initial HTML code is set up to have a view shown by default. . .
				if (lis[j].className == "active" && selectedTab == "") {
					// show the view if there is
					showView(liLinks);
				}
			}
		} //end inner for loop
		
		// Now over-ride the static HTML code if the session cookie is active
		if (selectedTab != "") {
			
			var cookieLink = getTabLinksByID(arguments[i], selectedTab);
			
			if (typeof cookieLink != "undefined") {
				showView(cookieLink);
			} else {
			 // if no particular view has been set either by the cookie or the static HTML, 
			 // load the first tab by default
			showView(lis[0].getElementsByTagName("a")[0])
			}
		}
	} //end outer for loop (whew!)
}

/** Use code below if you want to set a session cookie that enables the browser to
*** remember which layer is active if/when user leaves and then returns to the page.
*** A value of 1 to the boolean sessionControl var sets the cookie. A value of 0 disables it.
**/
var sessionControl = 1;

function getCookie(cName) {
	//regular expression to search for target name/value pair
	var re = new RegExp(cName+"=[^;]+", "i"); 
	//search for cookie & return its value if found
	if (document.cookie.match(re)) 
		return document.cookie.match(re)[0].split("=")[1];
		return "";
}

function setCookie(name, value) {
	 //cookie value is domain wide (path=/)
	document.cookie = name + "=" + value;
}
