function Menu_itemOver(newMenuItemId)
{
  clearInterval(this.delaytimer);
  this.delaytimer = null;

  if (this.currentMenuItemId == newMenuItemId)
    return;

  if (this.process == 'none')
  {
    var indent = document.getElementById('indent');
    indent.style.width = '0px';

    var currentItemType = this.GetSubType(this.currentMenuItemId);

    if (currentItemType == 'SubItem')
      this.HideSubItem(this.currentMenuItemId, newMenuItemId);

    if (currentItemType == 'DropItem')
      this.HideDropItem(this.currentMenuItemId, newMenuItemId);

    if (currentItemType == null)
      this.ShowItem(newMenuItemId);

    var firstItem = document.getElementById(this.menuPrefix+'1');
    var curItem = document.getElementById(this.menuPrefix+newMenuItemId);
    var subMenu = document.getElementById(this.subMenuPrefix+newMenuItemId);

    var menuWidth = document.getElementById(this.menuLinePrefix+'2').offsetWidth;
    var width = curItem.offsetLeft - firstItem.offsetLeft;
    var subMenuWidth = subMenu.offsetWidth;

    // Если меню не вмещается, то выровнять его по правому краю
    if ((width+subMenuWidth)>menuWidth)
      // Из-за бага в IE 6.0 оставляем чуть-чуть больше места
      width = menuWidth - subMenuWidth - 3;

    indent.style.width = width+'px';
  }
  else
  {
    this.mustShow = newMenuItemId;
  }
}

function Menu_itemOut(event)
{
  if (this.delaytimer == null)
  {
    var t = this;
    this.delaytimer = setInterval(function() {return t.Collapsed();}, this.delay);
  }
}

function Menu_subItemOver()
{
  clearInterval(this.delaytimer);
  this.delaytimer = null;
}

function Menu_subItemOut()
{
  if (this.delaytimer == null)
  {
    var t = this;
    this.delaytimer = setInterval(function() {return t.Collapsed();}, this.delay);
  }
}

function Menu_getItem(itemId)
{
  var object = document.getElementById(this.menuPrefix + itemId);
  return object;
}

function Menu_getSubItem(itemId)
{
  var object = document.getElementById(this.subMenuPrefix + itemId);
  if (object != null)
    return object;
  else
    return document.getElementById(this.dropMenuPrefix + itemId);
}

function Menu_getSubType(menuItemId)
{
  var menuItem = this.GetSubItem(menuItemId);

  if (menuItem != null)
  {
    if (menuItem.id.indexOf(this.subMenuPrefix) != -1)
      return 'SubItem';
    else
      return 'DropItem';
  }
  else
    return null;
}

function Menu_getFront(menuItemId)
{
  var object = document.getElementById(this.dropMenuPrefix + this.frontPrefix + menuItemId);
  return object;
}

function Menu_getContent(menuItemId)
{
  var object = document.getElementById(this.dropMenuPrefix + this.contentPrefix + menuItemId);
  return object;
}


function Menu_hideSubItem(oldMenuItemId, newMenuItemId)
{
  var oldMenuSubItem = this.GetSubItem(oldMenuItemId);
  oldMenuSubItem.style.display = 'none';
  var oldMenuItem = this.GetItem(oldMenuItemId);
  this.ShowItem(newMenuItemId);
}

function Menu_showSubItem(menuItemId)
{
  var menuItem = this.GetItem(menuItemId);
  var menuSubItem = this.GetSubItem(menuItemId);
  menuSubItem.style.display = 'block';

  this.currentMenuItemId = menuItemId;
}


function Menu_hideDropItem(oldMenuItemId, newMenuItemId)
{
  var oldMenuDropItem = this.GetSubItem(oldMenuItemId);

  var content;
  if (this.process != 'hidding')
  {
    oldMenuDropItem.style.borderColor = 'black';
    content = this.GetContent(oldMenuItemId);
    content.style.display = 'none';
  }

  this.process = 'hidding';

  var width = this.GetItemWidth(oldMenuDropItem);
  var height = this.GetItemHeight(oldMenuDropItem);
  var left = this.GetItemLeft(oldMenuDropItem);

  if ((width > 0) || (height > 0))
  {
    var dwidth = this.dropWidthStep;
    if (width - this.dropWidthStep < 0)
    {
      dwidth = width;
    }

    height = Math.max(0, height - this.dropHeightStep);

    if (height == 0)
      oldMenuDropItem.style.borderColor = 'white';

    oldMenuDropItem.style.left = left + Math.floor(dwidth / 2) + 'px';
    oldMenuDropItem.style.width = Math.floor(width - dwidth) + 'px';
    oldMenuDropItem.style.height = height + 'px';

    var t = this;
    setTimeout(function() {return t.HideDropItem(oldMenuItemId, newMenuItemId)}, 1);
  }
  else
  {
    content = this.GetContent(oldMenuItemId);
    content.style.display = 'block';

    var front = this.GetFront(oldMenuItemId);
    front.style.display = 'block';

    var oldMenuItem = this.GetItem(oldMenuItemId);
    this.process= 'none';

    if (this.mustShow != null)
    {
      var mustShowId = this.mustShow;
      this.mustShow = null;
      this.ShowItem(mustShowId);
    }
    else
      this.ShowItem(newMenuItemId);
  }
}

function Menu_showDropItem(menuItemId)
{
  var newMenuDropItem = this.GetSubItem(menuItemId);
  if (this.process != 'showing')
  {
    newMenuDropItem.style.borderColor = 'black';
  }

  this.process = 'showing'

  var width = this.GetItemWidth(newMenuDropItem);
  var height = this.GetItemHeight(newMenuDropItem);
  var left = this.GetItemLeft(newMenuDropItem);

  if ((width < this.dropItemWidth) || (height < this.dropItemHeight))
  {
    var dwidth = this.dropWidthStep;
    if (width + this.dropWidthStep > this.dropItemWidth)
    {
      dwidth = this.dropItemWidth - width;
    }

    newMenuDropItem.style.left = left - Math.floor(dwidth / 2) + 'px';

    newMenuDropItem.style.width = Math.floor(width + dwidth) + 'px';
    newMenuDropItem.style.height = Math.min(this.dropItemHeight, height + this.dropHeightStep) + 'px';

    var t = this;
    setTimeout(function() {return t.ShowDropItem(menuItemId)}, 1);
  }
  else
  {
    newMenuDropItem.style.borderColor = 'white';

    var front = this.GetFront(menuItemId);
    front.style.display = 'none';

    var menuItem = this.GetItem(menuItemId);

    this.currentMenuItemId = menuItemId;
    this.process= 'none';

    if (this.mustShow != null)
    {
      var mustShowId = this.mustShow;
      this.mustShow = null;
      this.ItemOver(mustShowId);
    }
  }
}


function Menu_getItemWidth(menuItem)
{
  var width = 0;
  var pos = menuItem.style.width.indexOf('px');
  if (pos > 0)
      width = Number(menuItem.style.width.substring(0, pos));
  return width;
}

function Menu_getItemHeight(menuItem)
{
  var height = 0;
  var pos = menuItem.style.height.indexOf('px');
  if (pos > 0)
      height = Number(menuItem.style.height.substring(0, pos));
  return height;
}

function Menu_getItemLeft(menuItem)
{
  return Number(menuItem.offsetLeft);
}


function Menu_showItem(menuItemId)
{
  var subMenuItemType = this.GetSubType(menuItemId);

  if (subMenuItemType == 'SubItem')
  {
    this.ShowSubItem(menuItemId);
  }
  if (subMenuItemType == 'DropItem')
  {
    this.ShowDropItem(menuItemId);
  }
}

function Menu_collapsed()
{
  this.count++;
  if (this.count > 10)
  {
    this.count = 0;
    clearInterval(this.delaytimer);
    this.delaytimer = null;
    this.ItemOver(this.selectedMenuItemId);
  }
}


function Menu(name, selectedMenuItemId, dropItemWidth, dropItemHeight, dropStep, delay)
{
  this.name = name;

  this.process = 'none';

  this.currentMenuItemId = 0;
  this.selectedMenuItemId = selectedMenuItemId;
  this.menuLinePrefix = name + 'Line';
  this.menuPrefix = name + 'Item';
  this.subMenuPrefix = name + 'SubItem';
  this.dropMenuPrefix = name + 'DropItem';
  this.contentPrefix = 'Content';
  this.frontPrefix = 'Front';

  this.dropItemWidth = dropItemWidth;
  this.dropItemHeight = dropItemHeight;

  this.dropWidthStep = Math.floor(dropItemWidth / dropStep);
  this.dropHeightStep = Math.floor(dropItemHeight / dropStep);

  this.delay = delay;

  this.delaytimer = null;

  this.count = 0;

  this.ItemOver = Menu_itemOver;
  this.ItemOut = Menu_itemOut;

  this.SubItemOver = Menu_subItemOver;
  this.SubItemOut = Menu_subItemOut;

  this.GetItem = Menu_getItem;
  this.GetSubItem = Menu_getSubItem;
  this.GetSubType = Menu_getSubType;
  this.GetContent = Menu_getContent;
  this.GetFront = Menu_getFront;

  this.HideSubItem = Menu_hideSubItem;
  this.ShowSubItem = Menu_showSubItem;

  this.HideDropItem = Menu_hideDropItem;
  this.ShowDropItem = Menu_showDropItem;

  this.GetItemWidth = Menu_getItemWidth;
  this.GetItemHeight = Menu_getItemHeight;
  this.GetItemLeft = Menu_getItemLeft;

  this.ShowItem = Menu_showItem;

  this.Collapsed = Menu_collapsed;

  this.ItemOver(this.selectedMenuItemId);
}



