﻿function ComboBox(comboBoxModel)
{
    if(!comboBoxModel)
    {
        throw new Error("ComboBox.ComboBox() - parameter 'comboBoxModel' cannot be null");
    } else
    {
        this.comboBoxModel  = comboBoxModel;
        this.popupMenu      = new PopupMenu(comboBoxModel);
        this.selectedIndex  = -1;
        
        this.onSelectedIndexChanged = new EventDelegate();

        this.popupMenu.onAction.addEventListener(getMethodPointer(this, function(menu, index) {
            this.onSelectedIndexChanged.fireEvent(this, index);
        }));

        extend(this, Widget);
    }
};

ComboBox.prototype = new Widget;

// public String getWidgetName();
ComboBox.prototype.getWidgetName = function()
{
    return "combobox";
};

ComboBox.prototype.getModel = function()
{
    return this.comboBoxModel;
};

// public void setMenuPadding({left:int, top:int, right:int} padding);
ComboBox.prototype.setMenuPadding = function(menuPadding)
{
    this.menuPadding = menuPadding;
};

// public {left:int, top:int, right:int} getMenuPadding();
ComboBox.prototype.getMenuPadding = function()
{
    return (this.menuPadding ? this.menuPadding : { left:0, top:0, right:0 });
};

// public PopupMenu getPopupMenu();
ComboBox.prototype.getPopupMenu = function()
{
    return this.popupMenu;
};

// public ComboBoxModel getComboBoxModel();
ComboBox.prototype.getComboBoxModel = function()
{
    return this.comboBoxModel;
};

// public void setSelectedIndex(int index);
ComboBox.prototype.setSelectedIndex = function(index)
{
    this.selectedIndex = index;
    this.comboBoxModel.setText(this.comboBoxModel.getElementAt(index).toString());
};

// public void setExpanded(boolean expanded);
ComboBox.prototype.setExpanded = function(expanded)
{
    if(expanded)
    {
        var menuPadding = this.getMenuPadding();
        
        // this.popupMenu.setBounds(this.getX() + menuPadding.left, this.getY() + this.getHeight() + menuPadding.top, this.getWidth() + menuPadding.right, 100);
        // this.popupMenu.setBounds(this.getX() + menuPadding.left, this.getY() + this.getHeight() + menuPadding.top, this.getWidth() + menuPadding.right, 100);
        // this.container.add(this.popupMenu);
        // this.popupMenu.popup(this, this.getX() + menuPadding.left, this.getY() + this.getHeight() + menuPadding.top);
        this.popupMenu.popup(this, event.x, event.y);
        // this.popupMenu.update();
    } else
    {
        // this.container.remove(this.popupMenu);
        this.popupMenu.hide();
    }
    
    this.expanded = expanded;
};

// public boolean isExpanded();
ComboBox.prototype.isExpanded = function()
{
    return (this.expanded ? true : false);
};

ComboBox.prototype.toString = function()
{
	return "Widget/ComboBox";
}
