
function
DynamicMenu(container, data)
{
  this.div = pbu.$("<div class='dynamicmenu'><ul class='items'></ul><div class='more'>Mer &raquo;</div></div>")[0];
  this.morediv = pbu.$(".more", this.div)[0];
  this.dropdown = pbu.$("<div class='dynamicmenu' style='position:absolute;z-index:99;display:none;'><ul class='drop'></ul></div>")[0];
  this.ul = pbu.$(".items", this.div)[0];
  this.menuitems = [];
  this.dropvisible = false;
  var li;
  for (var i = 0; i < data.items.length; i++) {
    this.menuitems[i] = pbu.$("<li><a href=''></a></li>")[0];
    pbu.$("a", this.menuitems[i])[0].href=data.items[i].url;
    pbu.$("a", this.menuitems[i]).text(data.items[i].name);
  }
  container.appendChild(this.div);
  pixboxGlobal.headermenu = this; // XXX: global reference so the resizer function can call us
  var menu = this;
  pbu.$(document).ready(function() {
    document.body.appendChild(menu.dropdown);
  });

  pbu.$(this.morediv).bind("click", function() {
    if (menu.dropvisible)
      menu.hideMore();
    else
      menu.showMore();
  });
  pbu.$(this.morediv).bind("mouseover", function() {
    menu.showMore();
  });
/*
  pbu.$().mousemove(function(e) {
    menu.mouseMoveHandler(e);

  });
*/
}

DynamicMenu.prototype.mouseMoveHandler = function(e) {
//    pbu.$("#debug").val(e.pageX + " " + e.pageY + " " + menu.drop_pos.left + " " + menu.drop_pos.top);
}

DynamicMenu.prototype.showMore = function() {
  var menu = this;
  menu.dropvisible = true;
  menu.dropdown.style.display = "block";
  var p = pbu.getAbsolutePosition(menu.morediv);
  pbu.$(menu.dropdown).css({"top":(p.top+30)+"px","left":p.left+"px","display":"block"});
  menu.drop_pos = pbu.getAbsolutePosition(menu.dropdown);
}

DynamicMenu.prototype.hideMore = function() {
  var menu = this;
  menu.dropvisible = false;
  menu.dropdown.style.display = "none";

}


DynamicMenu.prototype.redraw = function(newsize) {
  var tot = 0;
  var num_in_dropdown = 0;
  this.dropdown.style.display = "none";
  this.div.style.width = newsize+"px";
  var uldrop = pbu.$("ul", this.dropdown)[0];
  var max = this.div.offsetWidth - this.morediv.offsetWidth - 5; // XXX 5?
  for (var i=0; i< this.menuitems.length; i++) {
    if (tot >= max) {
      num_in_dropdown++;
      uldrop.appendChild(this.menuitems[i]);
    }
    else {
      this.ul.appendChild(this.menuitems[i]);
      tot += this.menuitems[i].offsetWidth;
      if (tot >= max) {
        num_in_dropdown++;
        uldrop.appendChild(this.menuitems[i]);
      }
    }
  }
  if (num_in_dropdown > 0)
    this.morediv.style.display = "block";
  else
    this.morediv.style.display = "none";
}


DynamicMenu.prototype.applyUpdate = function(json) {
}
