var menu = {
  timeout: 500,
  closeTimer: null,
  menuItem: null,

  cancelCloseTimer: function()
  {
    if (this.closeTimer != null)
    {
      window.clearTimeout(this.closeTimer);
      this.closeTimer = null;
    }
  },

  show: function(element, callback)
  {
    $(element).slideDown('normal', callback);
  },

  hide: function(element, callback)
  {
    var element = $(element),
        height = element.css('height');

    element.animate({
      height: 0,
      opacity: 0
    }, 'slow', 'swing', function()
    {
        element.hide().css({
          height: height,
          opacity: 1
        });
        callback();
    });
  },

  close: function()
  {
    if (menu.menuItem != null)
    {
      menu.hide(menu.menuItem, function()
      {
        menu.menuItem = null;
      });
    }
  },

  open: function(element)
  {
    this.cancelCloseTimer();
    this.close();

    this.menuItem = element;
    this.show(this.menuItem);
  },

  startCloseTimer: function()
  {
    this.closeTimer = window.setTimeout(this.close, this.timeout);
  }
};
