//This script handles expanding and collapsing categories in the list on the
//"new projects" page.

function toggleCategory(obj) {
	var imgtochange = null;
	var listtotoggle = null;
	
	var siblingiterator = obj;
	while (siblingiterator != null) {
		if (siblingiterator.nodeName.toUpperCase() == 'UL') {
			//Found the list.
			listtotoggle = siblingiterator;
			break;
		}
		
		siblingiterator = siblingiterator.nextSibling;
	}

	//The image is the first child node.
	imgtochange = obj.childNodes[0];
	
	if (listtotoggle == null) {
		//A list was not found.
		return true;
	}

	if (listtotoggle.style.visibility != 'hidden') {
		fadeobj = listtotoggle;
		for (var fadeamount = 1.0; fadeamount >= 0.0; fadeamount -= 0.1)
		{
		     setTimeout('fadeIn('+fadeamount+')',500*(1-fadeamount));
		}

		setTimeout('hideList()', 500);
		
		if (imgtochange != null) {
			imgtochange.src = 'images/bulletclosed' +
			obj.className.substr(obj.className.length-1) + '.gif';
		}
	}
	else {
		listtotoggle.style.visibility = 'visible';
		listtotoggle.style.display = 'block';

		fadeobj = listtotoggle;
		for (var fadeamount = 0.0; fadeamount <= 1.0; fadeamount += 0.1)
		{
			setTimeout('fadeIn('+fadeamount+')',500*fadeamount);
		}

		if (imgtochange != null) {
			imgtochange.src = 'images/bulletopen' +
			obj.className.substr(obj.className.length-1) + '.gif';
		}
	}
	
}

function fadeIn(fadeamount) {
	fadeobj.style.opacity = parseFloat(fadeamount).toFixed(1);
	fadeobj.style.filter = 'alpha(opacity=' + (fadeamount * 100) + ')';
}

function hideList() {
	fadeobj.style.visibility = 'hidden';
	fadeobj.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.

var nestedlists = document.getElementsByTagName("ul");
for (var nlist = 0; nlist < nestedlists.length; nlist++) {
	if (nestedlists[nlist].className != 'ProjectCategoryList') {
		//Different list; skip.
		continue;
	}

	fadeobj = nestedlists[nlist];
	hideList();
}

