function BenditUnfoldMenu (id)
{
  this.id = id
  var manager = this;
  var seenFoldOpen = false;
  $('#' + this.id + ' span.category').each(function ()
  {
    this.manager = manager;
    this.id = 'id-' + Math.floor(Math.random() * 100000);
    this.parentId = this.parentNode.getAttribute('id');
    if ($(this.parentNode).is('.path'))
    {
      this.foldIsOpen = true;
      seenFoldOpen = true;
    }
    else
    {
      this.foldIsOpen = false;
    }

    $(this).click(function ()
    {
      this.manager.update(this);
    })
  })
  
  if (!seenFoldOpen)
  {
    var cookie = $.cookie('bendit-menu-open');
    if (cookie)
    {
      $('#' + cookie + ' > span.category').each (function () {
        this.foldIsOpen = true;
      })
    }
  }
  this.update();
}
BenditUnfoldMenu.prototype.update = function (span)
{
  $('#' + this.id + ' span.category').each(function ()
  {
    if (span && (span.id == this.id) && !span.foldIsOpen)
    {
      this.foldIsOpen = true;
      $('#' + this.parentId + ' > div').slideDown(250);
      $.cookie('bendit-menu-open', this.parentId);
    }
    else if (span && (span.id == this.id) && span.foldIsOpen)
    {
      this.foldIsOpen = false;
      $('#' + this.parentId + ' > div').slideUp(250);
    }
    else if (span && this.foldIsOpen)
    {
      this.foldIsOpen = false;
      $('#' + this.parentId + ' > div').slideUp(250);
    }
    else if (!this.foldIsOpen)
    {
      this.foldIsOpen = false;
      $('#' + this.parentId + ' > div').hide();
    }
    else
    {
      this.foldIsOpen = true;
      $('#' + this.parentId + ' > div').show();
    }
  })
}

