/// class PD.Web.UI.CheckBoxDialog
/// using PD.Web.UI.MessageDialog

/**
@class CheckBoxDialog
@constructor
@namespace Web.UI
@extends Web.UI.Dialog
**/
PD.Web.UI.CheckBoxDialog =
PD.Web.UI.MessageDialog.extend({

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

    _checks: null,

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

    get_checks: function () {
        return this._checks;
    },

    get_radiosNode: function () {
        return this.get_node().find(".-pd-msg-dialog-checks");
    },

    //================================
    // EVENT HANDLERS
    //================================

    onInit: function (e) {
        this._super(e);
        this._checks = new PD.BCL.List(this.get_options().checks);
    },

    fireButtonCallback: function (callback) {

        var checked = this.get_radiosNode().find("input[name=option]:checked"),
            values = new PD.BCL.List();

        checked.each(function (index, check) {
            values.add($(check).val());
        });

        callback({
            sender: this,
            values: values,
            close: $.proxy(this.close, this)
        });

    },

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

    _setText: function () {

        var hasOptionsTag = this.get_message().indexOf("{{options}}") > 0,
            markup = this._getChecksMarkup();

        if (hasOptionsTag) {
            this.get_textNode().append(this.get_message().replace("{{options}}", markup));
        }
        else {
            this.get_textNode().append(this.get_message());
            this.get_textNode().append(markup);
        }

    },

    _getChecksMarkup: function () {

        var result = "",
            template = [
                "<label class='-pd-msg-dialog-check'>",
                    "<input type='checkbox' name='option'/>",
                    "<div></div>",
                "</label><br/>",
            ];

        this.get_checks().each(function (index, check) {
            var $template = $(template.join(""));
            $template.find("DIV").html(check.label);
            $template.find("INPUT").attr("value", check.value);
            if (index === 0) {
                $template.find("INPUT").attr("checked", "checked");
            }
            result += $template[0].outerHTML;
        });

        return "<div class='-pd-msg-dialog-checks is-fixed'>" + result + "</div>";

    }

});

//================================
// STATIC METHODS
//================================

PD.Web.UI.CheckBoxDialog.show = function (options) {
    return new PD.Web.UI.RadioDialog(options);
};

PD.Web.UI.CheckBoxDialog.warn = function (options) {
    return PD.Web.UI.MessageDialog.warn(options, PD.Web.UI.CheckBoxDialog);
};

PD.Web.UI.CheckBoxDialog.info = function (options) {
    return PD.Web.UI.MessageDialog.info(options, PD.Web.UI.CheckBoxDialog);
};

PD.Web.UI.CheckBoxDialog.error = function (options) {
    return PD.Web.UI.MessageDialog.error(options, PD.Web.UI.CheckBoxDialog);
};

PD.Web.UI.CheckBoxDialog.success = function (options) {
    return PD.Web.UI.MessageDialog.success(options, PD.Web.UI.CheckBoxDialog);
};