function $(){var r=[],a=arguments;for(var i=0,j=a.length;i<j;i++){(typeof a[i]=='string')?(r.push(document.getElementById(a[i]))):(r.push(a[i]))}
return(r.length==1)?r[0]:r};

function getElementsByClass(s,n,t) {  // s=searchclass n=node t=tag
	var c=[], e=(n?n:document).getElementsByTagName(t?t:'*'),r=new RegExp("(^|\\s)"+s+"(\\s|$)");
	for (var i=0,j=e.length;i<j;i++) r.test(e[i].className)?c.push(e[i]):''; return c };


function popit(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
};


function msg(s,c) {	c?$('msgbox').innerHTML+= " -- " + s:$('msgbox').innerHTML=s;};

/*
HOW IT WORKS:

start with hideSheets(initFolderLinks());
-- this runs initfolderlinks, which swaps the actions of the tab links from scrolling links to javascript calls.  
initFolderLinks returns the default div for each folder.

hideSheets() hides all sheets except those exempted in its first argument -- which initially is the result of initFolderLinks.
Additionally, it only acts within the node/folder supplied in its second argument -- initially the whole document.
When switching sheets later, the javascript functions assigned to the tabs by initFolderLinks take care of the proper exemptions and node by supplying the selected tab and folder.

hideSheets() also runs clearOtherTabs() to reset the selected tab.  clearOtherTabs also takes exemption and scope arguments.
It removes or applies the 'ontab' class, using the clearTab() and hilite() utility functions, based on the supplied exemption and scope.

getTabs() is a utility function used by a few of these other functions.


showSheet() takes a target tab and its parent folder (redundant, I know) and makes it visible.  
Then it calls hideSheets with the target as the exemption, and the parent folder as the scope.  
As above, running hideSheets takes care of setting the selected tab.

showFolder() just takes a folder, finds all the sheets inside, and makes them visible.
Then it gets all the contained tabs, runs clearTab on them, and manually hilite()s the tab with class 'showall'.


*/







// FIND ALL FOLDERS, SET LINKS ON ANCHORS IN TABS
function initFolderLinks() {
	// init defdiv
	defdiv = new Array();
	
	viewing= document.location.hash.substr(1);
	//msg("viewing: " + viewing, 1);

	if (!document.getElementsByTagName) return null;
	var folders = getElementsByClass("folder");
	for (var j=0; j < folders.length ; j++ ) {  // for each folder
		def=false;
		//var anchors = folders[j].getElementsByTagName("a");
		var anchors = folders[j].getElementsByTagName("ul")[0].getElementsByTagName("a");
		for(var i=0; i < anchors.length; i++) { // for each anchor in folder
			var a = anchors[i];
			var href = a.href;
			var id = a.id;
								
			if ((href.indexOf("#") != -1) && (href.indexOf("nav") != -1)) { // jump ref
				var index = href.indexOf("#") + 1;
				var t = href.substr(index);  // t is the sheet name
				
				// look for default tab specified by style
				if (a.className.indexOf("defTab")>=0 || t==viewing) {
					defdiv.push(t);
					def = true;
				} 
				
				
				// now set the href and title, preserve original string in rel
				href = "javascript:showSheet('" + t + "','" + folders[j].id + "');";
				a.setAttribute("href",href);  // set the javascript for the link
				a.setAttribute("rel",t); // store the sheet name in the rel attribute for later
				a.setAttribute("title","View the " + t.substr(3) + " sheet in this group"); // helpful title
			} //endif jumpref
		} // end anchors in folder
		if (!def) { // if no defaults were specified for this folder, use the first tab
			defdiv.push(anchors[0].rel);
		}
	
	
	}// end folderloop
	
	
// collapse defdivs
defdiv = defdiv.toString();
//msg("defdiv: " + defdiv,1);
return defdiv;
};


// SHOWS A SHEET, HIDES OTHERS IN ITS PARENT FOLDER
function showSheet(sheetID,folderID) { 
	if (!document.getElementById) return null;
	d = $(sheetID);
	if (d.style.display == "none") {
		d.style.display = "block";
	}
	hideSheets(sheetID,folderID); // hide with our target exempt, and its parent folder as the node
};


function hideSheets(exemptID,folderID) {
	if (!document.getElementsByTagName) return null;
	node=$(folderID);
	if (!exemptID) exemptID = "";
	var divs = getElementsByClass("sheet",node);
	for(var i=0; i < divs.length; i++) {
		var div = divs[i];
		var id = div.id;
		if ((id.length>1) && (id.indexOf("nav")>=0) && (exemptID.indexOf(id)<0)) {
			div.style.display = "none";
		}
	}
	if (divs.length) clearOtherTabs(exemptID,folderID); // only hide sheets if we found any
};

function clearOtherTabs(exemptID,folderID) {
	tabs = getTabs(folderID);
    for (j=0 ; j<tabs.length; j++ ) {
		tab = tabs[j];
		// clear all tabs in the folder(s), if it's not our tab
		if (tab.href.indexOf(exemptID)<0) clearTab(tab.id);
		// find the tabs in our folder not already hilited, and hilite
		if ((exemptID.indexOf(tab.rel)>=0) && (tab.className.indexOf('ontab')<0)) hilite(tab.id);
    }
    clearTab(getElementsByClass("showall",$(folderID))[0]); // clear showall tab
};

function getTabs(folderID) {
	var tabs = new Array();
	// if no node specified, use whole doc
	folderID ? node=$(folderID) : node=document;
	var anchors = node.getElementsByTagName("a");
	for(var i=0; i < anchors.length; i++) {
		if (anchors[i].id.indexOf("tab") == 0) tabs.push(anchors[i]);
	}
	return tabs;
};

function clearTab(tabID) {
	t=$(tabID);
	if(t)t.className = t.className.replace('ontab','');
};

function hilite(tabID) {
	$(tabID).className += " ontab";
};

 // SHOW ALL SHEETS IN A FOLDER
function showFolder(folderID) {
 	node=$(folderID);
 	// GET THE SHEETS
 	var divs = getElementsByClass("sheet",node);
	for(var i=0; i < divs.length; i++) {
		var div = divs[i];
		var id = div.id;
		// MAKE ALL SHEETS VISIBLE
		if ((id.length>1) && (id.indexOf("nav")>=0)) {
			div.style.display = "block";
		}
	}
	// GET TABS
	var tabs = getTabs(folderID);
	for (j=0 ; j<tabs.length; j++ ) {
		tab = tabs[j];
		// clear all tabs in the folder(s)
		clearTab(tab.id);
    }
    // hilite the show all tab (reqires "showall" class)
	hilite(getElementsByClass("showall",node)[0]);
};




// ---------------------- GENERAL FUNCS


// GENERIC SHOW/HIDE FUNCTION
function showhide(what) {
  if (!document.getElementById) return null;
  showWhat = document.getElementById(what);
  if (showWhat.style.display == "none") {
  	showWhat.style.display = "block";
  } else {
  	showWhat.style.display = "none";
  }
};

// SHOWS ALL DIVS WITH "nav" IN THE ID
function showall() {
	var divs = document.getElementsByTagName("div");
  for(var i=0; i < divs.length; i++)
  {
    var div = divs[i];
    var id = div.id;
    if (id.indexOf("nav")>=0)
    {
      div.style.display = "block";
    }
  }
 };

// LOADER
function addLoadEvent(func) {
   var oldonload = window.onload;
   if (typeof window.onload != 'function') {
      window.onload = func;
   }
   else {
      window.onload = function() {
      oldonload();
      func();
      }
    }
};


// CALL THE INIT FUNCTIONS

tabinit = function() {
	hideSheets(initFolderLinks());
};

addLoadEvent(tabinit);
