/**
	Cross-browser menu hiding/showing thingemajigger. Kinda reveals how efficient
	different browsers are - runs fastest on Opera.
	
	-tony
*/

var MENU_ATTRIBUTE = 'menu';
var HEIGHT_ATTRIBUTE = 'fullHeight';
var CLOSED_ATTRIBUTE = 'isClosed';
var TOGGLING_ATTRIBUTE = 'isToggling';
var FULL_PERIOD = 90;
var NUM_STEPS = 9;
var menuDivs = new Array();

//must be called after the div#menuDiv has been fully loaded
//@param openItem is the menu name of the div to keep open on load
function whenLoaded(openItem) {
	var allDivs = document.getElementsByTagName('div');

	for(var i = 0; i < allDivs.length; ++i) {
		if(allDivs[i].getAttribute(MENU_ATTRIBUTE)) {
			var currentMenu = allDivs[i];
			menuDivs.push(currentMenu); //add to list of menu divs
			currentMenu.setAttribute(HEIGHT_ATTRIBUTE, currentMenu.offsetHeight); //and store max height
			//unless current menu is the one to keep open, make it folded on load
			if(currentMenu.getAttribute(MENU_ATTRIBUTE) != openItem) {
				currentMenu.setAttribute(CLOSED_ATTRIBUTE, "true");
				currentMenu.style.height = "1px";
				currentMenu.style.display = "none";
			}
		}
	}
}
//recursive function, expands/contracts menu over the
//period of FULL_PERIOD milliseconds
function timedToggle(menuIndex, stepsLeft) {
	var currentMenu = menuDivs[menuIndex];
	var isClosed = currentMenu.getAttribute(CLOSED_ATTRIBUTE) == "true";

	if(stepsLeft == 0) {
		if(isClosed) {
			currentMenu.setAttribute(CLOSED_ATTRIBUTE, "false");
			currentMenu.style.height = "auto";
		}
		else {
			currentMenu.setAttribute(CLOSED_ATTRIBUTE, "true");
			currentMenu.style.display = "none";
		}
		currentMenu.setAttribute(TOGGLING_ATTRIBUTE, "false");
		return;
	}
	currentMenu.setAttribute(TOGGLING_ATTRIBUTE, "true");
	var fraction = stepsLeft / NUM_STEPS;
	if(isClosed) fraction = 1 - fraction;

	if(!(fraction == 0 && isClosed))
	currentMenu.style.height = currentMenu.getAttribute(HEIGHT_ATTRIBUTE) * fraction;
	currentMenu.style.display = "block";
	setTimeout("timedToggle("+menuIndex+", "+(stepsLeft-1)+")", FULL_PERIOD / NUM_STEPS);
}
//this function is called from an <a href...> element with
//the name of the menu that needs to be expanded/contracted
function toggleMenu(name) {
	var i = 0;
	for(; i < menuDivs.length; ++i)
		if(menuDivs[i].getAttribute(MENU_ATTRIBUTE) == name) break;

	toggleMenuByIndex(i);
}
function toggleMenuByIndex(menuIndex) {
	if(menuIndex >= menuDivs.length) {
		alert("Invalid menu index: " + menuIndex);
		return;
	}
	var currentMenu = menuDivs[menuIndex];
	if(currentMenu.getAttribute(TOGGLING_ATTRIBUTE) == "true") return; //don't toggle if menu is folding right now
	var opened = currentMenu.getAttribute(CLOSED_ATTRIBUTE) != "true";
	menuToggled = currentMenu;
	timedToggle(menuIndex, NUM_STEPS);
	closeAllBut(menuIndex);
}
function closeAllBut(menuIndex) {
	for(var i = 0; i < menuDivs.length; ++i) {
		if(i != menuIndex && menuDivs[i].getAttribute(CLOSED_ATTRIBUTE) != "true")
			toggleMenuByIndex(i);
	}
}