function GetMenu(id)
{
  var obj = g_GUI_Objects[id];
  return obj;
}
var g_counter = 0;


function GUI_MenuItem(parentMenu, caption, action, normalStyle, hoverStyle)
{
  this.prototype = new GUI_Object();

  for (i in this.prototype)
    this[i] = this.prototype[i];
  this.CreateHtmlObject(parentMenu.m_vertical ? 'DIV' : 'SPAN');

  this.m_type = 'GUI_MenuItem';
  this.m_normalStyleName = normalStyle;
  this.m_hoverStyleName = hoverStyle;
  this.m_normalStyle = GetCssDefinition(normalStyle);
  this.m_hoverStyle = GetCssDefinition(hoverStyle);

  this.m_innerDiv = document.createElement('DIV');
  this.m_innerDiv.style.float = 'left';
  this.m_innerDiv.appendChild(document.createTextNode(caption));
  this.m_htmlObject.appendChild(this.m_innerDiv);
  this.m_htmlObject.m_guiObject = this;
  this.m_hover = false;
  this.m_arrowSrcHover = undefined;
  this.m_arrowSrc = undefined;

  
  this.AddArrow = function(arrowSrc, srcHover, style)
  {
    this.m_arrowSrcHover = srcHover;
    this.m_arrowSrc = arrowSrc;

    if (arrowSrc != undefined)
    {
      obj = document.createElement('img');
      obj.src = arrowSrc;
    }
    else
      obj = document.createElement('div');

    this.m_normalStyle += ';padding-right:2px;';
    this.m_hoverStyle += '; padding-right:2px;';

    obj.style.cssText = GetCssDefinition(style);
    obj.style.float = 'right';
    this.m_arrow = obj;
    this.m_innerDiv.appendChild(obj);
    this.m_htmlObject.style.cssText = this.m_normalStyle;
  }


  this.m_htmlObject.onmouseover = function()
  {
    this.m_guiObject.m_parentMenu.OnOverMenuItem(this.m_guiObject);
  }


  this.m_htmlObject.onmouseout = function()
  {
    this.m_guiObject.m_parentMenu.OnOutMenuItem(this.m_guiObject);
  }


  this.m_htmlObject.onmousedown = function()
  {
    if (this.m_guiObject.m_action != undefined)
      this.m_guiObject.m_action();
  }


  this.Focus = function()
  {
  }


  this.Blur = function()
  {
  }


  this.ShowNormalStyle = function()
  {
    if (this.m_hover)
      this.m_htmlObject.style.cssText = this.m_normalStyle;

    if (this.m_arrow)
      this.m_arrow.src = this.m_arrowSrc;
    this.m_hover = false;
    if (this.m_subMenu)
      this.m_subMenu.Hide();
  }


  this.ShowHoverStyle = function()
  {
    if (!this.m_hover)
      this.m_htmlObject.style.cssText = this.m_hoverStyle;
    if (this.m_arrow)
      this.m_arrow.src = this.m_arrowSrcHover;
    this.m_hover = true;
  }


  this.AddSubMenu = function(style, vertical, arrowSrc, arrowSrcHover, arrowStyle)
  {
    if (style == undefined)
      style = this.m_parentMenu.m_styleName;
    if (vertical == undefined)
      vertical = true;
    if ((this.m_parentMenu.m_vertical) && ((arrowSrc != undefined) || (arrowStyle != undefined)))
      this.AddArrow(arrowSrc, arrowSrcHover, arrowStyle);
    this.m_subMenu = new GUI_Menu(this.m_parentMenu.m_parent, style, vertical);
    this.m_subMenu.m_parentMenu = this.m_parentMenu;
    this.m_subMenu.Hide();
    return this.m_subMenu;
  }



  this.HideSubMenu = function()
  {
    if (this.m_subMenu != undefined)
      this.m_subMenu.Hide();
  }


  this.ShowSubMenu = function()
  {
    if (this.m_subMenu != undefined)
    {
      if (this.m_parentMenu.m_vertical)
      {
         var y = this.Top();
         var x = this.m_parentMenu.Left() + this.m_parentMenu.Width() - 1 - 2;
         this.m_subMenu.SetPosition(x, y);
         this.m_subMenu.Show();
      }
      else
      {
         var x = this.Left();
         var y = this.m_parentMenu.Top() + this.m_parentMenu.Height() - 1;
         this.m_subMenu.SetPosition(x, y);
         this.m_subMenu.Show();
      }
    }
  }

  this.m_subMenu = undefined;
  this.m_parentMenu = parentMenu;

  this.m_caption = caption;
  this.m_action = action;

  this.m_htmlObject.style.cssText = this.m_normalStyle;

  this.SetParent(parentMenu.GetHTML());

  return this;
}






function GUI_Menu(parent, style, vertical)
{
  this.prototype = new GUI_Object();

  for (i in this.prototype)
    this[i] = this.prototype[i];

  this.m_type = 'GUI_Menu';
  this.m_vertical = vertical;
  this.m_items = new Array();
  this.m_parentMenu = undefined;


  this.AddItem = function(caption, action, normalStyle, hoverStyle)
  {
    var ret = new GUI_MenuItem(this, caption, action, normalStyle, hoverStyle);
    this.m_items[this.m_items.length] = ret;
    return ret;
  }


  this.AddBreak = function(style)
  {
    var obj = document.createElement('DIV');
    
    obj.style.cssText = GetCssDefinition(style);
    obj.m_guiObject = this;
    obj.style.fontSize = '1px';
    obj.innerHTML = '&nbsp;';

    this.m_htmlObject.appendChild(obj);
    obj.onmouseover = function(){this.m_guiObject.OnOverMenuItem(undefined);}
  }


  this.CreateHtmlObject('DIV');
  this.m_htmlObject.m_guiObject = this;
  this.m_outMenuItemTimer = undefined;

  this.ClearOutMenuItemTimer = function()
  {
    if (this.m_outMenuItemTimer != undefined)
      window.clearTimeout(this.m_outMenuItemTimer);
    this.m_outMenuItemTimer = undefined;
  }


  this.SetOutMenuItemTimer = function(delay)
  {
    this.ClearOutMenuItemTimer();
    this.m_outMenuItemTimer = window.setTimeout('GetMenu("' + this.GetID() + '").OnOutMenuItemTimer();', delay);
  }


  this.OnOutMenuItemTimer = function()
  {
    this.ClearOutMenuItemTimer();
    for (var i = 0; i < this.m_items.length; i++)
    {
      var item = this.m_items[i];
      if (item.m_hover)
      {
        item.ShowNormalStyle();
        item.HideSubMenu();
        if (this.m_parentMenu)
	  this.m_parentMenu.OnOutMenuItemTimer();
      }
    } 
  }


  this.OnOverMenuItem = function(menuItem)
  {
    this.ClearOutMenuItemTimer();
    for (var i = 0; i < this.m_items.length; i++)
    {
      var item = this.m_items[i];
      if (item != menuItem)
      {
        item.ShowNormalStyle();
        item.HideSubMenu();
      }
      else
      {
        if (this.m_parentMenu != undefined)
          this.m_parentMenu.ClearOutMenuItemTimer();
        item.ShowHoverStyle();
        item.ShowSubMenu();
      }
    }
  }


  this.OnHide = function()
  {
    this.ClearOutMenuItemTimer();
    for (var i = 0; i < this.m_items.length; i++)
    {
      var item = this.m_items[i];
      if (item.m_hover)
        item.HideSubMenu();
    } 
  }


  this.OnShow = function()
  {
    this.ClearOutMenuItemTimer();
    for (var i = 0; i < this.m_items.length; i++)
    {
      var item = this.m_items[i];
      if (item.m_hover)
        item.ShowNormalStyle();
    } 
  }


  this.OnOutMenuItem = function(menuItem)
  {
    this.SetOutMenuItemTimer(200);
  }


  this.m_htmlObject.onmouseover = function()
  {
    this.m_guiObject.ClearOutMenuItemTimer();
  }


  this.m_htmlObject.onmouseout = function()
  {
    this.m_guiObject.SetOutMenuItemTimer(200);    

  }


  this.Focus = function()
  {
  }


  this.Blur = function()
  {
  }

  this.m_style = GetCssDefinition(style);
  this.m_styleName = style;
  this.m_htmlObject.style.cssText = this.m_style;

  this.SetParent(parent);

  return this;
}
