﻿function PopupMenu(listModel)
{
    this.listModel = listModel;
    this.onAction = new EventDelegate();
    
    extend(this, Widget);
};

PopupMenu.prototype = new Widget();

// public void setPadding({left:int, top:int, right:int, bottom:int} padding);
PopupMenu.prototype.setPadding = function(padding)
{
    this.padding = padding;
};

// public ... getPadding();
PopupMenu.prototype.getPadding = function()
{
    return (this.padding ? this.padding : {left:0, top:0, right:0, bottom:0});
};

// public String getWidgetName();
PopupMenu.prototype.getWidgetName = function()
{
    return "popupmenu";
};

// public ListModel getModel();
PopupMenu.prototype.getModel = function()
{
    return this.listModel;
};

// public {width:int, height:int} getPreferredSize();
PopupMenu.prototype.getPreferredSize = function()
{
    var w = 0, h = 0;
    for(var i = 0; i < this.listModel.getElementCount(); i++)
    {
        var element = this.listModel.getElementAt(i);
        var cell = this.getCellForElement(this, element, i, false, false, false);
        
        w = Math.max(cell.clientWidth, w);
        h += cell.clientHeight;
    }
    
    return {width: w, height:h};
};

// public void update();
PopupMenu.prototype.update = function()
{
    var padding = this.getPadding();
    var y = padding.top;
    for(var i = 0; i < this.listModel.getElementCount(); i++)
    {
        var cell = this.getCellForElement(this, this.listModel.getElementAt(i), i, false, false, true);
        
        with(cell.style)
        {
            height      = 17;
            left        = this.padding.left;
            top         = y;
            width       = this.getWidth() - this.padding.left - this.padding.right;
        }
        
        y += parseInt(cell.style.height);
    }

    this.domContainer.style.height = y + padding.bottom;
};

// public void popup(Widget parent, int x, int y);
PopupMenu.prototype.popup = function(parent, x, y)
{
    document.body.appendChild(this.domContainer);
    
    var size = this.getPreferredSize();
        
    this.setBounds(x, y, size.width + 10, size.height);
};

// public void hide();
PopupMenu.prototype.hide = function()
{
    if(this.domContainer.parentElement)
    {
        this.domContainer.parentElement.removeChild(this.domContainer);
    }
};

// public void click(int index);
PopupMenu.prototype.click = function(index)
{
    this.onAction.fireEvent(this, index);
};

// public Element getCellForElement(...);
PopupMenu.prototype.getCellForElement = function(menu, object, index, selected, editing, update)
{
    var cell = this.getCell(this, object, index, selected, editing, update);

    if(cell.parentNode && cell.parentNode != this)
    {
        cell.style.position    = "absolute";
        this.domContainer.appendChild(cell);
    }
    
    return cell;
};

// public Element getCellWidget(PopupMenu menu, Object object, int index, bool selected, bool editing, bool update);
PopupMenu.prototype.getCell = function(menu, object, index, selected, editing, update)
{
    throw new Error("Not implemented");
};

PopupMenu.prototype.toString = function()
{
	return "Widget/PopupMenu [listModel = " + this.listModel + "]";
}
