function initMenu()
{
  var menu = document.getElementById('menu');
  if (menu == null) return;
  var children = menu.getElementsByTagName('li');

  for (var i = 0; i < children.length; i++) {
    if (children[i].nodeType != 1) continue;
    children[i].onclick = menuExpand;

    // Collapse submenu except if a submenu is active
    blocks = children[i].getElementsByTagName('ul');
    if ((blocks.length > 0) && (!hasActiveEntry(blocks[0]))) {
      blocks[0].style.display = 'none';
    }
  }
}


// Check whether a menu block has an active <li> entry
function hasActiveEntry(block)
{
  entries = block.getElementsByTagName('li');
  for (var k = 0; k < entries.length; k++) {
    if (entries[k].className.indexOf('active') >= 0) {
      return true;
    }
  }
  return false;
}


// Expand or collapse submenu
function menuExpand(e)
{
  var follow = true;
  blocks = this.getElementsByTagName('ul');
  // Note: properties set in CSS are not returned with .style.display
  if (blocks.length > 0) {
    blocks[0].style.display = (blocks[0].style.display == 'block' ? 'none' : 'block');
    follow = false;
  }

  // Stop event propagation for what it's worth
  if (e) {
    e.stopPropagation();
  } else {
    window.event.cancelBubble = true;
  }
  return follow;
}
