var time = 1;	// Lower = Faster (miliseconds)
var rate = 5;	// Pixels per "time" (Higher = Faster)

function closeAllMenus() {
	var treeMenu = document.getElementById('tree_menu');
	var ulArray = treeMenu.getElementsByTagName('UL');
	for (i=0; i <= ulArray.length; i++) {
		try {
			if (ulArray[i].id.length > 1) {
				var ulIDPart = ulArray[i].id.substring(ulArray[i].id.length-3,ulArray[i].id.length);
				if (ulIDPart == '_ul') {
					ulArray[i].style.display = 'none';
				}
			}
		} catch(e){}
	}
}

function openMenu(linkObj, objID) {
	var obj = document.getElementById(objID);
	var objUL = document.getElementById(objID + '_ul');

	objUL.style.height = objUL.clientHeight;
	
	clearTransition(objUL);
	if (obj.className == "closed") {
		obj.className = "opened";
		linkObj.className = "opened";
		startTransition(objUL,'open');
	} else {
		obj.className = "closed";
		linkObj.className = "closed";
		startTransition(objUL,'close');
	}
}

var runTimeout;
function startTransition(obj,action) {
	obj.style.display = 'block';
	obj.style.overflow = 'hidden';

	var currentHeight = parseInt(obj.style.height);
	var totalHeight = obj.scrollHeight;
	var remainderHeight = totalHeight - currentHeight
	runTimeout = true;

	if (action == 'open' && currentHeight < totalHeight) {
		obj.style.height = currentHeight + (remainderHeight < rate ? remainderHeight : rate) + 'px';
	} else if (action == 'close' && currentHeight > rate) {
		obj.style.height = currentHeight - rate + 'px';		
	} else {
		clearTransition(obj);
		if (action == 'close') obj.style.display = 'none';
	}
	if (runTimeout) obj.timeout = setTimeout("loopTransition('"+obj.id+"','"+action+"')",time);
}

function loopTransition(objID,action) {
	var obj = document.getElementById(objID);
	startTransition(obj,action);
}

function clearTransition(obj) {
	clearTimeout(obj.timeout);
	runTimeout = false;
	obj.style.overflow = 'visible';
}
