//This is something I originally wrote for HireGeeks.

function toggleCategory(obj) {
	if (!obj || !obj.nodeName) {
		obj = this;
	}
		
	var imgtochange = obj.parentNode;
	
	var listtotoggle = obj;
	while (listtotoggle && listtotoggle.nodeName.toUpperCase() != 'DIV') {
		listtotoggle = listtotoggle.nextSibling;
	}
	
	if (listtotoggle == null) {
		//A list was not found.
		return true;
	}

	if (listtotoggle.style.visibility != 'hidden') {
		//Hide.
		fadeobj = listtotoggle;
		for (var fadeamount = 1.0; fadeamount >= 0.0; fadeamount -= 0.1)
		{
		     setTimeout("fadeIn("+fadeamount+", '" + listtotoggle.id + "')",500*(1-fadeamount));
		}

		setTimeout("hideList('" + listtotoggle.id + "')", 500);
		
		if (imgtochange != null) {
			imgtochange.style.backgroundImage = 'url(images/bulletclosed' +
			obj.className.substr(obj.className.length-1) + '.png)';
		}
	}
	else {
		//Show
		listtotoggle.style.opacity = 0;
		listtotoggle.style.filter = 'alpha(opacity=0)';
		listtotoggle.style.visibility = 'visible';
		listtotoggle.style.display = 'block';

		fadeobj = listtotoggle;
		for (var fadeamount = 0.0; fadeamount <= 1.0; fadeamount += 0.1)
		{
		     setTimeout("fadeIn("+fadeamount+", '" + listtotoggle.id + "')",500*fadeamount);
		}

		if (imgtochange != null) {
			imgtochange.style.backgroundImage = 'url(images/bulletopen' +
			obj.className.substr(obj.className.length-1) + '.png)';
		}
	}
	
}

function hideList(objid) {
	obj = document.getElementById(objid);
	if (!obj)
		obj = fadeobj;
		
	obj.style.visibility = 'hidden';
	obj.style.display = 'none';
}

//When we first load the script, hide all categories below the top level.
//This is done so we can show all categories by default, in case JS isn't
//enabled. This "show by default" attitude can be seen elsewhere on the site.

function foldAll(noToggle) {
	var nestedlists = document.getElementsByTagName("div");
	for (var nlist = 0; nlist < nestedlists.length; nlist++) {
		if (nestedlists[nlist].parentNode.parentNode.className != 'AboutList') {
			//Different list; skip.
			continue;
		}

		//Now find links that open and close the category.
		var aboutlinks = nestedlists[nlist].parentNode.getElementsByTagName("a");
		for (var linkidx = 0; linkidx < aboutlinks.length; linkidx++) {
			var linkcontrol = aboutlinks[linkidx];
			if (!linkcontrol.href && linkcontrol.name) {
				linkcontrol.style.cursor = 'pointer';
				linkcontrol.onclick = toggleCategory;
			}
			else if (linkcontrol.href.indexOf('#') >= 0) {
				//This might link to an unfolded category.
				linkcontrol.onclick = unfoldAnchor;
			}
		}

		fadeobj = nestedlists[nlist];
		
		if (noToggle == true) {
			hideList();
		}
		else {
			toggleCategory(fadeobj);
		}
	}
}

//Unfold the category this belongs to.
function unfoldAnchor() {
	var anchorstart = this.href.lastIndexOf('#');
	var anchorname = this.href.substring(anchorstart+1);
	
	var a_targets = document.getElementsByName(anchorname);
	for (var linkidx = 0; linkidx < a_targets.length; linkidx++) {
		if (a_targets[linkidx].nodeName.toUpperCase() == 'A') {
			toggleCategory(a_targets[linkidx]);
			break;
		}
	}
	
	return true;
}

foldAll(true);

var toggleall = document.getElementById('ToggleAll');
if (toggleall) {
	toggleall.style.display = 'block';
	toggleall.style.visibility = 'visible';
	toggleall.onclick = foldAll;
}
