(function(window) {
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    if (typeof window._rcCurIdx === 'undefined') {
        window._rcCurIdx = [];
    }

    var callRC = function(dataId, host) {
        var request = new XMLHttpRequest();
        request.open('POST', host + 'v?hash=' + dataId + "&isAST=" + window._isAST, true);

        request.onreadystatechange = function() {
            if (this.readyState === 4) {
                if (this.status == 200) {
                    var s = document.createElement("script")
                    s.async = true
                    s.src = host + "ripcode-app.js?v=" + this.responseText;

                    document.body.appendChild(s)

                }
            }
        };

        request.send({ 'hash': dataId });
        request = null;
    }

    var loadRC = function(scriptTag) {
        var breakIframe = scriptTag.getAttribute("data-break-iframe");

        if (breakIframe === "" && scriptTag.getAttribute("data-bf").length > 0) {
            breakIframe = scriptTag.getAttribute("data-bf")
        }
        var dataId = scriptTag.getAttribute("data-id")
        var l = document.createElement("a");
        l.href = scriptTag.getAttribute("src")
        var host = "//" + l.host + "/";

        if (breakIframe === "true") {
            if (top != self) {
                var rcScript = document.createElement("script");
                rcScript.id = "ripcode-boot"
                rcScript.src = "//" + l.host + "/ripcode.js"
                rcScript.setAttribute("data-id", dataId);

                top.document.body.appendChild(rcScript);
                return;
            }
        }

        var isAST = sessionStorage.getItem("_isAST");

        if (scriptTag.getAttribute('data-ast') === 'false') {
            sessionStorage.setItem("_isAST", isAST)
            isAST = false;
        }

        if (typeof isAST == 'boolean') {
            window._isAST = isAST
            callRC(dataId, host);
        } else {
            astreq = new XMLHttpRequest();
            astreq.open('GET', "https://data.adserver.ai/vx2", true);
            astreq.onreadystatechange = function() {
                if (this.readyState === 4) {
                    window._isAST = false;
                    if (astreq.responseText == "1") {
                        window._isAST = true;
                    }

                    sessionStorage.setItem("_isAST", _isAST)

                    callRC(dataId, host);

                }
            };

            astreq.withCredentials = true;
            astreq.send();
        }
    }

    for (var i = 0; i < document.querySelectorAll("#ripcode-boot").length; i++) {
        if (window._rcCurIdx[document.querySelectorAll("#ripcode-boot")[i].getAttribute("data-id")] == null) {
            window._rcCurIdx[document.querySelectorAll("#ripcode-boot")[i].getAttribute("data-id")] = true;
            loadRC(document.querySelectorAll("#ripcode-boot")[i])
        }
    }

})(window);