mw.session = {
    checkPause: false,
    checkPauseExplicitly: false,
    check: function (callback) {
        if (!mw.session.checkPause) {
            mw.session.checkPause = true;
            if (mw.session.checkPauseExplicitly) {
                return false;
            }
            $.post(mw.settings.api_url + "is_logged", function (data) {
                if (data === null) {
                    return;
                }
                if (data != false) {
                    if (typeof callback === 'function') {
                        callback.call(undefined, true)
                    }
                    ;
                }
                else {
                    if (typeof callback === 'function') {
                        callback.call(undefined, false)
                    }
                    ;
                }
                mw.session.checkPause = false;
            });
        }
    },
    logRequest: function () {
        var modal = mw.tools.modal.init({
            html: "<h3 style='margin:0;'>" + mw.msg.session_expired + ".</h3> <p style='margin:0;'>" + mw.msg.login_to_continue + ".</p> <br> <div id='session_popup_login'></div>",
            id: "session_modal",
            name: "session_modal",
            overlay: true,
            width: 400,
            height: 300,
            template: 'mw_modal_basic',
            callback: function () {
                mw.load_module("users/login", '#session_popup_login', false, {template: 'popup'});
            }
        });
    },
    checkInit: function () {
        if (self !== top) {
            return false;
        }
        setInterval(function () {
            mw.session.check(function (is_logged) {
                if (is_logged) {
                    var m = mw.tools.modal.get("#session_modal")
                    if (m) {
                        mw.$(m.overlay).remove();
                        mw.$(m.main).remove();
                    }
                }
                else {
                    mw.session.logRequest();
                }
            });
        }, 45000);
    }
}
$(document).ready(function () {

    $(document).bind("ajaxSend",function () {

        mw.session.checkPause = true;
    }).bind("ajaxComplete", function () {
            mw.session.checkPause = false;
        });
});
