/**
The module for the View Categorize As Dialog.
@class ViewCategorizeDialog
@namespace ProspectDirector.Modules.Views
*/
ProspectDirector.Modules.Views.ViewCategorizeDialog = (function (options) {

    var 

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

    PRIVATE = {

        /**
        @method init
        @private
        **/
        init: function () {

            options = $.extend({
                selector: null,
                categories: [],
                viewStateId: null
            }, options);

            PRIVATE.registerEventHandlers();

            PRIVATE.get_dialog().find("[data-focus=true]")[0].select();

        },

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

        /**
        @property get_dialog
        @private
        */
        get_dialog: function () {
            return $(options.selector);
        },

        /**
        @property get_form
        @private
        */
        get_form: function () {
            return PRIVATE.get_dialog().find("form");
        },

        /**
        @property get_cancelButton
        @private
        */
        get_cancelButton: function () {
            return PRIVATE.get_dialog().find("input[data-command=cancel]");
        },

        /**
        @property get_saveButton
        @private
        */
        get_saveButton: function () {
            return PRIVATE.get_dialog().find("input[data-command=save]");
        },


        get_category: function () {
            return PRIVATE.get_dialog().find("input[name=category]");
        },

        //---------------------------
        //--- PRIVATE EVENT HANLDERS
        //---------------------------

        /**
        @method submit_onSuccess
        @private
        **/
        submit_onSuccess: function (a, b, c) {

            ProspectDirector.Types.Core.Notifications.success("Save Command Successful");
            this.get_dialog().data("dialog").hide({
                command: "ok"
            });

        },

        /**
        @method cancelButton_onClick
        @private
        **/
        cancelButton_onClick: function (e) {

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

        },

        /**
        @method saveButton_onClick
        @private
        **/
        saveButton_onClick: function (e) {

            PRIVATE.get_saveButton().disable();

            PD.ajax({

                type: "POST",

                url: ProspectDirector.RUNTIME.resolveUrl("~/view/categorize"),

                data: {
                    category: this.get_category().val(),
                    viewIds: JSON.stringify(PRIVATE.get_dialog().data("dialog").params.selections)
                },

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

                success: $.proxy(PRIVATE.submit_onSuccess, this)

            });

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

        },

        //---------------------------
        //--- PRIVATE METHODS
        //---------------------------

        /**
        @method registerEventHandlers
        @private
        **/
        registerEventHandlers: function () {

            PRIVATE.get_saveButton().on("click",
                $.proxy(PRIVATE.saveButton_onClick, this)
            );

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

            PRIVATE.get_dialog().find("input[type=text]").on("keypress", $.proxy(function (e) {
                if (e.charCode === 13) {
                    PRIVATE.saveButton_onClick(e);
                }
            }, this));

        }

    },

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

    PUBLIC = {
    };

    PRIVATE.init();

    return PUBLIC;

});