[%+ PROCESS _common_js_bugs.tt +%]

Ext.namespace("TP");
Ext.BLANK_IMAGE_URL     = url_prefix+'plugins/[% plugin_name %]/images/s.gif';
var default_dashboard   = [% json_encode(default_dashboard) %];
var initial_shapes      = [% json_encode(shapes) %];
var default_state_order = [% json_encode(default_state_order) %];

/* create state provider */
TP.cp = new Ext.state.HttpProvider({
    url: 'panorama.cgi?state'
});
TP.cp.loadData([% json_encode(extstate) %]);
Ext.state.Manager.setProvider(TP.cp);

if(TP.initial_active_tab == undefined && get_hash(1)) {
    TP.initial_active_tab = TP.nr2TabId(get_hash(1));
}
if(TP.cp.get('tabbar').activeTab && TP.initial_active_tab == undefined) {
    TP.initial_active_tab = TP.nr2TabId(TP.cp.get('tabbar').activeTab);
}
if(TP.initial_active_tab == undefined) {
    TP.initial_active_tab = TP.nr2TabId("0");
}

Ext.QuickTips.init();

Ext.onReady(function() {
    cookieSave("thruk_tz", getBrowserTimezone());
    cookieSaveScreenSize();

    /* main viewport */
    if(one_tab_only) {
        TP.viewport = new Ext.Viewport({
            id:       'tabbar',
            layout:   'border',
            renderTo: Ext.getBody(),
            margins:  '0 0 0 0',
            border:   false,
            items:    [],
            xdata:    {},
            getActiveTab: function() {
                if(this.items.length > 0) {
                    return(this.items.get(0));
                }
                return;
            },
            setActiveTab: function() {}
        });
        TP.add_pantab({ id: one_tab_only });
    } else {
        TP.viewport = new Ext.Viewport({
            id:       'viewport',
            layout:   'border',
            renderTo: Ext.getBody(),
            margins:  '0 0 0 0',
            items:    [ new TP.TabBar() ]
        });
        [% IF thruk_author && !c.config.demo_mode %]
        Ext.getCmp('debug_tab').show();
        Ext.getCmp('debug_dom_elements').show();
        [% END %]

        if(readonly || dashboard_ignore_changes) {
            var icon = Ext.query('SPAN.new-tab')[0];
            icon.parentNode.parentNode.parentNode.style.display = 'none';
        }
    }

    /* full reload page interval */
    [% IF full_reload_interval != 0 %]
    TP.timeouts['timeout_page_reload'] = window.setInterval("TP.fullReload()", [% full_reload_interval %]*1000);
    TP.log('[global] full_reload_interval set to: [% full_reload_interval %]');
    [% END %]
    cleanUnderscoreUrl();
    initLastUserInteraction();

    jQuery(window).off("focus").on("focus", function() {
        // refresh current tab when switching browser tabs and last refresh is older than 10min
        var tabbar = Ext.getCmp('tabbar');
        var tab = tabbar.getActiveTab();
        if(tab && (!tab.last_full_refresh || tab.last_full_refresh < (new Date()).getTime() - 600000)) {
            if(tab.xdata && tab.xdata.refresh > 0) {
                TP.updateAllIcons(tab);
            }
        }
    });

    /* disable backspace on body to prevent accidentally leaving the page */
    Ext.EventManager.on(document, 'keydown', function(evt, t) {
        if(evt.getKey() == evt.BACKSPACE && evt.target) {
            if(evt.target.tagName.toLowerCase() != "input" && evt.target.tagName.toLowerCase() != "textarea") {
                evt.preventDefault();
                return false;
            }
        }
    });

    /* disable scrolling if body is masked */
    var bodyDom  = Ext.getBody().dom;
    var bodyMask = false;
    TP.timeouts['body_scroll_interval'] = window.setInterval(function() {
        if(bodyDom.className.match(/mask/)) {
            if(!bodyMask) {
                document.documentElement.style.overflow = "inherit";
                bodyMask = true;
            }
        } else {
            if(bodyMask) {
                document.documentElement.style.overflow = "";
                bodyMask = false;
            }
        }
    }, 500);

    /* reset scrollbar timeout on html element scroll */
    var htmlRootEl = Ext.fly(Ext.getBody().dom.parentNode);
    htmlRootEl.on({
        'mousewheel': TP.setBaseHTMLScroll,
        'wheel':  TP.setBaseHTMLScroll,
        'DOMMouseScroll': TP.setBaseHTMLScroll,
        'scroll': TP.setBaseHTMLScroll
    });
    Ext.getBody().on({
        'mousewheel': TP.setBaseHTMLScroll,
        'wheel':  TP.setBaseHTMLScroll,
        'DOMMouseScroll': TP.setBaseHTMLScroll,
        'scroll': TP.setBaseHTMLScroll
    });

    jQuery(window).on('beforeunload unload', function(e){
        thrukState.unloading = true;
    });

});
