function catmenu_load() {
	catmenu_Setup();
	if (!catmenu_GlobalStateFromCookie()) catmenu_GlobalState("open");
}

if (navigator.userAgent.indexOf("MSIE")!=-1) {
	window.attachEvent("onload",catmenu_load);
} else {
	window.addEventListener("load",catmenu_load,true);
}

var topIMG;
var parentIMG=new Array();
var action="per";
var areclosed=",";
var countclosed=0;

function catmenu_Setup() {
	var obj,objs,objs2;

	obj=document.getElementById("hashlegend"); if (!obj) return;
	obj.className="hasJs";
	var div=document.getElementById("catmenu"); if (!div) return;
	objs=div.getElementsByTagName("H1"); obj=objs[0];
	obj.className="hasJs";
	topIMG=document.createElement("IMG");
	topIMG.className="catmenu_open";
	topIMG.setAttribute("src","/images/less.gif");
	topIMG.alt=topIMG.title="Collapse All";
	topIMG.onclick=function() { catmenu_ToggleGlobalState(); }
	obj.insertBefore(topIMG,obj.firstChild);
	
	objs=div.getElementsByTagName("H2");
	for (var i=0;i<objs.length;i++) {
		obj=document.createElement("IMG");
		obj.id="img_"+objs[i].id.substring(4);
		obj.className="catmenu_open";
		obj.src="/images/less.gif";
		obj.alt=obj.title="Collapse";
		obj.onclick=function() { catmenu_ToggleState(this,this.parentNode); }
		parentIMG.push(obj);
		objs[i].insertBefore(obj,objs[i].firstChild);
		objs[i].style.marginLeft = "13px";
		objs[i].style.textIndent = "-13px";
		
		obj=document.getElementById("ul_"+objs[i].id.substring(4));
		obj.className="hasjs";
	}
}

function catmenu_GlobalState(newstate) {
	if (!topIMG) return;

	action="all";
	for (var i=0;i<parentIMG.length;i++) {
		catmenu_State(newstate,parentIMG[i],parentIMG[i].parentNode);
	}
	if (newstate=="open") {
		topIMG.className="catmenu_open";
		topIMG.src="/images/less.gif";
		topIMG.alt=topIMG.title="Collapse All";
	} else if (newstate=="closed") {
		topIMG.className="catmenu_closed";
		topIMG.src="/images/more.gif";
		topIMG.alt=topIMG.title="Expand All";
	}
	action="per";
}

function catmenu_GlobalStateFromCookie(newstate) {
	var regex=/(^|;)\s*catmenu_closed=,([\d,]+),/;
	var matches=regex.exec(document.cookie); if (!matches) return false;
	var cats=matches[2].split(",");
	
	var obj;
	for (var i=0;i<cats.length;i++) {
		obj=document.getElementById("img_"+cats[i]);
		obj.onclick();
	}
	return true;
}		

function catmenu_ToggleGlobalState() {
	var newstate=(topIMG.className=="catmenu_open" ? "closed" : "open");
	catmenu_GlobalState(newstate);
}

function catmenu_State(newstate,img,h1) {
	var obj=document.getElementById("ul_"+h1.id.substring(4));
	if (newstate=="open" && img.className=="catmenu_closed") {
		img.className="catmenu_open";
		img.src="/images/less.gif";
		img.alt=img.title="Collapse";
		obj.style.display="block";
		var ac=areclosed.split(","+h1.id.substring(4)+","); areclosed=ac[0]+","+ac[1];
		countclosed--;
	} else if (newstate=="closed" && img.className=="catmenu_open") {
		img.className="catmenu_closed";
		img.src="/images/more.gif";
		img.alt=img.title="Expand";
		obj.style.display="none";
		areclosed+=h1.id.substring(4)+",";
		countclosed++;
	}
	catmenu_setclosedcookie();
	
	if (action=="per" && countclosed>0) {
		topIMG.className="catmenu_closed";
		topIMG.src="/images/more.gif";
		topIMG.alt=topIMG.title="Expand All";
	} else if (action=="per" && countclosed==0) {
		topIMG.className="catmenu_open";
		topIMG.src="/images/less.gif";
		topIMG.alt=topIMG.title="Collapse All";
	}
}

function catmenu_ToggleState(img,h1) {
	var newstate=(img.className=="catmenu_open" ? "closed" : "open");
	catmenu_State(newstate,img,h1);
}

function catmenu_setclosedcookie() {
	var d=new Date(); d.setTime(d.getTime()+365*86400*1000);
	document.cookie="catmenu_closed="+areclosed+"; expires="+d.toGMTString()+"; path=/";
}
