//
// Copyright (c) 2006 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees. This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents. Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and the BroadVision logo are registered
// trademarks, and BroadVision One-To-One is a trademark of BroadVision,
// Inc. IONA and Orbix are trademarks of IONA Technologies, Ltd. RSA,
// MD5, and RC2 are trademarks of RSA Data Security, Inc. All other
// trademarks, service marks, and trade names belong to their respective
// owners. BroadVision, Inc. disclaims any proprietary interest in the
// marks and names of others.
//

	function getTop(obj) {
		var cur = 0;
		if(obj.offsetParent) {		
			while(obj.offsetParent) {
				cur+=obj.offsetTop;
				obj = obj.offsetParent;
			}
		}
		return cur;
	}

	// getLeft:
	//		Calculates the offsetLeft of the object.
	function getLeft(obj) {
		var cur = 0;
		if(obj.offsetParent) {		
			while(obj.offsetParent) {
				cur+=obj.offsetLeft;
				obj = obj.offsetParent;
			}
		}
		return cur;
	}

//
//
//
function Menubar(name, bHorizontal) {
  this.name = name;
  this.bHorizontal = bHorizontal;
  this.activateMenu = _menubar__activateMenu;
}

//
//
//
function _menubar__activateMenu(menuId) {
  var menu = g_menus[menuId];
  if (typeof menu == "undefined") {
    menu = new Menu(menuId);
    g_menus[menuId] = menu;
  }

  if (menuId != g_lastMenu) {
    if ("" != g_lastMenu) {
      g_menus[g_lastMenu].hide();
    }
    g_lastMenu = menuId;
  }

  var rootElement = document.getElementById(this.name);
var xLeft = getLeft(rootElement);
var yTop = getTop(rootElement);
//alert("left: " + xLeft + " top: " + yTop);
  var x = rootElement.offsetLeft;
  var y = rootElement.offsetTop;
//alert("left: " + x + " top: " + y);
  var itemElement = document.getElementById(this.name + "_" + menuId.substr(c_menuPrefix.length));
  var offsetLeft = itemElement.offsetLeft;
//alert("offsetLeft: " + offsetLeft);
  var offsetHeight = itemElement.offsetHeight;

x += 0;
y += 96;

  if (this.bHorizontal) {
//    while (itemElement.parentElement.parentElement != rootElement) { 
//      x += itemElement.offsetLeft;
//      y += itemElement.offsetTop;
//      itemElement = itemElement.parentElement;
//    }
//    x += 5;
//    y += offsetHeight - 2;

var xxLeft = getLeft(itemElement) - offsetLeft - 8;
var yTop = getTop(itemElement) + offsetHeight + 4;
x = xxLeft;
y = yTop;
//alert("left: " + xLeft + " top: " + yTop);
//alert("left: " + x + " top: " + y);

  }
  else {
    x += itemElement.offsetWidth - 2;
    y += itemElement.offsetTop + 5;
  }

  var clientHeight = browserEx.getClientHeight();
  if (y + menu.height - g_scrollTop > clientHeight) {
    y = clientHeight - menu.height + g_scrollTop;
  }
  menu.moveTo(x, y);

  if (g_menuVisible) {
    menu.show(true);
  }
  else {
    menu.show(true, c_showTimeout);
  }
}

//
//
//
function menubar_onmouseover(event) {
  if (c_disable) return;
  if (event && document.getElementById) {
    var element = (event.target) ? event.target : event.srcElement;
    clearTimeout(g_cleanupMenuTimer);
    g_isMenuTimerRunning = false;

    var id = element.id;
    if ("" == id) {
      element = element.parentElement || element.parentNode;
      id = element.id;
    }
    var needle = id.indexOf("_");
    var menubarId = id.substr(0, needle);
    var menuId = c_menuPrefix + id.substr(needle + 1);
    var menubar = g_menubars[menubarId];

    if (typeof menubar != "undefined") {
      menubar.activateMenu(menuId);
    }

    event.cancelBubble = true;
  }
}

//
//
//
function menubar_onmouseout(event) {
  if (c_disable) return;
  if (event && document.getElementById) {
    var element = (event.target) ? event.target : event.srcElement;

    var id = element.id;
    var needle = id.indexOf("_");
    var menubarId = id.substr(0, needle);
    var menuId = c_menuPrefix + id.substr(needle + 1);

    var menu = g_menus[menuId];
    if (typeof menu != "undefined") {
      clearTimeout(menu.showTimer);
    }
    event.cancelBubble = true;
  }
}

//
//
//
function Menu(menuId, parent) {
  this.id = menuId;
  this.element = new Div(menuId);
  this.menuitems = new Array();

  this.parent = parent;
  this.width = c_minMenuWidth;
  this.height = 0;

  this.lastItemIndex = -1;
  this.showTimer = 0;

  this.initialize = _menu__initialize;
  this.show = _menu__show;
  this.hide = _menu__hide;
  this.moveTo = _menu__moveTo;
  this.activateItem = _menu__activateItem;

  this.initialize();
}

//
//
//
function _menu__initialize() {
  var menu = document.getElementById(this.id);
  var items = menu.getElementsByTagName("div");
  for (var index = 0; index < items.length; index++) {
    if (index % 2 == 0) {
      var menuitemId = items[index].id;
      var menuitem = new MenuItem(menuitemId);
      var length = this.menuitems.length;
      this.menuitems[length] = menuitem;
    }
  }

  for (var index = 0; index < this.menuitems.length; index++) {
    var width = this.menuitems[index].width;
    if (width > this.width) {
      this.width = width;
    }
    this.height += this.menuitems[index].height;
  }

  var yTop = 0;
  for (var index = 0; index < this.menuitems.length; index++) {
    var menuitem = this.menuitems[index];
    var height = menuitem.height;
    menuitem.clipTo(0, this.width, height, 0);
    menuitem.moveTo(0, yTop);
    yTop += height;
  }

  this.element.clipTo(0, this.width + (browser.ns7 ? 1 : 0), this.height+1, 0);
}

//
//
//
function _menu__moveTo(x, y) {
  this.element.moveTo(x, y);
}

//
//
//
function _menu__show(bVisible, timeout) {
  if (null == timeout) {
    this.element.css.zIndex = g_zIndex++;
    this.element.show(bVisible);
    g_menuVisible = true;
  }
  else {
    this.showTimer = setTimeout("g_menus[\"" + this.id + "\"].show(true)",timeout);
  }
}

//
//
//
function _menu__hide() {
  this.element.show(false);
  if (-1 != this.lastItemIndex) {
    var menuitem = this.menuitems[this.lastItemIndex];
    menuitem.activate(false);
  }
}

//
//
//
function _menu__activateItem(itemIndex) {
  if (this.lastItemIndex != itemIndex) {
    if (this.lastItemIndex != -1) {
      var menuitem = this.menuitems[this.lastItemIndex];
      menuitem.activate(false);
    }
  }

  var menuitem = this.menuitems[itemIndex];
  menuitem.activate(true);
  this.lastItemIndex = itemIndex;
}

//
//
//
function MenuItem(id) {
  this.id = id;
  this.menuId = "";
  this.itemIndex = -1;
  this.submenuId = "";

  var needle = id.indexOf("_");
  if (-1 != needle) {
    this.menuId = id.substr(0, needle);
    var rest = id.substr(needle + 1);
    var needle = rest.indexOf("_");
    if (-1 == needle) {
      this.itemIndex = parseInt(rest);
    }
    else {
      this.itemIndex = parseInt(rest.substr(0, needle));
      this.submenuId = c_menuPrefix + rest.substr(needle + 1);
    }
  }

  this.item = new Div(this.id);
  this.img = new Div(this.menuId + "_" + this.itemIndex + c_imageSuffix);

  this.width = this.item.element.offsetWidth;
  this.height = this.item.element.offsetHeight;

  this.highlight = _menuItem__highlight;
  this.moveTo = _menuItem__moveTo;
  this.clipTo = _menuItem__clipTo;
  this.activate = _menuItem__activate;
}

//
//
//
function _menuItem__highlight(bHighlight) {
  var className = bHighlight ? c_menuOnStyle : c_menuOffStyle;
  document.getElementById(this.id).className = className;
}

//
//
//
function _menuItem__moveTo(x, y) {
  this.item.moveTo(x, y);
  this.img.moveTo(x + this.width - c_imageWidth - 4, y);
  if ("" == this.submenuId) {
    this.img.show(false);
  }
}

//
//
//
function _menuItem__clipTo(t, r, b, l) {
  this.item.clipTo(t, r, b, l);
  this.width = r - l;
  this.height = b - t;
}

//
//
//
function _menuItem__activate(bActivate) {
  if (bActivate) {
    this.highlight(true);
    if ("" != this.submenuId) {
      var menu = g_menus[this.menuId];
      var submenu = g_menus[this.submenuId];
      if (typeof submenu == "undefined") {
        submenu = new Menu(this.submenuId);
        g_menus[this.submenuId] = submenu;
      }
      var x = menu.element.x + this.width;
      var y = menu.element.y + this.item.y;

      var clientHeight = browserEx.getClientHeight();
      if (y + submenu.height - g_scrollTop > clientHeight) {
        y = clientHeight - submenu.height + g_scrollTop;
      }

      submenu.moveTo(x, y);
      submenu.show(true);
    }
  }
  else {
    this.highlight(false);
    if ("" != this.submenuId) {
      g_menus[this.submenuId].hide();
    }
  }
}

//
//
//
function menuitem_onmouseover(event) {
  if (c_disable) return;

  var element = (event.target) ? event.target : event.srcElement;
  var id = element.id;
  var needle = id.indexOf("_");
  if (-1 != needle) {
    var menuId =  id.substr(0, needle);
    var itemIndex = id.substr(needle + 1);

    var menu = g_menus[menuId];
    menu.activateItem(parseInt(itemIndex));
  }

  clearTimeout(g_cleanupMenuTimer);
  g_isMenuTimerRunning = false;

  event.cancelBubble = true;
}

//
//
//
function menu_cleanup() {
  if ("" != g_lastMenu) {
    g_menus[g_lastMenu].hide();
    g_lastMenu = "";
  }

  g_isMenuTimerRunning = false;
  g_menuVisible = false;
}

//
//
//
function menu_mouseover_listener() {
  if (("" != g_lastMenu) && (!g_isMenuTimerRunning)) {
    g_cleanupMenuTimer = setTimeout("menu_cleanup()", c_cleanupMenuTimeout);
    g_isMenuTimerRunning = true;
  }
}

//
// Globals
//

  var g_menubars = new Array();
  var g_menus = new Array();

  var g_lastMenu = "";
  var g_menuVisible = false;
  var g_cleanupMenuTimer = 0;
  var g_isMenuTimerRunning = false;
  var g_zIndex = 200;

  var c_minMenuWidth = 100;
  var c_disable = false;

  var c_cleanupMenuTimeout = 500;
  var c_showTimeout= 500;

  var c_menuOnStyle = "menuItemOn";
  var c_menuOffStyle = "menuItemOff";

  var c_menuPrefix = "mm";
  var c_imageSuffix = "i";
  var c_imageWidth = 10;

