var activeButton = null;
function buttonClick(event, menuId) 
{
	var button;
	if (thebrowser.isIE) button = window.event.srcElement;
	else button = event.currentTarget;
	if (button.menu == null) 
	{
		button.menu = document.getElementById(menuId);
		if (button.menu.isInitialized == null) menuInit(button.menu);
	}
	if (button.onmouseout == null) button.onmouseout = buttonOrMenuMouseout;
	if (button == activeButton) return false;
	if (activeButton != null) resetButton(activeButton);

	if (button != activeButton) {depressButton(button); activeButton = button;}
	else activeButton = null;
	return false;
}

function openmenu(event, menuId) {
	var button;
	if (activeButton == null) 
	{
		buttonClick(event, menuId);
		return;
	}
	if (thebrowser.isIE) button = window.event.srcElement;
	else button = event.currentTarget;

	if (activeButton != null && activeButton != button)
	buttonClick(event, menuId);
}
function depressButton(button) {
	var x, y;
	button.className += " menuButtonActive";

	if (button.onmouseout == null)
	button.onmouseout = buttonOrMenuMouseout;
	if (button.menu.onmouseout == null)
	button.menu.onmouseout = buttonOrMenuMouseout;

	x = getPageOffsetLeft(button);
	y = getPageOffsetTop(button) + button.offsetHeight;

	if (thebrowser.isIE) 
	{
		x += button.offsetParent.clientLeft;
		y += button.offsetParent.clientTop;
	}
	button.menu.style.left = x + "px";
	button.menu.style.top  = y + "px";
	button.menu.style.visibility = "visible";
	if (button.menu.iframeEl != null)
	{
		button.menu.iframeEl.style.left = button.menu.style.left;
		button.menu.iframeEl.style.top  = button.menu.style.top;
		button.menu.iframeEl.style.width  = button.menu.offsetWidth + "px";
		button.menu.iframeEl.style.height = button.menu.offsetHeight + "px";
		button.menu.iframeEl.style.display = "";
	}
}

function resetButton(button) 
{
	removeClassName(button, "menuButtonActive");
	if(thebrowser.isIE) removeClassName(button.offsetParent, "over");
	else removeClassName(button, "over");
	if (button.menu != null) 
	{
		closeSubMenu(button.menu);
		button.menu.style.visibility = "hidden";

		if (button.menu.iframeEl != null) button.menu.iframeEl.style.display = "none";
	}
}

function mmov(event) 
{
	var menu;
	if (thebrowser.isIE) menu = getContainerWith(window.event.srcElement, "DIV", "menu");
	else menu = event.currentTarget;
	if (menu.activeItem != null) closeSubMenu(menu);
}

function closeSubMenu(menu) 
{
	if (menu == null || menu.activeItem == null) return;

	if (menu.activeItem.subMenu != null) 
	{
		closeSubMenu(menu.activeItem.subMenu);
		menu.activeItem.subMenu.style.visibility = "hidden";

		if (menu.activeItem.subMenu.iframeEl != null)
		  menu.activeItem.subMenu.iframeEl.style.display = "none";

		menu.activeItem.subMenu = null;
	}
	menu.activeItem = null;
}

function buttonOrMenuMouseout(event) {
  var el;
  if (activeButton == null)
    return;

  if (thebrowser.isIE)
    el = window.event.toElement;
  else if (event.relatedTarget != null)
      el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);

  if (getContainerWith(el, "DIV", "menu") == null) {
    resetButton(activeButton);
    activeButton = null;
  }
}

function menuInit(menu) {
  var itemList;
  var textEl, arrowEl;
  var itemWidth;
  var w, dw;
  var i, j;
  if (thebrowser.isIE) {
    menu.style.lineHeight = "2.5ex";    
  }

  itemList = menu.getElementsByTagName("A");
  
  if (itemList.length > 0)
    itemWidth = itemList[0].offsetWidth;
  else
    return;

  for (i = 0; i < itemList.length; i++) {
    textEl  = null;
    arrowEl = null;
    
    if (textEl != null && arrowEl != null) {
      textEl.style.paddingRight = (itemWidth 
        - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";

      if (thebrowser.isOP)
        arrowEl.style.marginRight = "0px";
    }
  }

  if (thebrowser.isIE) {
    w = itemList[0].offsetWidth;
    itemList[0].style.width = w + "px";
    dw = itemList[0].offsetWidth - w;
    w -= dw;
    itemList[0].style.width = w + "px";
  }

  if (thebrowser.isIE) {
    var iframeEl = document.createElement("IFRAME");
    iframeEl.frameBorder = 0;
    iframeEl.src = "javascript:;";
    iframeEl.style.display = "none";
    iframeEl.style.position = "absolute";
    iframeEl.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
    menu.iframeEl = menu.parentNode.insertBefore(iframeEl, menu);
  }
  menu.isInitialized = true;
}

function getContainerWith(node, tagName, className) {
  while (node != null) {
    if (node.tagName != null && node.tagName == tagName &&
        hasClassName(node, className))
      return node;
    node = node.parentNode;
  }
  return node;
}

function hasClassName(el, name) {
  var i, list;
  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function removeClassName(el, name) {
  var i, curList, newList;
  if (el.className == null)
    return;

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function getPageOffsetLeft(el) {
  var x;
  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);
  return x;
}

function getPageOffsetTop(el) {
  var y = el.offsetTop;
  
  if (el.offsetParent != null)
   { y += getPageOffsetTop(el.offsetParent);}

  return y;
}