/* Minification failed. Returning unminified contents.
(202,33-36): run-time error JS1009: Expected '}': ...
(202,33-36): run-time error JS1006: Expected ')': ...
(201,30): run-time error JS1004: Expected ';'
(203,37): run-time error JS1004: Expected ';'
(203,37-38): run-time error JS1195: Expected expression: :
(204,29-30): run-time error JS1195: Expected expression: }
(205,18-19): run-time error JS1195: Expected expression: )
(218,5-6): run-time error JS1006: Expected ')': }
(217,10): run-time error JS1004: Expected ';'
(218,6-7): run-time error JS1195: Expected expression: )
 */
///// SCROLL TO SECTION
$('.homepage-category_bar-item').click(function () {
    var sectionNr = $(this).data('id');
    $('html, body').animate({
        scrollTop: $(".homepage-section[data-id=" + sectionNr + "]").offset().top - 70
    }, 1000);
});

///////  CATEGORY BAR STICKY ON SCROLL
//if ($('.homepage-slider').length) {
//    $(window).scroll(function () {
//        var scroll = $(window).scrollTop(),
//            startScroll = $('.homepage-slider').offset().top + $('.homepage-slider').outerHeight();

//        if (scroll >= startScroll) {
//            $(".category_bar-container-home").addClass("sticky");
//            $(".sticky-div").show();
//        } else {
//            $(".category_bar-container-home").removeClass("sticky");
//            $(".sticky-div").hide();
//        }

//    });
//}


//////////////////////// SLIDERS /////////////////

///// HOMEPAGE VIDEOS SLIDER MOBILE
var slider = $('.homepage-videos__container'),
    slideItem = $('.homepage-videos__all'),
    slideWidth = $('.homepage-videos__container').outerWidth(),
    dot = $('.homepage-videos__dot'),
    maxSlides = 3,
    slideTo = 0,
    interval;

var sectionsPage = 1,
    sectionReqReturned = true,
    sectionsSeen = [];


function changeSlide(i) {
    if (i > 2) {
        i = 0;
        slideTo = 0;
    }
    slider.css('transform', 'translate3d(-' + i * 100 + '%, 0 ,0)');
    $('.homepage-videos__dot[data-slide-to="' + i + '"]').addClass('active').siblings().removeClass('active');
    $('.homepage-videos__all[data-slide="' + i + '"]').addClass('current').siblings().removeClass('current');
}
//AUTOPLAY
function playSlider() {
    interval = setInterval(function () {
        changeSlide(++slideTo);
    }, 5000);
}

function destroySlider() {
    changeSlide(0);
    clearInterval(interval);
}

function slideRight() {
    slideTo++;
    if (slideTo > 2) {
        slideTo = 0;
    }
    changeSlide(slideTo);
}

function slideLeft() {
    slideTo--;
    if (slideTo < 0) {
        slideTo = 2;
    }
    changeSlide(slideTo);
}

$(document).ready(function () {
    //AUTOPLAY
    if ($(window).outerWidth() < 993) {
        //SLIDER TEXT HEIGHT
        var sliderTextHeight = $('.homepage-videos__main').innerHeight() - $('.homepage-videos__item').innerHeight();
        $('.homepage-videos__content').outerHeight(sliderTextHeight);
        $('.carousel-indicators').css('bottom', sliderTextHeight);

        $('.homepage-videos__dot[data-slide-to="' + slideTo + '"]').addClass('active').siblings().removeClass('active');
        $('.homepage-videos__all[data-slide="' + slideTo + '"]').addClass('current').siblings().removeClass('current');
        playSlider();

    }
});

dot.click(function () {
    $(this).addClass('active').siblings().removeClass('active');
    slideTo = $(this).attr('data-slide-to');
    changeSlide(slideTo);
});

$(window).resize(function () {
    if ($(window).outerWidth() > 992) {
        destroySlider();
    }
    else {
        clearInterval(interval);
        playSlider();
    }
});

// Swipe slider v2
$("#homepage_slider-standard").on("touchstart", function (event) {
    var xClick = event.originalEvent.touches[0].pageX;
    $(this).one("touchmove", function (event) {
        var xMove = event.originalEvent.touches[0].pageX;
        if (Math.floor(xClick - xMove) > 5) {
            clearInterval(interval);
            slideRight();
            playSlider();
        }
        else if (Math.floor(xClick - xMove) < -5) {
            clearInterval(interval);
            slideLeft();
            playSlider();
        }
    }).on("touchend", function () {
        $(this).off("touchmove");
    });
});

// Swipe slider v1
$("#hero__slider").on("touchstart", function (event) {
    var xClick = event.originalEvent.touches[0].pageX;
    $(this).one("touchmove", function (event) {
        var xMove = event.originalEvent.touches[0].pageX;
        if (Math.floor(xClick - xMove) > 5) {
            $('.carousel-control-next')[0].click();
        }
        else if (Math.floor(xClick - xMove) < -5) {
            $('.carousel-control-prev')[0].click();
        }
    }).on("touchend", function () {
        $(this).off("touchmove");
    });
});


var hSections;
if ($("#hp-sections").length > 0)
    hSections = JSON.parse($("#hp-sections").val());

function getSectionsAsync() {
    var section = hSections.shift();
    if (!section) {
        window.removeEventListener("scroll", homepageSectionScrollHandler);
        return;
    }
    if (sectionReqReturned) {
        sectionReqReturned = false;
        $.ajax({
            url: "/home/section" + section,
            type: "GET",
            beforeSend: function () {
                $('#section-loader').removeClass('d-none');
            },
            success: function (response) {
                if (response !== "") {
                    $("#homepage__sections").append(response);
                } else {
                    window.removeEventListener("scroll", homepageSectionScrollHandler);
                }
            }
        }).done(function () {
            sectionReqReturned = true;
            $('#section-loader').addClass('d-none');
            imgLazy.update();
            homepageSliders.load();
        });
    }
}
function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    //return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    return elemBottom <= docViewBottom;
}
function homepageSectionScrollHandler() {
   
    var sections = document.querySelectorAll(".h-section");
    sections.forEach(section => {
        if (isScrolledIntoView(section) && !sectionsSeen.includes(section.dataset.id)) {
            if (section.dataset.sectionchildren) {
                let sectionsChildren = JSON.parse(section.dataset.sectionchildren)
                sectionsChildren.forEach(sectionChild => {
                    if (window.productsModel && !window.productsModel.find(m => m.Id == sectionChild.Id) && sectionChild.Title !=="")
                        window.productsModel.push(
                            {
                                ...sectionChild,
                                Name: section.dataset.title + " - " + sectionChild.Position,
                            })
                })
            }
         

            if (window.analyticsEvents) {
                var jsonParsed = JSON.parse(section.dataset.sectionids);
                if (typeof jsonParsed !== "undefined")
                    window.analyticsEvents.promoView.push(jsonParsed, section.dataset.dimension);
                else
                    console.log(section.dataset);
            }
            sectionsSeen.push(section.dataset.id);
        }
    })
    if (document.querySelector("#homepage__sections")) {
        var bottomOfContent =
            Math.round(
                document.querySelector("#homepage__sections").getBoundingClientRect().bottom + this.pageYOffset) -
            50;
        if (sectionReqReturned && ((this.innerHeight + this.pageYOffset) >= bottomOfContent)) {
            getSectionsAsync();
        }
    }
}

window.addEventListener("scroll", homepageSectionScrollHandler);


;
