/// class ProspectDirector.Modules.Processes.ProcessCreateDialog
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Processes.ProcessCreateDialog = (function (options) {

    var 

        PRIVATE = {

            init: function (options) {

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

                PRIVATE.init_dialog();

            },

            //---------------------------
            //--- PROPERTIES
            //---------------------------

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

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

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

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

            get_icon: function () {
                return PRIVATE.get_dialog().find("INPUT[data-id=icon]");
            },

            get_name: function () {
                return PRIVATE.get_dialog().find("INPUT[data-id=name]");
            },

            get_type: function () {
                return PRIVATE.get_dialog().find("SELECT[data-id=entity-type]");
            },

            //---------------------------
            //--- INITIALIZERS
            //---------------------------

            init_dialog: function () {

                var that = this;

                this.get_cancelButton().on("click", $.proxy(this.cancelButton_onClick, this));
                this.get_createButton().on("click", $.proxy(this.createButton_onClick, this));
                this.get_icon().iconBrowserBox();
                this.get_type().focus();

                this.get_dialog().on("keypress", function (e) {

                    if (e.keyCode === 13 && e.target.tagName !== "TEXTAREA") {
                        e.preventDefault();
                        that.createButton_onClick();
                    }

                });

            },

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

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

            createButton_onClick: function () {

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

                this.get_createButton().disable();

                PD.ajax({

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

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

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

                    success: $.proxy(this.create_onSuccess, this),

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

                });

            },

            create_onSuccess: function (a, b, c) {

                ProspectDirector.Types.Core.Notifications.success("Create Successful");

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

            }

        },

        PUBLIC = {

        };

    PRIVATE.init(options);

    return PUBLIC;

});

ProspectDirector.Modules.Processes.ProcessCreateDialog.Dialog =
new (ProspectDirector.Types.Core.Dialog.extend({

    init: function () {

        this._super("ProcessCreateDialog",
                "New Process",
                ProspectDirector.RUNTIME.resolveUrl("~/process/new"));

    }

}));