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

    var 

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

    PRIVATE = {

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

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

            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");

            window.location = ProspectDirector.RUNTIME.resolveUrl("~/ViewBrowser/Index?viewId=" + a.viewId);
            
        },

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

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

        }

    },

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

    PUBLIC = {
    };

    PRIVATE.init();

    return PUBLIC;

});