(function() {
    if (window.__matt_version_callback) {
        __matt_version_callback({{NEW_VERSION}});
        return;
    }




    function init() {
        var currentVersion = -1;
        window.__matt_version_callback = function(version) {
            if (currentVersion == -1) {
                currentVersion = version;
                return;
            }
            if (version != currentVersion) {
                console.log('Version changed, refreshing: '+currentVersion+' -> '+version);
                setTimeout(() => {
                    window.location.reload();
                }, 1500);
            }
        };


        var url = window.__matt_js_url;
        var scriptTag = null;
        function performJsonpCheck(repeat) {
            if (scriptTag) {
                document.head.removeChild(scriptTag);
            }
            scriptTag = document.createElement('script');
            scriptTag.setAttribute('src', url.replace(/(\?.*)/, '') + '?t=' + (new Date()).getTime());
            document.head.appendChild(scriptTag);


            if (repeat) {
                setTimeout(function() {
                    performJsonpCheck(true);
                }, 1000);
            }
        }

        performJsonpCheck(true);
        setInterval(function() {
            performJsonpCheck(false);
        }, 10000);
    }

    function ready(fn) {
        if (document.readyState != 'loading') {
            fn();
        } else {
            document.addEventListener('DOMContentLoaded', fn);
        }
    }

    ready(init);
})();
