/**
The module for the administration View Publisher form.
@class ViewEditorDialog
@namespace ProspectDirector.Modules.Views
*/
ProspectDirector.Modules.Views.ViewEditorDialog = (function (options) {

    var 

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

    PRIVATE = {

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

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

            PRIVATE.init_dialog();
            PRIVATE.init_grid();
            PRIVATE.init_menu();
            PRIVATE.init_splitter();

            setTimeout(function () {

                PRIVATE.dialog_onResize();

            }, 100);

        },

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

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

        get_splitter: function () {
            return PRIVATE.get_dialog().find("[data-id=form-splitter][data-role=splitter]");
        },

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

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

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

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

        init_dialog: function () {

            var dialog = PRIVATE.get_dialog().data("kendoWindow");

            dialog.bind("resize", $.proxy(PRIVATE.dialog_onResize, PRIVATE));
            dialog.bind("close", $.proxy(PRIVATE.dialog_onClose, PRIVATE));
            PRIVATE.get_buttonOk().on("click", $.proxy(PRIVATE.buttonOk_onClick, PRIVATE));

        },

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

            PRIVATE.get_grid().data("viewGrid", new ProspectDirector.Modules.Views.ViewGrid({
                selector: PRIVATE.get_grid().selector,
                viewStateId: options.viewStateId,
                height: "100%",
                summary: false,
                search: false
            }));

            PRIVATE.get_grid().data("filler", new ProspectDirector.Types.KendoExtensions.GridFill({
                selector: PRIVATE.get_grid().selector
            }));

        },

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

            PRIVATE.get_menu().data("menu", new ProspectDirector.Types.Core.Menu({
                selector: PRIVATE.get_menu().selector
            }));

            PRIVATE.get_menu().data("menu").addPlugIn(ProspectDirector.Types.View.Menu, {
                viewStateId: options.viewStateId,
                viewGrid: PRIVATE.get_grid().data("viewGrid")
            });

        },

        init_splitter: function () {

            new ProspectDirector.Types.Core.ViewportSplitter({
                selector: PRIVATE.get_splitter().selector
            });

            PRIVATE.get_splitter().data("kendoSplitter").bind("resize", $.proxy(PRIVATE.splitter_onResize, PRIVATE));

            setTimeout(function () {

                if (PRIVATE.get_splitter().length > 0) {
                    PRIVATE.get_splitter().data("kendoSplitter").resize();
                }

            }, 100);

        },

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

        splitter_onResize: function () {

            if (PRIVATE.get_grid().length > 0) {

                setTimeout(function () {
                    if (PRIVATE.get_grid().data("filler")) {
                        PRIVATE.get_grid().data("filler").size();
                    }
                }, 50);

            }
            else {
                PRIVATE.get_splitter().data("kendoSplitter").unbind("resize", this);
            }

        },

        dialog_onResize: function (e) {

            var content = PRIVATE.get_dialog(),
                inner = PRIVATE.get_dialog().find(".app-dlg");

            inner.height(content.height() - 52);

            PRIVATE.get_splitter().height(inner.height());
            PRIVATE.get_splitter().width(inner.width() - 22);
            PRIVATE.get_splitter().css({
                margin: "10px"
            });

            PRIVATE.get_splitter().data("kendoSplitter").resize();

        },

        dialog_onClose: function () {

            var splitter = null;

            if (PRIVATE.get_splitter().length > 0) {

                splitter = PRIVATE.get_splitter().data("kendoSplitter");

                if (splitter) {
                    splitter.unbind("resize", PRIVATE.dialog_onResize);
                    splitter.destroy();
                }

            }

        },

        buttonOk_onClick: function (e) {

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

        }

    },

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

    PUBLIC = {
    };

    PRIVATE.init(options);

    return PUBLIC;

});