$(function() {
    document.addEventListener('visitorLocated', function(e) {
        let video = $('.video video');
        let lowerCaseIsoCode = e.detail.isoCode.toLowerCase();
        let englishSpeakingIsoCodes = ['gb', 'us', 'ie', 'au'];

        $('.storyVideo-1').attr('src', 'media/en-1-t1.mp4');
        $('.storyVideo-2').attr('src', 'media/en-1-t2.mp4');
        $('.storyVideo-3').attr('src', 'media/en-1-t3.mp4');

        $.ajax({
            method: 'HEAD',
            url: 'img/story-img-' + lowerCaseIsoCode + '-1.png',
            success: function() {
                setStoryImages(lowerCaseIsoCode);
            },
            error: function() {
                console.log("Using default story images.");
            }
        })

        function setStoryImages(lowerCaseIsoCode) {
            $.ajax({
                method: 'HEAD',
                url: 'img/story-img-' + lowerCaseIsoCode + '-4.png',
                success: function() {
                    let storyImg1 = $("#story-img-1");
                    let storyImg2 = $("#story-img-2");
                    let storyImg3 = $("#story-img-3");
                    let storyImg4 = $("#story-img-4");

                    storyImg1.attr("src", 'img/story-img-' + lowerCaseIsoCode + '-1.png');
                    storyImg2.attr("src", 'img/story-img-' + lowerCaseIsoCode + '-2.png');
                    storyImg3.attr("src", 'img/story-img-' + lowerCaseIsoCode + '-3.png');
                    storyImg4.attr("src", 'img/story-img-' + lowerCaseIsoCode + '-4.png');
                },
                error: function() {
                    $(".stories-4-images-design").css("display", "none");
                    $(".stories-3-images-design").css("display", "block");
                    let storyImg1 = $("#story-img-1-design-3-images");
                    let storyImg2 = $("#story-img-2-design-3-images");
                    let storyImg3 = $("#story-img-3-design-3-images");

                    storyImg1.attr("src", 'img/story-img-' + lowerCaseIsoCode + '-1.png');
                    storyImg2.attr("src", 'img/story-img-' + lowerCaseIsoCode + '-2.png');
                    storyImg3.attr("src", 'img/story-img-' + lowerCaseIsoCode + '-3.png');
                }
            });
        }

        if (!englishSpeakingIsoCodes.includes(lowerCaseIsoCode))
            $('.videoStoriesContainer').remove();

        $.ajax({
            method: 'HEAD',
            url: 'media/' + lowerCaseIsoCode + '-2.mp4',
            success: function() {
                video.attr('src', 'media/' + lowerCaseIsoCode + '-2.mp4');
                video.attr('poster', 'img/video-thumbnail-' + lowerCaseIsoCode + '-step2.png');
            },
            error: function() {
                let esIsoCode = "es";
                switch (lowerCaseIsoCode) {
                    case "mx":
                    case "cl":
                    case "co":
                    case "ar":
                    case "uy":
                    case "cr":
                    case "ec":
                    case "pa":
                    case "pe":
                    case "ve":
                        video.attr('src', 'media/es-2.mp4');
                        video.attr('poster', 'img/video-thumbnail-es-step2.png');
                        video.prop('autoplay', true);
                        setStoryImages(esIsoCode);
                        break;
                    default:
                        video.attr('src', 'media/en-2.mp4');
                        video.attr('poster', 'img/video-thumbnail-en-step2.png');
                        video.prop('autoplay', true);
                        setStoryImages('en');
                }
                console.log("Using default video");
            }
        });
    });
});