// touchles javascript™ -- no event handlers in html

var newLink = document.createElement("link");
newLink.setAttribute("rel","stylesheet"); newLink.setAttribute("type","text/css"); newLink.setAttribute("href","http://www.sfsu.edu/template/includes/jssafe.css");
document.getElementsByTagName("head")[0].appendChild(newLink);
// if there is js, page appends a second css file (above) to close menu onload
// this gets rid of the onload css-open/js-close flash and leaves the menu accessible (open) if no js

var menuObj;
var divMenu;
var h3Menu;
var aMenu;
var isOpen = new Array(false,false,false,false);
var allOpen = false;
var closeDir = "Following are the four subcategories for links that will be revealed if you show";
var openDir = "Each of the following four shortcut links jumps to a subcategorized list of";


function goMenu() {
	// set up link menu:
	menuObj = document.getElementById("menubox");
	divMenu = menuObj.getElementsByTagName("div");
	h3Menu = menuObj.getElementsByTagName("h3");
	aMenu = menuObj.getElementsByTagName("a");
	// workaround for IE's back button functionality, which reloads page instead of returning to a cached, user-generated DOM state:
	if (navigator.appName == "Microsoft Internet Explorer") {
		rememberFocus();
		recallFocus();
	}
	for (var i=0; i<aMenu.length; i++) {
		// set behavior for clicked link menu category:
		if (aMenu[i].href.indexOf("#cat") != -1 && aMenu[i].className.indexOf("jumponly") == -1) {
			aMenu[i].onclick = showOne;
		}
	}
	// set behavior for show all and hide all links:
	document.getElementById("showall").onclick = showAll;
	document.getElementById("hideall").onclick = hideAll;
	document.getElementById("keyshowall").onclick = showAll;
	document.getElementById("keyhideall").onclick = hideAll;
}

function showOne() {
	// show or hide uls for clicked row:
	var whichCat = this.href.split("#cat")[1];
	// show the row's uls if they are not already showing:
	if (!isOpen[whichCat]) {
		for (var i=0; i<divMenu.length; i++) {
			if (divMenu[i].className.indexOf("cat" + whichCat) != -1) {
					divMenu[i].style.display = "block";
			}
		}
		showIt("block","close","show","hide",closeDir,openDir,true,whichCat);
	}
	// or hide row's uls if they are showing:
	else {
		if (this.className.indexOf("openonly") == -1) {
			for (var i=0; i<divMenu.length; i++) {
				if (divMenu[i].className.indexOf("cat" + whichCat) != -1) {
					divMenu[i].style.display = "none";
				}
			}
		showIt("none","open","hide","show",openDir,closeDir,false,whichCat);
		}
	}
	// keep track of whether all, none, or some are open:
	var allClosed = 0;
	for (var i=0; i<isOpen.length; i++) {
		!isOpen[i] ? allClosed += 1 : allClosed -= 1;
	}
	if (allClosed == isOpen.length) { showStat("none","inline") }
	else if (allClosed == "-" + isOpen.length) { showStat("inline","none") }
	else { showStat("inline","inline") }
	// in case a scrollbar has been introduced:
	findPos();
}

function showStat(hidestat,showstat) {
	document.getElementById("hideall").style.display = hidestat;
	document.getElementById("showall").style.display = showstat;
	document.getElementById("keyhideall").style.display = hidestat;
	document.getElementById("keyshowall").style.display = showstat;
}

function showIt(liststat,imagestat,titlestat,titlechange,dirstat,dirchange,boolstat,catnum) {
	var title = menuObj.getElementsByTagName("h3")[catnum].firstChild.getAttribute("title");
	h3Menu[catnum].style.backgroundImage = "url(/template/images/" + imagestat + ".png)";
	h3Menu[catnum].style.backgroundRepeat = "no-repeat";
	h3Menu[catnum].firstChild.setAttribute("title",title.replace(titlestat,titlechange));	
	h3Menu[catnum].firstChild.firstChild.nextSibling.firstChild.nodeValue = h3Menu[catnum].firstChild.firstChild.nextSibling.firstChild.nodeValue.replace(titlestat,titlechange);
	h3Menu[catnum].nextSibling.firstChild.nodeValue = h3Menu[catnum].nextSibling.firstChild.nodeValue.replace(dirstat,dirchange);
	isOpen[catnum] = boolstat;
}

function showAll() {
	showHide("block","close","none","inline","show","hide",closeDir,openDir,true);
}

function hideAll() {
	showHide("none","open","inline","none","hide","show",openDir,closeDir,false);
}

function showHide(liststat,imagestat,showstat,hidestat,titlestat,titlechange,dirstat,dirchange,boolstat) {
	// execute menu states for show all and hide all links:
	for (var i=0; i<divMenu.length; i++) {
		if (divMenu[i].className.indexOf("cat") != -1) {
			divMenu[i].style.display = liststat;
		}
	}
	for (var j=0; j<h3Menu.length; j++) {	
		var catnum = j;
		showIt(liststat,imagestat,titlestat,titlechange,dirstat,dirchange,boolstat,catnum)
	}
	showStat(hidestat,showstat)
	// in case a scrollbar has been introduced:
	findPos();
}



	


function rememberFocus() {
	for (var i=0; i<aMenu.length; i++) {
		if (aMenu[i].href.indexOf("#cat") == -1 || aMenu[i].href.indexOf("_cat") == -1) {
			aMenu[i].onclick = function() {document.cookie = this;}
		}
	}
}

function recallFocus() {
	if (window.location.hash == "#menustart") {
		showAll()
		focusThis()
	}
	else if (window.location.hash.indexOf("cat") != -1) {
		var whichCat = window.location.hash.split("cat")[1];
		// show the row's uls:
		if (!isOpen[whichCat]) {
			for (var i=0; i<divMenu.length; i++) {
				if (divMenu[i].className.indexOf("cat" + whichCat) != -1) {
						divMenu[i].style.display = "block";
				}
			}
			showIt("block","close","show","hide",closeDir,openDir,true,whichCat);
		}
		document.getElementById("hideall").style.display = "inline";
		focusThis()
		findPos();
	}
}

function focusThis() {
	for (var i=0; i<aMenu.length; i++) {
		if (aMenu[i] == document.cookie && aMenu[i].href){
			try { aMenu[i].focus() }
			catch (iesucks) { }
		}
	}
}
