// source --> https://landoll.com/wp-content/themes/Avada-Child-Theme/custom.js?ver=2fbb58059a042ba273dc80ae30b771a4 
jQuery(document).ready(function ($) {


    // ==============================
    // Sticky Objects

    function updateOffset() {

        var stickyOffset = 0;

        var wpadminbarHeight = Number($("#wpadminbar").outerHeight());
        if ($.isNumeric(wpadminbarHeight)) {
            stickyOffset = stickyOffset + wpadminbarHeight;
        }

        var bannerHeight = Number($("#banner").outerHeight());
        if ($.isNumeric(bannerHeight)) {
            stickyOffset = stickyOffset + bannerHeight;
        }

        var headerHeight = Number($("#header").outerHeight());
        if ($.isNumeric(headerHeight)) {
            stickyOffset = stickyOffset + headerHeight;
        }

        stickyOffset = Math.round(stickyOffset);
        var viewportWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        var value = (viewportWidth > 1024) ? stickyOffset + "px" : "";
        $(":root").css("--awb-sticky-offset", value);

        $("#main .fusion-sticky-container").attr("data-sticky-offset", value);

    }

    if ($("#main .awb-sticky").length || $("#main .fusion-sticky-container").length) {

        updateOffset();

        // run on window resize
        $(window).resize(function () {
            updateOffset();
        });

    }


    // ==============================
    // Filter

    $(".filter").each(function () {

        // what's what
        var t = ".awb-menu__main-a"

        // make first tab active by default
        $(t + ":first", this).addClass("active");

        // find tabs with EXACT text match
        $(t + " span", this).filter(function () {
            return $(this).text() === "Landoll";
        }).parent().parent().addClass("brands-top");
        $(t + " span", this).filter(function () {
            return $(this).text() === "Brillion";
        }).parent().parent().addClass("brands-bottom");

        // add onclick event to all tabs
        $(".awb-menu__main-a", this).on("click", function () {

            // what's what
            var t = ".filter .awb-menu__main-a";
            var s = ".filter .section";
            var p = ".filter .product";
            var target = $(this).text().toLowerCase().replace(/[ –—]/g, "-");
            target = target.replace(/-?&/g, "");
            var all = false;
            var brand = false;

            if (["view-all", "show-all", "reset-filter", "reset"].includes(target)) {
                all = true;
            }
            if (["landoll", "brillion"].includes(target)) {
                brand = true;
            }

            // tab logic
            if (all) {
                $(t).removeClass("active active2");
                $(this).addClass("active");
            } else if (brand) {
                $(t + ":first").removeClass("active");
                $(t).removeClass("active2");
                $(this).addClass("active2");
            } else {
                $(t).removeClass("active");
                $(this).addClass("active");
            }

            // content logic
            if (all) {
                $(s + ", " + p).removeClass("inactive");
            } else if (brand) {
                $(p).addClass("inactive");
                $("." + target).removeClass("inactive");
            } else {
                $(s).addClass("inactive");
                $("#" + target).removeClass("inactive");
            }

            //scroll logic
            if (all) {
                $('html, body').animate({
                    scrollTop: 0
                }, 500);
            } else if (!brand) {
                // scroll content into view accounting for wpadminbar, and header height
                var offsetHeight = 0;
                var wpadminbarHeight = Number($("#wpadminbar").outerHeight());
                if ($.isNumeric(wpadminbarHeight)) {
                    offsetHeight = offsetHeight + wpadminbarHeight;
                }
                var headerHeight = Number($("#header").outerHeight());
                if ($.isNumeric(headerHeight)) {
                    offsetHeight = offsetHeight + headerHeight;
                }
                $('html, body').animate({
                    scrollTop: $("#" + target).offset().top - offsetHeight
                }, 500);
            }
        });
    });


    // ==============================
    // Color Picker Slider

    if ($(".color-picker").length) {

        var images = $(".color-picker img");

        images.each(function () {
            var img = new Image();
            img.onload;
            img.src = $(this).attr("src");
        });

        var colorMenuItems = $(".color-picker .awb-menu__main-a");

        // make first tab active by default
        colorMenuItems.first().addClass("active");

        // add onclick trigger to tabs
        colorMenuItems.each(function () {

            var tabText = $(this).text().trim();
            var imgColor = tabText.replace(/[^A-Za-z0-9]/g, "");
            var className = tabText.toLowerCase().replace(/[ –—]/g, "-");

            $(this).addClass(className);

            $(this).on("click", function () {

                // replace image sources
                images.each(function () {
                    var source = $(this).attr("src");
                    if (this.hasAttribute("srcset")) {
                        var srcSet = $(this).attr("srcset");
                    }

                    var colors = new RegExp("(Red|ViperRed|Green|BalboaBlue|SapphireBlue|Yellow|Black|AnniversaryGray|MetallicGray|White)", "g");

                    source = source.replace(colors, imgColor);
                    $(this).attr("src", source);
                    if (typeof srcSet !== 'undefined') {
                        srcSet = srcSet.replace(colors, imgColor);
                        $(this).attr("srcset", srcSet);
                    }
                });

                // reset tabs then make clicked tab active
                $(".color-picker a").removeClass("active");
                $(this).addClass("active");

            });
        });

        // Swiper.js slider settings
        const swiper = new Swiper('.swiper', {

            // basic settings
            direction: 'horizontal',
            loop: true,
            pagination: {
                el: '.swiper-pagination',
                clickable: true,
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },

            // customize setting
            autoplay: false,
            disableOnInteraction: true,
            centeredSlides: true,
            centeredSlidesBounds: true,
            effect: "fade",
            fadeEffect: {
                crossFade: true,
            },
            keyboard: {
                enabled: true,
            },

        });
    }


    // ==============================
    // Tabs

    $(".tabs").each(function () {

        var tabSet = $(this);

        $(".awb-menu__main-a", this).each(function () {

            // what's what
            var t = ".awb-menu__main-a";
            var c = ".tab-content";
            var target = $(this).text().toLowerCase().replace(/( & |, | )/g, "-");

            // hide all contents by default
            $("#" + target).addClass(c.substring(1) + " inactive");

            // add onclick trigger to tabs
            $(this).on("click", function () {

                // reset tabs and contents
                $(t).removeClass("active");
                $(c).addClass("inactive");

                // make clicked tab active and show target content
                $(this).addClass("active");
                $("#" + target).removeClass("inactive");

                // scroll content into view while accounting for wpadminbar, header, tabs, and header top margin height
                var offsetHeight = 0;
                var wpadminbarHeight = Number($("#wpadminbar").outerHeight());
                if ($.isNumeric(wpadminbarHeight)) {
                    offsetHeight = wpadminbarHeight;
                }
                var headerHeight = Number($("#header").outerHeight());
                if ($.isNumeric(headerHeight)) {
                    offsetHeight = offsetHeight + headerHeight;
                }
                offsetHeight = offsetHeight + Number(tabSet.outerHeight());
                $('html, body').animate({
                    scrollTop: $("#" + target).offset().top - offsetHeight + 1 // add 1 to allow sticky background to display
                }, 500);

            });
        });
    });


    // ==============================
    // Gallery

    if ($('.fusion-gallery-image a[href*="youtu.be"]').length || $('.fusion-gallery-image a[href*="youtube.com"]').length) {

        // add class to all gallery objects with a youtube link
        $('.fusion-gallery-image a[href*="youtu.be"], .fusion-gallery-image a[href*="youtube.com"]').each(function () {
            $(this).addClass("youtube")
        });

    }


    // ==============================
    // Copyright Year

    // add the current year to the copyright
    var currentYear = new Date().getFullYear();
    $(".copyright-year").append(" " + currentYear);


    // ==============================
    // Remove extra p tags from ACF oEmbed

    // Notes: ACF oEmbed output returns string without extra p tags.
    // I suspect the p tags are added by the Avada text element that it gets added to.
    // For some reason they aren't removed via the_content filter in functions.php like
    // they are with standard WYSIWYG oEmbeds.

    $('p > script').unwrap();


});


// ==============================
// Resize iFrames

function resizeIframe(iframe) {
    iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
};
// source --> https://landoll.com/wp-content/themes/Avada-Child-Theme/frontend.js?ver=2fbb58059a042ba273dc80ae30b771a4 
jQuery(document).ready(function ($) {


    // ==============================
    // Tables

    $('table').each(function () {

        var element = $(this);

        var scrollWrapper = $('<div />', {
            'class': 'scrollable',
            'html': '<div />'
        }).insertBefore(element);

        element.data('scrollWrapper', scrollWrapper);
        element.appendTo(scrollWrapper.find('div'));

        if (element.outerWidth() > element.parent().outerWidth()) {
            element.data('scrollWrapper').addClass('has-scroll');
        }

        $(window).on('resize orientationchange', function () {
            if (element.outerWidth() > element.parent().outerWidth()) {
                element.data('scrollWrapper').addClass('has-scroll');
            } else {
                element.data('scrollWrapper').removeClass('has-scroll');
            }
        });

    });

    
});