/// class ProspectDirector.Modules.Documents.DocumentTypeSelectionDialog
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Documents.DocumentTypeSelectionDialog = Class.extend({

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

    init: function (options) {

        this.options = $.extend({
            selector: null,
            data: {
                viewStateId: null
            }
        }, options);

        this.init_dialog();
        this.init_grid();

    },

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

    get_dialog: function () {
        return $(this.options.selector);
    },

    get_grid: function () {
        return this.get_dialog().find("[data-role=grid]");
    },

    get_searchBox: function () {
        return this.get_dialog().find("INPUT[data-role=search]");
    },

    get_okButton: function () {
        return this.get_dialog().find("INPUT[data-command=ok]");
    },

    get_cancelButton: function () {
        return this.get_dialog().find("INPUT[data-command=cancel]");
    },

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

    init_dialog: function () {

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

    },

    init_grid: function () {

        this.get_grid().data("viewGrid", new ProspectDirector.Modules.Views.ViewGrid({
            selector: this.get_grid().selector,
            viewStateId: this.options.data.viewStateId,
            selectionMode: "multiple",
            summary: false,
            search: this.get_searchBox(),
            rowDblClick: $.proxy(this.grid_onRowDblClick, this)
        }));

        this.get_grid().data("selection", new ProspectDirector.Types.Core.GridSelectionManager({
            grid: this.get_grid(),
            pk: "ObjectId",
            keys: ["ObjectId"]
        }));

        this.get_grid().disableTextSelection();

    },

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

    cancelButton_onClick: function (e) {

        this.get_dialog().data("dialog").hide();

    },

    okButton_onClick: function (e) {

        var selections = this.get_grid().data("selection").getSelectionsData();

        if (selections.length > 0) {
            this.returnResult(selections);
        }
        else {
            this.get_dialog().data("dialog").hide();
        }

    },

    grid_onRowDblClick: function (e) {

        var selections = this.get_grid().data("selection").getSelectionsData();

        if (selections.length > 0) {
            this.returnResult(selections);
        }

    },

    returnResult: function (selections) {

        var result = [];

        $(selections).each(function (i, e) {

            result[result.length] = {
                id: e.ObjectId,
                name: e.Name,
                category: e.Category
            };

        });

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

    }

});

ProspectDirector.Modules.Documents.DocumentTypeSelectionDialog.Dialog =
new (ProspectDirector.Types.Core.Dialog.extend({

    init: function () {

        this._super("DocumentTypeSelectionDialog",
                    "Select Document Type",
                    ProspectDirector.RUNTIME.resolveUrl("~/DocumentType/SelectionDialog"));

    }

}));