/// class ProspectDirector.Web.UI.Controls.ToolBarSplitButton
/// using ProspectDirector.Web.UI.Controls.Control

/**
@class ToolBarSplitButton
@namespace Web.UI.Controls
@constructor
**/
ProspectDirector.Web.UI.Controls.ToolBarSplitButton = ProspectDirector.Web.UI.Controls.Control.extend({

    //===================================
    // FIELDS
    //===================================

    _items: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {

        var content = null;

        this._super(options);

        content =
        "<div class=\"btn-group\">" +
            "<button class=\"btn btn-default\">" + this.get_text() + "</button>" +
            "<button class=\"btn btn-default dropdown-toggle\" data-toggle=\"dropdown\">" +
                "<span class=\"caret\"></span>" +
                "<span class=\"sr-only\">Toggle Dropdown</span>" +
            "</button>" +
            "<ul class=\"dropdown-menu\" role=\"menu\">" +
            "</ul>" +
        "</div>";

        this.setDomNode(content);

        if (this.get_command() !== undefined && this.get_command() !== null) {
            if ($.isFunction(this.get_command().execute)) {
                this.get_buttonDomNode().on("click", $.proxy(this.get_command().execute, this.get_command()));
            }
        }

        this._items = new ProspectDirector.Web.List(options.items);

    },

    //===================================
    // PROPERTIES
    //===================================

    get_items: function () {
        return this._items;
    },

    get_text: function () {
        return this._options.text;
    },

    get_disabled: function () {
        return this._options.disabled;
    },

    set_text: function () {
        this._options.text = value;
    },

    get_command: function () {
        return this._options.command;
    },

    get_buttonDomNode: function () {
        return this.get_domNode().children("BUTTON:eq(0)");
    },

    get_caretDomNode: function () {
        return this.get_domNode().children("BUTTON:eq(1)");
    },

    get_menuDomNode: function () {
        return this.get_domNode().children("UL");
    },

    //===================================
    // PUBLIC METHODS
    //===================================

    render: function (content) {

        var that = this;

        if (this.get_disabled()) {
            this.get_buttonDomNode().attr("disabled", "disabled");
        }
        else {
            this.get_buttonDomNode().removeAttr("disabled");
        }

        this.get_items().each(function (index, data) {

            var item = new ProspectDirector.Web.UI.Controls.ToolBarItem(data);
            that.get_menuDomNode().append(item.render());

        });

        if (this._isCaretDisabled()) {
            this.get_caretDomNode().attr("disabled", "disabled");
        }
        else {
            this.get_caretDomNode().removeAttr("disabled");
        }

        return this._super(content);

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _isCaretDisabled: function () {
        return this.get_menuDomNode().children("LI[disabled]").length === this.get_items().count();
    }

});