$(document).ready(function () {
    var outerLayout;
    var topLayout;
    var eastMinSize = isMobileMode && isMobileMode == 1 ? 0 : 370;

    var layoutSettings_Outer = {
        fxName: "none",
        spacing_open: 4,
        spacing_closed: 0,
        togglerLength_open: 0,
        togglerLength_closed: 0,
        maskIframesOnResize: false,
        name: "outerLayout",
        defaults: { contentSelector: ".content" },
        north: {
            size: (getIsMenuWide() ? 70 : 40),
            spacing_open: 0,
            spacing_closed: 0,
            togglerLength_open: 0,
            togglerLength_closed: -1,
            resizable: false,
            slidable: false
        },
        west: {
            size: oLSize,
            minSize: 150,
            spacing_closed: 20,
            togglerLength_closed: -1,
            togglerLength_open: 0,
            slidable: false,
            onclose: function () {
                $("#btn-tree-open").show();
                if (typeof isMobileMode == "undefined" || isMobileMode != 1) {
                    $.ajax({
                        url: "layoutcookies.php?page=businessmodel&west_hide=1",
                        success: function (response) {
                            var r = response; r = null; 
                        }
                   });
                }
            },
            onopen: function () {
                $("#btn-tree-open").hide();
                if (typeof isMobileMode == "undefined" || isMobileMode != 1) {
                    $.ajax({
                        url: "layoutcookies.php?page=businessmodel&west_hide=0",
                        success: function (response) {
                            var r = response; r = null; 
                        }
                    });
                }
            },
            onresize: function () {
                //58753
                if (typeof goguid !== "undefined"){
                    $('div#box-tree-int').scrollTo($('li[name="' + goguid + '"]').parent().parent());
                }
                outerLayout = $("#main").layout(layoutSettings_Outer);
                $( window ).resize();
                $.ajax({
                    url: "layoutcookies.php?page=businessmodel&west=" + outerLayout.state.west.size,
                    success: function (response) {
                        var r = response; r = null; 
                    }
                });
            }
        },
        east: {
            size: oRSize,
            minSize: eastMinSize,
            resizable: (isMobileMode && isMobileMode == 1 ? false : true),
            closable: (isMobileMode && isMobileMode == 1 ? false : true),
            slidable: (isMobileMode && isMobileMode == 1 ? true : false),
            initHidden : ((isMobileMode && isMobileMode == 1) ? (mobileViewReport ? true : false) : false) || (eastHide && eastHide == 1 && !isInitStartPage()),
            onresize: function () {
                $('iframe#iframe-sub-doc').height($('#box-layout-east-container').height());
                $('iframe#iframe-sub-doc').width($('#box-layout-east-container').width()); 
                outerLayout = $("#main").layout(layoutSettings_Outer);
                $( window ).resize();
                if (typeof isMobileMode == "undefined" || isMobileMode != 1) {
                    $.ajax({
                        url: "layoutcookies.php?page=businessmodel&east=" + outerLayout.state.east.size,
                        success: function (response) {
                            var r = response; r = null; 
                        }
                    });
                }
            },
            onclose_start: function () {
                existUnsavedData = (existUnsavedData ? !confirm(gConfirmsavelesValue) : existUnsavedData);
                return !existUnsavedData;
            },
            onclose: function () {
                $("#btn-discussion-open").show();
                $('#box-frame').removeClass('box-frame-wide').addClass('box-frame-padding');	
                $('#box-frame-header-right').addClass('box-frame-header-right-wide');	
                $('#btn-discussion-open').removeClass('btn-vertical-right-select').removeClass('btn-vertical-right-unselect').addClass('btn-vertical-right');
                $('#container').layout().resizeAll();
                if (typeof isMobileMode === "undefined" || isMobileMode != 1) {
                    $.ajax({
                        url: "layoutcookies.php?page=businessmodel&east_hide=1",
                        success: function (response) {
                            var r = response; r = null; 
                        }
                    });
                }
            },
            onopen: function () {
                $("#btn-discussion-open").hide();
                $('#box-frame').removeClass('box-frame-padding').addClass('box-frame-wide');	
                $('#box-frame-header-right').removeClass('box-frame-header-right-wide');	
                $('#container').layout().resizeAll();
                if (typeof isMobileMode === "undefined" || isMobileMode != 1) {
                    $.ajax({
                        url: "layoutcookies.php?page=businessmodel&east_hide=0",
                        success: function (response) {
                            var r = response; r = null; 
                        }
                    });
                }
            }
        },        
       center: {
            minWidth: 450,
            onresize: "innerLayout.resizeAll"
        }
    };
    
    var layoutSettings_Top = {
        fxName: "none",
        spacing_open: 0,
        spacing_closed: 0,
        togglerLength_open: 0,
        togglerLength_closed: 0,
        maskIframesOnResize: false,
        name: "topLayout",
        defaults: {
        },
        north: {
            size: ($.cookie('hideheader') == 1 ? 0 : 74),
            spacing_open: 2,
            spacing_closed: 2,
            togglerLength_open: 0,
            togglerLength_closed: 0,
            resizable: false,
            slidable: false,
            onclose: function () {
                $("#btn-toggle-header").addClass("closed");
                $("#btn-toggle-header").blur();
                $.ajax({
                    url: "hideheader.php?hheader=1",
                    success: function (response) {
                    }
                });
            },
            onopen: function () {
                $("#btn-toggle-header").removeClass("closed");
                $("#btn-toggle-header").blur();
                $.ajax({
                    url: "hideheader.php?hheader=0",
                    success: function (response) {
                    }
                });
            }
        }
    };

    topLayout = $('#container').layout(layoutSettings_Top);
    $("#btn-toggle-header").show();

    outerLayout = $("#main").layout(layoutSettings_Outer);
    outerLayout.sizePane('west', oLSize);
    if (!isPublication){
        outerLayout.sizePane('east', oRSize);
    }
    if (wstHide && wstHide == 1) {
        outerLayout.close("west");
    }

    if (!isPublication && isMobileMode && isMobileMode == 1){
        outerLayout.close("east");
    } else {
        if (eastHide && eastHide == 1) {
            outerLayout.close("east");
        } else {
            $("#btn-discussion-open").click();
        }
        //64032
        loadforum(goguid, grguid, gforumid);        
    }    

    loadDoc(docName);

    if (isMobileMode && isMobileMode == 1) {
        $("#btn-tree-open").unbind( "click" );
        $("#btn-tree-open").click(function () {
            ShowModalWindow(true);
            var url = 'mnavigator.php' + location.search;
            window.location.href = url;
        });
        $("#btn-discussion-open").unbind( "click" );
        $("#btn-discussion-open").click(function () {
           ShowModalWindow(true);
            var url = 'mforum.php' + location.search;
            window.location.href = url;
        });
        $('#btn-discussion-open').removeClass('btn-vertical-right-select').removeClass('btn-vertical-right-unselect').addClass('btn-vertical-right'); 
        $('#box-frame').width($('#box-frame').width() - 20);
    } else {
        outerLayout.options.stateManagement.autoSave = true;
        if ($("#box-tree > header").length) outerLayout.addCloseBtn("#box-tree > header", "west");
        if ($("#btn-tree-open").length) outerLayout.addOpenBtn("#btn-tree-open", "west");
        if ($("#box-layout-east > header").length) outerLayout.addCloseBtn("#box-layout-east > header", "east");
        if ($("#btn-discussion-open").length) outerLayout.addOpenBtn("#btn-discussion-open", "east");         
    }

    $('iframe#iframe-doc').ready(function () {
        var innerLayout;

        var layoutSettings_Inner = {
            fxName: "none",
            spacing_open: 0,
            spacing_closed: 0,
            togglerLength_open: 0,
            togglerLength_closed: 0,
            name: "innerLayout",
            defaults: { contentSelector: ".content" },
            north: {
                size: 30,
                spacing_open: 2,
                spacing_closed: 2,
                togglerLength_open: 0,
                togglerLength_closed: 0,
                resizable: false,
                slidable: false
            }
        };
        innerLayout = $('#box-frame').layout(layoutSettings_Inner);
        // 65029
        if (isMobileMode && isMobileMode == 1){
            $('#container').layout().resizeAll();
            $("#box-frame").width($("#box-frame").width()-20); //71182
        }        
    });

    $('iframe#iframe-doc').load(function () {
        if (eastHide && eastHide == 1 && $('#btn-discussion-open').is(":visible") ) {
            //66451
            $("#main").css("padding-right", "20px");
        }            
        $("iframe#iframe-doc").css("background-image", "none");
        if (/iPhone|iPod|iPad/.test(navigator.userAgent)) {
            $('#box-iframe').css({ height: "4000px", overflow: 'auto', '-webkit-overflow-scrolling': 'touch' });
            $('#iframe-doc').css({ height: "4000px", overflow: 'auto', '-webkit-overflow-scrolling': 'touch' });
        };
        var u = new Url;
        var u_frame = new Url(this.contentWindow.location.href);
        var p = u_frame.path.toLowerCase();

        if (u_frame.query["oguid"]) {
            u.query.oguid = u_frame.query["oguid"];
            $("#box-frame-header-right").show();
        } else {
            delete u.query["oguid"]
        }

        if (gDeleteLang && u_frame.query["lang"]) {
            delete u.query["lang"]
        }

        if (u_frame.query["rguid"]) {
            u.query.rguid = u_frame.query["rguid"];
        } else {
            delete u.query["rguid"]
        }

        if (u_frame.query["forumid"]) {
            u.query.forumid = u_frame.query["forumid"];
        } else if (p.substring(p.lastIndexOf('/') + 1, p.length) == 'forum.php') {
            u.query.forumid = 0;
        } else {
            delete u.query["forumid"]
        }

        if (supportHistoryApi()) {
            history.replaceState(null, '', u);
        }

        if (typeof redrawTreeToURL == 'function') {
            redrawTreeToURL();
        }
        var reportName = "";
        if ($('#cmreports').is(':visible')) {
            reportName = $("#cmreports option:selected").text();
        }
        ShowModalWindow(false);
    });

    function isInitStartPage(){
        var u_frame = new Url(window.location.href);
        return (u_frame.query["oguid"]);
    }

    $("#btn-tree-open").css({ top: ($("#box-frame").offset().top + 2) + 'px' });
    $("#btn-discussion-open").css({ top: ($("#box-frame").offset().top + 2) + 'px' });   

    if (!isPublication && ((eastHide && eastHide == 1 && !isInitStartPage()) || isMobileMode )) {
        $('#btn-discussion-open').show();
    }    
});