$(function () {
    var video = $('.video');
    var videoMobile = $('.video-mobile');
    videoMobile.hide();
    document.addEventListener('visitorLocated', function (e) {
        let specialSpan = $('.residentsSpan');
        specialSpan.text(e.detail.country.toUpperCase());
        let video = $(".video video");
        $.ajax({
            method: "HEAD", url: "i18n/" + e.detail.isoCode.toLowerCase() + ".json", success: function () {
                if (e.detail.isoCode.toLowerCase() === "gb") {
                    var secondPopupHalf = $($('[data-init="registration-success"] .half')[1]);
                    var btcIconItem = $('<div class="item"><div class="icon"><img src="images/bitcoinicon.png"></div><div class="text"><h4 data-i18n>New!</h4><p><span data-i18n>Crypto Currency guidance for beginners and advanced traders!</span></p></div></div>');
                    secondPopupHalf.append(btcIconItem);
                }
            }, error: function () {
                var secondPopupHalf = $($('[data-init="registration-success"] .half')[1]);
                var btcIconItem = $('<div class="item"><div class="icon"><img src="images/bitcoinicon.png"></div><div class="text"><h4 data-i18n>New!</h4><p><span data-i18n>Crypto Currency guidance for beginners and advanced traders!</span></p></div></div>');
                secondPopupHalf.append(btcIconItem);
            }
        });
        let countryIsoCode = e.detail.isoCode.toLowerCase();
        switch (countryIsoCode) {
            case "ch":
                countryIsoCode = "de";
                break;
            case "at":
                countryIsoCode = "de";
                break;
            case "pm":
                countryIsoCode = "fr";
                break;
            case "fo":
                countryIsoCode = "dk";
                break;
            case "br":
                countryIsoCode = "es";
                break;
            case "ar":
                countryIsoCode = "es";
                break;
            case "mx":
                countryIsoCode = "es";
                break;
            case "cl":
                countryIsoCode = "es";
                break;
            case "cr":
                countryIsoCode = "es";
                break;
            case "pa":
                countryIsoCode = "es";
                break;
            case "pe":
                countryIsoCode = "es";
                break;
            case "ve":
                countryIsoCode = "es";
                break;
            case "co":
                countryIsoCode = "es";
                break;
            case "do":
                countryIsoCode = "es";
                break;
            case "bo":
                countryIsoCode = "es";
                break;
            case "ec":
                countryIsoCode = "es";
                break;
            case "by":
                countryIsoCode = "ru";
                break;
            case "ge":
                countryIsoCode = "ru";
                break;
            case "ua":
                countryIsoCode = "ru";
                break;
            case "md":
                countryIsoCode = "ru";
                break;
            case "kz":
                countryIsoCode = "ru";
                break;
            case "uz":
                countryIsoCode = "ru";
                break;
            case "kg":
                countryIsoCode = "ru";
                break;
            case "lt":
                countryIsoCode = "ru";
                break;
        }
        let videoUrl = "media/" + countryIsoCode + "-1.mp4";
        $.ajax({
            method: "HEAD", url: videoUrl, success: function (response) {
                video.attr('src', videoUrl)
            }, error: function (response) {
                console.log('No country specific video found for current visitor.');
            }
        });
        let videoUrl2 = "media/" + countryIsoCode + "-2.mp4";
        let video2 = $('.popup-video');
        $.ajax({
            method: "HEAD", url: videoUrl2, success: function (response) {
                video2.attr('src', videoUrl2)
            }, error: function (response) {
                console.log('No country specific register popup video found for current visitor.');
            }
        });
        $.ajax({
            method: "HEAD", url: "media/subtitles/" + countryIsoCode + ".vtt", success: function () {
                video.append($('<track kind="captions" srclang="' + countryIsoCode + '" src="media/subtitles/' + countryIsoCode + '.vtt" default>'));
            }, error: function () {
                video.append($('<track kind="captions" srclang="en" src="media/subtitles/en.vtt" default>'));
            }
        });
        $('.popup-youtube').on('click', function () {
            let index = $(this).attr('data-index');
            $('.mfp-iframe').on('load', function () {
                let video = ($($(this)[0].contentDocument).find('video'));
                video.append($('<track kind="subtitles" srclang="' + countryIsoCode.toUpperCase() + '" src="subtitles/' + countryIsoCode + '-' + index + '.vtt" default>'));
            });
        });
    });
    $('.register-btn').on('click', function () {
        $('html, body').animate({scrollTop: $(".residentsSpan").offset().top}, 1000);
    });
    let readMoreBtns = $('.read-more-alert-button');
    $.each(readMoreBtns, function (index, button) {
        $(button).on("click", function () {
            let confirmResult = confirm("Please Register In Order Receive The Software");
            if (confirmResult) {
                $('html, body').animate({scrollTop: $(".residentsSpan").offset().top}, 1000);
            }
        });
    });
});
