<script>
    //window.availableLanguages = <?php //echo json_encode(scandir(__DIR__ . '/../i18n')); ?>// ;
    window.intgrtnInit = function () {
        var path = "/";
        if (window.location.pathname.split("/")[2]) {
            path = "/" + window.location.pathname.split("/")[1] + "/";
        }
        window.intgrtn.setOptions({
            cookies: {
                path: path
            },
            forms: {
                signup: {
                    buttons: {
                        generatePassword: {
                            show: true,
                        },
                    },
                },
            },
        },false);

        window.intgrtn.events.on("get.project.details.success", function (response) {
            $(document).prop('title', response.data.project.name);

            if (window.intgrtn.lookup("flowType") === true) {
                window.intgrtn.store("flowType", "quizz");
            }

            var currency = response.data.client.location.currency.symbol ? response.data.client.location.currency.symbol : "$";
            $("[data-visitor-currency-symbol]").text(currency);

            initUiFlags(response);
        });

        function initUiFlags(projectDetailsResponse) {
            var currencySymbol = projectDetailsResponse.data.client.location.currency.symbol ? projectDetailsResponse.data.client.location.currency.symbol : "$";
            var countryIso = projectDetailsResponse.data.client.location.countryIsoCode ? projectDetailsResponse.data.client.location.countryIsoCode.toLowerCase() : null;
            var country = projectDetailsResponse.data.client.location.country ? projectDetailsResponse.data.client.location.country : null;
            var countryColors = {
                "AT": ['#ed2939', '#ed2939'],
                "AU": ['#00008b', '#ff0000'],
                "BE": ['#000000', '#fdda24'],
                "BG": ['#05ac20', '#fd1322'],
                "CA": ['#ff0000', '#ff0000'],
                "CH": ['#ff0000', '#ff0000'],
                "CL": ['#0036a7', '#d62718'],
                "CZ": ['#d7141a', '#11457e'],
                "DE": ['#000000', '#dd0000'],
                "DK": ['#c60c30', '#c60c30'],
                "FI": ['#003580', '#003580'],
                "FR": ['#0050a4', '#ef4135'],
                "GA": ['#4997d0', '#4997d0'],
                "GB": ['#ff0000', '#00008b'],
                "GR": ['#001489', '#001489'],
                "IE": ['#169b62', '#ff883e'],
                "IT": ['#009246', '#ce2b37'],
                "MY": ['#1534cb', '#59292e'],
                "MX": ['#006847', '#ce1126'],
                "NL": ['#ae1c28', '#21468b'],
                "NO": ['#ef2b2d', '#002868'],
                "NZ": ['#00247d', '#cc142b'],
                "PH": ['#0038a8', '#ce1126'],
                "PK": ['#01411c', '#01411c'],
                "PL": ['#ff0000', '#ff0000'],
                "SE": ['#006aa7', '#fecc00'],
                "SG": ['#ee2536', '#ee2536'],
                "SK": ['#034da3', '#ef1620'],
                "ZA": ['#FFB612', '#007A4D']
            };

            $("[data-visitor-currency-symbol]").text(currencySymbol);


            $('.country-name-custom').each(function () {
                var obj = $(this);
                var countryIsoUpper = countryIso.toUpperCase();
                if (countryColors.hasOwnProperty(countryIsoUpper)) {
                    var words = country.split(/ (.+)/);
                    if (words.length < 2) {
                        words[0] = country.substring(0, country.length / 2);
                        words[1] = country.substring(country.length / 2);
                    } else {
                        words[0] += ' ';
                    }
                    $('<span/>').css('color', countryColors[countryIsoUpper][0]).text(words[0].toUpperCase()).appendTo(obj);
                    $('<span/>').css('color', countryColors[countryIsoUpper][1]).text(words[1].toUpperCase()).appendTo(obj);
                } else {
                    obj.text(country);
                }
                $('.flag-wrapper').removeClass('hidden');
            });
        }

    };

    (function (d) {
        if (window.intgrtn) {
            return;
        }
        var s = d.createElement("script");
        var date = new Date();
        s.src = "<?php echo $trackingUrl ?>/api/v2/integration/app.js?v=" + date.getFullYear().toString() + date.getMonth().toString() + date.getDate().toString() + date.getHours().toString() + Math.round(date.getMinutes() / 10).toString();
        d.getElementsByTagName("head")[0].appendChild(s);
    }(document));
</script>
