var menuHeights = new Array();
var menuNames = new Array();
var menuSpeed = 9;

function collapseMenu(menuIndex) {
	menuId = menuNames[menuIndex];
	menu = document.getElementById('ul' + menuId);
	// get current height of menu from DOM style tree
	height = parseInt(menu.style.height);
	liElement = document.getElementById('li' + menuId);
	// if element becomes 'selected' don't call collapseMenu again
	if ((height > 0) && (liElement.className != 'selected')) {
		height -= menuSpeed;
		height = (height < 0)?0:height;
		menu.style.height = height + 'px';
		setTimeout('collapseMenu('+menuIndex+')', 5);
	}
	// if height is 0 do not display menu at all
	// required to fix IE borkage!?
	if (height <= 0) {
		menu.style.display = 'none';
	}
}
function expandMenu(menuIndex) {
	menuId = menuNames[menuIndex];
	menu = document.getElementById('ul' + menuId);
	// get current height of menu from DOM style tree
	height = parseInt(menu.style.height);
	liElement = document.getElementById('li' + menuId);
	// if element becomes un-'selected' don't call expandMenu again
	if ((height < menuHeights[menuIndex]) && (liElement.className == 'selected')) {
		height += menuSpeed;
		height = (height > menuHeights[menuIndex])?menuHeights[menuIndex]:height;
		menu.style.display = 'block';
		menu.style.height = height + 'px';
		setTimeout('expandMenu("'+menuIndex+'")', 5);
	}
}
/*function hideSubmenus() {
	// hide non-selected sub menus in javascript enabled browsers
	// also load global arrays with correct values
	menus = document.getElementsByName('topmenu');
	
	for (i = 0; i < menus.length; i++) {
		menu = menus[i];
		menuNames[i] = menu.id;
		childId = 'ul' + menu.id;
		liId = 'li' + menu.id;
		var childElement = document.getElementById(childId)
		var liElement = document.getElementById(liId);
		menuHeights[i] = childElement.offsetHeight;
		if (liElement.className != 'selected') {
			childElement.style.height = '0px';
			childElement.style.display = 'none';
		}
		else {
			childElement.style.height = menuHeights[i] + 'px';
		}
	}
}*/
function hideSubmenus() {
  // hide non-selected sub menus in javascript enable browsers
  // also load global arrays with correct values
  menus = document.getElementsByName('topmenu');
  
  for (menuIndex = 0; menuIndex < menus.length; menuIndex++ ) {
    menu = menus[menuIndex];
    menuNames[menuIndex] = menu.id;
    childId = 'ul' + menu.id;
    liId = 'li' + menu.id;
    var childElement = document.getElementById(childId);
    var liElement = document.getElementById(liId);
    menuHeights[menuIndex] = childElement.offsetHeight;
    if (isSelected(childElement)) {
      childElement.style.height = menuHeights[menuIndex] + 'px';
      liElement.className = 'selected';
    }
    else {
			childElement.style.height = '0px';
			childElement.style.display = 'none';
    }
  }
}
function isSelected(menuElement) {
  children = menuElement.childNodes;
  
  for (j = 0; j < children.length; j++) {
    child = children[j];
    if (child.id == currentAsset) {
      return true;
    }
  }
  return false;
}
function toggleMenu(menu) {
	menuIndex = 0;
	while ((menuIndex < menuNames.length) && (menuNames[menuIndex] != menu.id)) {
		menuIndex++;
	}
	childId = 'ul' + menu.id;
	liId = 'li' + menu.id;
	liElement = document.getElementById(liId);
	if (liElement.className != 'selected') {
		liElement.className = 'selected';
		expandMenu(menuIndex);
	}
	else {
		liElement.className = '';
		collapseMenu(menuIndex);
	}
}
