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

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

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

    _radios: null,
    
    //================================
    // PROPERTIES
    //================================

    get_radios: function () {
        return this._radios;
    },

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

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

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

    fireButtonCallback: function (callback) {
        
        var value = this.get_radiosNode().find("input[name=option]:checked").val();
        
        callback({
            sender: this,
            value: value,
            close: $.proxy(this.close, this)
        });

    },

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

    _setText: function () {

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

        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);
        }

    },

    _getRadiosMarkup: function () {

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

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

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

    }

});

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

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

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

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

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

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