var enablePersist = true; //Enable saving state of content structure using session cookies? (on/off)
var collapsePrevious = true; //Collapse previously open content when opening present? (yes/no)
var expandOnly = false; //Enables only expanding of items, contraction is disabled.

function setEnablePersistance(enable) {
	if (enable == true) {
		enablePersist = true;
	}else if (enable == false){
		enablePersist = false;
	}
}

function setCollapsePrevious(collapse) {
	if (collapse == true) {
		collapsePrevious = true;
	}else if (collapse == false){
		collapsePrevious = false;
	}
}

function setExpandOnly(enable) {
	if (enable == true) {
		expandOnly = true;
	}else if (enable == false){
		expandOnly = false;
	}
}

/*
 *	Build a colleaction of elements with the specified style sheet classname
 */
function getElementbyClass(classname){
	ccollect = new Array()
	var inc = 0;
	var alltags = document.all ? document.all : document.getElementsByTagName("*");
	for (i = 0; i < alltags.length; i++) {
		if (alltags[i].className == classname){
			ccollect[inc++] = alltags[i];
		}
	}
}

/*
 *	Contract all content excluding any items to omit
 */
function contractAllContent(omit){
	var inc = 0;
	
	while (ccollect[inc]){
		if (ccollect[inc].id != omit) {
			ccollect[inc].style.display = "none";
		}
		inc++;
	}
}

/*
 *	Contract content for the id supplies
 */
function contractContent(cid){
	var inc = 0;
	
	while (ccollect[inc]){
		if (ccollect[inc].id == cid) {
			ccollect[inc].style.display = "none";
		}
		inc++;
	}
}

/*
 *	Expand the content for the item with the id supplied
 */
function expandContent(cid){
	if (typeof ccollect != "undefined"){
		if (collapsePrevious) {
			contractAllContent(cid);
		}
		if (expandOnly) {
			document.getElementById(cid).style.display = "block";
		}else{
			document.getElementById(cid).style.display =
				(document.getElementById(cid).style.display!="block") ? "block" : "none";		
		}
	}
}

/*
 *	Show the content for the components - called at the body onLoad event
 */
function reviveContent(){
	contractAllContent("omitnothing");
	selectedItem = getselectedItem();
	selectedComponents = selectedItem.split("|");
	for (i = 0; i < selectedComponents.length - 1; i++){
		document.getElementById(selectedComponents[i]).style.display="block";
	}
}

/*
 *	Get the cookie from the current location
 */
function get_cookie(Name) { 
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if (offset != -1) { 
			offset += search.length
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
				returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

/*
 *	Get the selected item from the cookie
 */
function getselectedItem(){
	if (get_cookie(window.location.pathname) != ""){
		selectedItem = get_cookie(window.location.pathname);
		return selectedItem;
	}else{
		return "";
	}
}

/*
 *	Save the switch state (show/hide) for each element
 */
function saveswitchstate(){
	var inc=0, selectedItem = "";
	while (ccollect[inc]) {
		if (ccollect[inc].style.display=="block") {
			selectedItem += ccollect[inc].id+"|";
		}
		inc++;
	}
	document.cookie=window.location.pathname + "=" + selectedItem;
}

/*
 *	The code to run onLoad - show the displayable content
 */
function do_onload(){
	getElementbyClass("switchcontent");
	if (enablePersist && typeof ccollect !="undefined") {
		reviveContent();
	}
}

/* Create the onLoad events */
if (window.addEventListener) {
	window.addEventListener("load", do_onload, false);
}else if (window.attachEvent) {
	window.attachEvent("onload", do_onload);
}else if (document.getElementById) {
	window.onload=do_onload;
}
if (enablePersist && document.getElementById){
	window.onunload=saveswitchstate;
}