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

    var 

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

    PRIVATE = {

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

            this.init_grid();
            this.init_menu();

        },

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

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

        get_grid: function () {
            return $("[data-id=viewgrid][data-role=grid]");
        },

        get_menu: function () {
            return $("[data-id=form-menu][data-role=menu]");
        },
        
        //---------------------------
        //--- EVENT HANDLERS
        //---------------------------

        grid_onRowDblClick: function () {

            var selections = this.get_grid().data("selection").getSelectionsData(),
                data = selections[0];

            if (!data) {
                return;
            }

            if (data.ObjectType === "sys.file") {
                PRIVATE.openFile(data);
            }
            else {
                PRIVATE.openEntity(data);
            }

        },

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

        /**
        @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%",
                selectionMode: "multiple",
                search: true,
                summary: true,
                rowDblClick: $.proxy(PRIVATE.grid_onRowDblClick, PRIVATE)
            }));

            PRIVATE.get_grid().data("rowContextMenu", new ProspectDirector.Modules.Views.EntityViewContextMenu({
                module: PRIVATE
            }));

            PRIVATE.get_grid().disableTextSelection();

            new ProspectDirector.Types.Core.ViewportSplitter({
                selector: "[data-role=splitter]"
            });

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

            PRIVATE.get_splitter().data("kendoSplitter").bind("resize", function () {
                setTimeout(function () {
                    PRIVATE.get_grid().data("filler").size();
                }, 20);
            });

        },

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

        },

        openEntity: function (data) {
            
            if (!data) {
                return;
            }

            if (ProspectDirector.RUNTIME.ENVIRONMENT.GLOBAL_DOUBLE_CLICK_BEHAVIOR === 1) {
                window.open(ProspectDirector.RUNTIME.resolveUrl("~/explore/") + data.ObjectId);
            }
            else {
                window.location = ProspectDirector.RUNTIME.resolveUrl("~/explore/") + data.ObjectId; 
            }

        },

        openFile: function (data) {
        
            if (!data) {
                return;
            }
            
            ProspectDirector.RUNTIME.iframeRequest("~/file/download/" + data.ObjectId);

        }

    },

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

    PUBLIC = {
    };

    PRIVATE.init();

    return PUBLIC;

});