// check browsers
var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var ie = !opera && /MSIE/.test(ua);
var ie50 = ie && /MSIE 5\.[01234]/.test(ua);
var ie6 = ie && /MSIE [6789]/.test(ua);
var ieBox = ie && (document.compatMode == null || document.compatMode != "CSS1Compat");
var moz = !opera && /gecko/i.test(ua);
var nn6 = !opera && /netscape.*6\./i.test(ua);

function IsIE() {
  if (ie || ie50 || ie6 || ieBox) {
    return true;
  }
  else {
    return false;
  }
}

var activeButton = null;

function btnClick(event, menuId) {
  btnClick2(event, menuId, 0);
}

function btnClick2(event, menuId, offset) {

  var button;

  if (IsIE())
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  button.blur();

  if (button.menu == null) {
    button.menu = document.getElementById(menuId);
    if (button.menu.isInitialized == null)
      qmenuInit(button.menu);
  }

  if (button.onmouseout == null)
    button.onmouseout = buttonOrMenuMouseout;

  if (button == activeButton)
    return false;

  if (activeButton != null)
    resetBtn(activeButton);

  if (button != activeButton) {
    clickBtn(button, offset);
    activeButton = button;
  }
  else
    activeButton = null;

  return false;
}

function barBtnMouseOver(event, menuId, horiz) {

  var button;
  var offset = (horiz) ? 0: 20;

  if (activeButton == null) {
    btnClick2(event, menuId, offset);
    return;
  }

  if (IsIE())
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  if (activeButton != null && activeButton != button)
    btnClick2(event, menuId, offset);
}

function clickBtn(button, offset) {

  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;

  // For IE, adjust position.

  if (IsIE()) {
    x += button.offsetParent.clientLeft;
    y += button.offsetParent.clientTop;
  }
  
  if (offset > 0) {
    x += offset;
    y -= (button.offsetHeight - 3);
  }
  else {
    x -= 2;
    y -= 2;
  }

  button.menu.style.left = x + "px";
  button.menu.style.top  = y + "px";
  button.menu.style.visibility = "visible";
}

function resetBtn(button) {

  removeClassName(button, "menuButtonActive");

  if (button.menu != null) {
    closeSubMenu(button.menu);
    button.menu.style.visibility = "hidden";
  }
}

function menuMouseOver(event) {

  var menu;

  if (IsIE())
    menu = getContainerWith(window.event.srcElement, "DIV", "menu");
  else
    menu = event.currentTarget;

  if (menu.activeItem != null)
    closeSubMenu(menu);
}

function menuItemMouseOver(event, menuId) {

  var item, menu, x, y;

  if (IsIE())
    item = getContainerWith(window.event.srcElement, "A", "menuItem");
  else
    item = event.currentTarget;
  menu = getContainerWith(item, "DIV", "menu");

  if (menu.activeItem != null)
    closeSubMenu(menu);
  menu.activeItem = item;

  item.className += " menuItemHighlight";

  if (item.subMenu == null) {
    item.subMenu = document.getElementById(menuId);
    if (item.subMenu.isInitialized == null)
      qmenuInit(item.subMenu);
  }

  if (item.subMenu.onmouseout == null)
    item.subMenu.onmouseout = buttonOrMenuMouseout;

  x = getPageOffsetLeft(item) + item.offsetWidth;
  y = getPageOffsetTop(item);

  var maxX, maxY;

  if (IsIE()) {
    maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
      (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
    maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
      (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
  }
  else {
    //
    // Assume NS for the rest
    //
    maxX = window.scrollX + window.innerWidth;
    maxY = window.scrollY + window.innerHeight;
  }
  maxX -= item.subMenu.offsetWidth;
  maxY -= item.subMenu.offsetHeight;

  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
      + (menu.offsetWidth - item.offsetWidth));
  y = Math.max(0, Math.min(y, maxY));

  item.subMenu.style.left = x + "px";
  item.subMenu.style.top  = y + "px";
  item.subMenu.style.visibility = "visible";

  if (IsIE())
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}

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";
    menu.activeItem.subMenu = null;
  }
  removeClassName(menu.activeItem, "menuItemHighlight");
  menu.activeItem = null;
}

function buttonOrMenuMouseout(event) {

  var el;

  if (activeButton == null)
    return;

  if (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) {
    resetBtn(activeButton);
    activeButton = null;
  }
}

function qmenuInit(menu) {

  var itemList;
  var itemWidth;
  var w, dw;
  var i, j;

  itemList = menu.getElementsByTagName("A");
  if (itemList.length > 0)
    itemWidth = itemList[0].offsetWidth;
  else
    return;

  if (IsIE()) {
    w = itemList[0].offsetWidth;
    itemList[0].style.width = w + "px";
    dw = itemList[0].offsetWidth - w;
    w -= dw;
    itemList[0].style.width = w + "px";
  }

  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;

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

function qmenuBarBegin()
{
  document.write('<DIV class=qmenuBar style="FLOAT: left;">');
}

function qmenuBarAddTxBtn(name, btn_msg, menu_url, horiz)
{
  document.write('<A class=txMenuBtn onmouseover="barBtnMouseOver(event, \'' + name + '\', ' + horiz + ');" onclick="return btnClick(event, \'' + name + '\');" href="' + menu_url + '">');
  document.write(btn_msg);
  document.write('</A>');
  if (!horiz) {
    document.write('<br><br>');
  }
}

function qmenuBarAddImgBtn(name, img_path, menu_url, horiz)
{
  document.write('<A class=imgMenuBtn onmouseover="barBtnMouseOver(event, \'' + name + '\', ' + horiz + ');" onclick="return btnClick(event, \'' + name + '\');" href="' + menu_url + '"><img src="' + img_path + '" border=0></A>');
  if (!horiz) {
    document.write('<br>');
  }
}

function qmenuBarAddTxItem(btn_msg, menu_url, target_str)
{
  document.write('<A class=txMenuBtn href="' + menu_url + '" ' + target_str + '>');
  document.write(btn_msg);
  document.write('</A>');
}

function qmenuBarAddImgItem(img_path, menu_url, target_str)
{
  document.write('<A class=imgMenuBtn href="' + menu_url + '" ' + target_str + '><img src="' + img_path + '" border=0></A>');
}

function qmenuBarEnd()
{
  document.write('</DIV>');
}

function qmenuBegin(name)
{
  document.write('<DIV style="z-index:9999" class=menu id=\'' + name + '\' onmouseover=menuMouseOver(event)>');
}

function qmenuAddItem(btn_msg, menu_url, target_str)
{
  document.write('<A class=menuItem href="' + menu_url + '" ' + target_str + '>');
  document.write(btn_msg);
  document.write('</A>');
}

function qmenuAddSubMenu(sub_name, btn_msg)
{
  document.write('<A class=menuItem onmouseover="menuItemMouseOver(event, \'' + sub_name + '\');" onclick="return false;" href="javascript:void(0)">');
  document.write(btn_msg);
  document.write('&nbsp;&nbsp;&nbsp;&nbsp;<img src="/pix1/icon/arrow.right.png" border=0></A>');
}

function qmenuSeparator()
{
  document.write('<DIV style="z-index:9999" class=menuItemSep></DIV>');
}

function qmenuEnd()
{
  document.write('</DIV>');
}

