/// class ProspectDirector.Modules.Views.ViewDesignColumnsDialog
/// using ProspectDirector.Modules.Views.ViewColumnConfiguration
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Views.ViewDesignColumnsDialog = ProspectDirector.Modules.Views.ViewColumnConfiguration.extend({

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

    _dialog: null,
    _okButton: null,
    _cancelButton: null,

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

    init: function (options) {

        this._super(options);

        this._dialog = $(this.options.selector);
        this._okButton = this._dialog.find("INPUT[type=button][data-command=ok]");
        this._cancelButton = this._dialog.find("INPUT[type=button][data-command=cancel]");

        this.init_dialog();

    },

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

    get_dialog: function () {
        return this._dialog;
    },

    get_okButton: function () {
        return this._okButton;
    },

    get_cancelButton: function () {
        return this._cancelButton;
    },

    //===========================
    // INITIALIZERS
    //===========================

    init_dialog: function () {

        var that = this,
            win = this.get_dialog().data("kendoWindow");

        win.bind("resize", $.proxy(this.dialog_onResize, this));

        this.get_okButton().on("click", $.proxy(this.okButton_onClick, this));
        this.get_cancelButton().on("click", $.proxy(this.cancelButton_onClick, this));

        that.dialog_onResize();

    },

    init_grid: function () {

        this._super();

        this.get_grid().data("filler", new ProspectDirector.Types.KendoExtensions.GridFill({
            selector: this.get_grid().selector,
            useAvailableSpace: true
        }));

    },

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

    okButton_onClick: function () {

        this.get_okButton().disable();

        PD.ajax({
            type: "POST",
            url: ProspectDirector.RUNTIME.resolveUrl("~/ViewDesign/ColumnsDialog"),
            data: {
                viewStateId: this.options.data.viewStateId,
                data: JSON.stringify(this.getData())
            },
            success: $.proxy(this.submit_onSuccess, this)
        });

    },

    cancelButton_onClick: function () {
        this.get_dialog().data("dialog").hide({
            command: "cancel"
        });
    },

    submit_onSuccess: function () {

        this.get_dialog().data("dialog").hide({
            command: "ok"
        });

    },

    dialog_onResize: function (e) {

        var that = this,
            content = this.get_dialog(),
            inner = this.get_dialog().find(".app-dlg");

        inner.height(content.height() - 42);
        inner.width(content.width() - 20);

        that.get_grid().data("filler").size();

    },

    grid_onDataBound: function (e) {
        
        var grid = this.get_grid().data("kendoGrid");

        if (grid.dataSource.data().length === 0) {
            ProspectDirector.Types.KendoExtensions.Grid.addMessage(this.get_grid(), "No columns have been added to the design.");
        }
        else {
            ProspectDirector.Types.KendoExtensions.Grid.removeMessage(this.get_grid());
        }

    },

    //===========================
    // OVERRIDDEN METHODS
    //===========================

    showFinderDialog: function () {

        ProspectDirector.Modules.Views.ViewColumnConfiguration.DesignColumnFinder.show({
            params: {
                viewStateId: this.options.data.viewStateId
            },
            callbacks: {
                ok: $.proxy(function (sender, e) {
                    this.finderDialog_onOk(e);
                }, this)
            }
        });

    }

});

//===========================
// STATIC PROPERTIES
//===========================

ProspectDirector.Modules.Views.ViewDesignColumnsDialog.Dialog =
new (ProspectDirector.Types.Core.Dialog.extend({

    init: function () {

        this._super("ViewDesignColumns",
                    "View Design Columns",
                    ProspectDirector.RUNTIME.resolveUrl("~/ViewDesign/ColumnsDialog"), {
                        config: {
                            resizable: true,
                            height:400,
                            width: 600
                        }
                    });

    }

}));