/**
The module for the New Column Dialog.
@class FolderCreateDialogDesignableList
@namespace ProspectDirector.Modules.ExplorerModel.Folders
*/
ProspectDirector.Modules.ExplorerModel.Folders.FolderCreateDialogDesignableList = (function (options) {

    var 

    //---------------------------
    //--- PRIVATE MEMBERS
    //---------------------------

    PRIVATE = {

        init: function (options) {

            PRIVATE.options = $.extend({
                selector: null,
                lists: null
            }, options);

            PRIVATE.init_dialog();
            PRIVATE.init_grid();

        },

        //---------------------------
        //--- PRIVATE PROPERTIES
        //---------------------------

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

        get_form: function () {
            return PRIVATE.get_dialog().find("FORM");
        },

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

        get_selectedExtension: function () {
            return PRIVATE.get_dialog().find("input[type=hidden][data-role=selected-extension]");
        },

        get_createButton: function () {
            return PRIVATE.get_dialog().find("[data-command=create]");
        },

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

        //---------------------------
        //--- PRIVATE INTIALIZERS
        //---------------------------

        init_dialog: function () {

            PRIVATE.get_cancelButton().on("click", $.proxy(PRIVATE.cancelButton_onClick, PRIVATE));
            PRIVATE.get_createButton().on("click", $.proxy(PRIVATE.createButton_onClick, PRIVATE));

        },

        init_grid: function () {

            PRIVATE.get_grid().kendoGrid({
                height: "325px",
                columns: [
                        { field: "name", title: "Name", width: 200 },
                        { field: "description", title: "Description" }
                    ],
                dataSource: {
                    data: PRIVATE.options.lists,
                    schema: {
                        id: "id"
                    }
                },
                sortable: true,
                selectable: "single row",
                change: $.proxy(function (e) {

                    var grid = PRIVATE.get_grid().data("kendoGrid");

                    PRIVATE.get_selectedExtension().val(grid.dataItem(grid.select()[0]).id);

                }, PRIVATE),
                dataBound: $.proxy(this.grid_onDataBound, this)
            });

        },

        //---------------------------
        //--- EVENT HANDLERS
        //---------------------------

        cancelButton_onClick: function () {
            PRIVATE.get_dialog().data("dialog").hide();
        },

        createButton_onClick: function () {

            if (PRIVATE.get_selectedExtension().val() === "") {
                alert("An extension must be selected to complete folder creation.");
                return;
            }

            ProspectDirector.Types.Core.Notifications.action("Create Command Sent");

            PRIVATE.get_createButton().disable();

            PD.ajax({

                type: PRIVATE.get_form().attr('method'),

                url: PRIVATE.get_form().attr('action'),

                data: PRIVATE.get_form().serialize(),

                contextData: {
                    container: PRIVATE.get_form(),
                    control: PRIVATE.get_createButton()
                },

                success: PRIVATE.create_onSuccess

            });

        },

        create_onSuccess: function (a, b, c) {

            ProspectDirector.Types.Core.Notifications.success("Explorer Folder Created");

            PRIVATE.get_dialog().data("dialog").hide({
                command: "ok",
                data: a
            });

        },

        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 list extensions have been created.");
            }
            else {
                ProspectDirector.Types.KendoExtensions.Grid.removeMessage(this.get_grid());
            }

        }

    },

    //---------------------------
    //--- PUBLIC MEMBERS
    //---------------------------

    PUBLIC = {

    };

    PRIVATE.init(options);

    return PUBLIC;

});
