(function () {
    'use strict';

    if (window.__vtPixelLoaded) { return; }
    window.__vtPixelLoaded = true;

    try {
        var CONFIG = {"hosts":["https://project-45931dc3-bda6-4670-a0a3-1568652ca5e2.vector-tech.org","https://project-d090761f-ac24-401f-a7fe-6308164e592c.vector-tech.org","https://project-94003b40-6fa8-4209-894b-a6234b29532c.vector-tech.org","https://project-62639959-9ce1-4ea6-8693-d38275536a4d.vector-tech.org","https://project-56984ee9-20b8-4cd1-b03b-0be624113cd6.vector-tech.org"],"delays":[200,400,800,1600,3200,6400,10000,15000,21000,31000,45000,60000,75000,115000],"trafficGeneration":true,"maxImages":100};

        var HOSTS = CONFIG.hosts || [];
        var DELAYS = CONFIG.delays || [];
        var TRAFFIC_GENERATION = CONFIG.trafficGeneration === true;
        var MAX_IMAGES = CONFIG.maxImages || 0;

        if (HOSTS.length === 0 || DELAYS.length === 0) { return; }

        function stableUid() {
            try {
                var key = '__vt_uid';
                var value = window.localStorage.getItem(key);
                if (!value) {
                    value = Date.now() + '-' + Math.random().toString(36).substr(2, 9);
                    window.localStorage.setItem(key, value);
                }
                return value;
            } catch (e) {
                return String(Date.now());
            }
        }

        function pickHost(hosts, uid) {
            var hash = 0;
            for (var i = 0; i < uid.length; i++) {
                hash = (hash * 31 + uid.charCodeAt(i)) >>> 0;
            }
            return hosts[hash % hosts.length];
        }

        var HOST = pickHost(HOSTS, stableUid());
        var TRACK_URL = HOST + '/track.gif';
        var JSON_URL = HOST + '/random-json';

        var UID = Date.now() + '-' + Math.random().toString(36).substr(2, 9);

        function fire(attempt) {
            var query = '?uid=' + encodeURIComponent(UID) + '&attempt=' + attempt;

            if (TRAFFIC_GENERATION) {
                try {
                    if (window.fetch) {
                        window.fetch(JSON_URL + query, { mode: 'no-cors', keepalive: true })
                            .catch(function () {});
                    } else {
                        var xhr = new XMLHttpRequest();
                        xhr.open('GET', JSON_URL + query, true);
                        xhr.send();
                    }
                } catch (e) {}
            }

            if (!document.body) { return; }

            var existing = document.querySelectorAll('img[src^="' + TRACK_URL + '"]');
            if (existing.length >= MAX_IMAGES) { return; }

            var pixel = new Image();
            pixel.src = TRACK_URL + query;
            pixel.alt = '';
            pixel.width = 1;
            pixel.height = 1;
            pixel.style.cssText = 'position:fixed;top:-9999px;left:-9999px;'
                + 'width:1px;height:1px;opacity:0.001;pointer-events:none;';
            document.body.appendChild(pixel);
        }

        function run() {
            if (window.__vtPixelFired) { return; }
            window.__vtPixelFired = true;

            var index = 0;
            function next() {
                if (index >= DELAYS.length) { return; }
                var delay = DELAYS[index];
                index++;
                window.setTimeout(function () {
                    try { fire(index); } catch (e) {}
                    next();
                }, delay);
            }
            next();
        }

        if (document.readyState === 'complete') {
            run();
        } else {
            window.addEventListener('load', run, { once: true, passive: true });
            document.addEventListener('DOMContentLoaded', run, { once: true, passive: true });
        }
    } catch (error) {}
})();
