document.write('<div id="whitelabeled_internet_vergelijker"></div>');

var whitelabeled_url = 'https://internet.whitelabeled.nl/show/yrLZqWKt7XHREzhN-195?noJS=1&tracking=TRACKING';

if (typeof wli_options !== 'undefined') {
    if (typeof wli_options['tracking'] !== 'undefined') {
        var tracking = wli_options['tracking'];
        whitelabeled_url = whitelabeled_url.replace('TRACKING', encodeURIComponent(tracking));
    }
} else if (typeof wlm_options !== 'undefined') {
    if (typeof wlm_options['tracking'] !== 'undefined') {
        var tracking = wlm_options['tracking'];
        whitelabeled_url = whitelabeled_url.replace('TRACKING', encodeURIComponent(tracking));
    }
}
whitelabeled_url = whitelabeled_url.replace('TRACKING', '-');

load(whitelabeled_url);

function loadScript(url, callback) {
    var script = document.createElement("script")
    script.type = "text/javascript";
    if (script.readyState) { //IE
        script.onreadystatechange = function () {
            if (script.readyState == "loaded" || script.readyState == "complete") {
                script.onreadystatechange = null;
                callback();
            }
        };
    } else { //Others
        script.onload = function () {
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

/* https://ghinda.net/article/script-tags/ */
function load(url) {
    var r = new XMLHttpRequest();
    r.open("GET", url, true);
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) return;
        // run the scripts inside the dom node
        var $container = document.getElementById('whitelabeled_internet_vergelijker');
        $container.innerHTML = r.responseText;
        runScripts($container)
    };
    r.send();
}

/* helpers
 */
// runs an array of async functions in sequential order
function seq(arr, callback, index) {
// first call, without an index
    if (typeof index === 'undefined') {
        index = 0
    }

    arr[index](function () {
        index++
        if (index === arr.length) {
            callback()
        } else {
            seq(arr, callback, index)
        }
    })
}

// trigger DOMContentLoaded
function scriptsDone() {
    var DOMContentLoadedEvent = document.createEvent('Event')
    DOMContentLoadedEvent.initEvent('DOMContentLoaded', true, true)
    document.dispatchEvent(DOMContentLoadedEvent)
}

/* script runner
 */

function insertScript($script, callback) {
    var s = document.createElement('script')
    s.type = 'text/javascript'
    if ($script.src) {
        s.onload = callback
        s.onerror = callback
        s.src = $script.src
    } else {
        s.textContent = $script.innerText
    }

// re-insert the script tag so it executes.
    document.head.appendChild(s)

// clean-up
    $script.parentNode.removeChild($script)

// run the callback immediately for inline scripts
    if (!$script.src) {
        callback()
    }
}
// https://html.spec.whatwg.org/multipage/scripting.html
var runScriptTypes = [
    'application/javascript',
    'application/ecmascript',
    'application/x-ecmascript',
    'application/x-javascript',
    'text/ecmascript',
    'text/javascript',
    'text/javascript1.0',
    'text/javascript1.1',
    'text/javascript1.2',
    'text/javascript1.3',
    'text/javascript1.4',
    'text/javascript1.5',
    'text/jscript',
    'text/livescript',
    'text/x-ecmascript',
    'text/x-javascript'
];
function runScripts($container) {
// get scripts tags from a node
    var $scripts = $container.querySelectorAll('script')
    var runList = []
    var typeAttr

    [].forEach.call($scripts, function ($script) {
        typeAttr = $script.getAttribute('type')

// only run script tags without the type attribute
// or with a javascript mime attribute value
        if (!typeAttr || runScriptTypes.indexOf(typeAttr) !== -1) {
            runList.push(function (callback) {
                insertScript($script, callback)
            })
        }
    })

// insert the script tags sequentially
// to preserve execution order
    seq(runList, scriptsDone)
}

var wli_amplitude = false;
