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

    var 

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

    PRIVATE = {

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

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

            PRIVATE.init_dialog();
            PRIVATE.init_categories();

            PRIVATE.registerEventHandlers();

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

        },

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

        /**
        @property get_dialog
        @private
        */
        get_dialog: function () {
            //TODO: convert this to selector only
            return $("#" + options.dialogId);
        },

        /**
        @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]");
        },

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

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

            ProspectDirector.Types.Core.Notifications.success("Save Command Successful");

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

        },

        /**
        @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: PRIVATE.get_form().attr('method'),

                url: PRIVATE.get_form().attr('action') + "?" + $.param({
                    viewStateId: options.viewStateId
                }),

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

                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 init_dialog
        @private
        **/
        init_dialog: function () {

            var that = this;

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

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

            });

        },

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

            PRIVATE.get_dialog().find("[data-role=category]").kendoComboBox({
                dataSource: options.categories,
                filter: "contains",
                suggest: true
            });

        },

        /**
        @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)
            );

        }

    },

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

    PUBLIC = {
    };

    PRIVATE.init();

    return PUBLIC;

});