/* ============ Main JS ============ */

(function($) {
    "use strict";

    var windowOn = $(window);

    // 01. PreLoader Js
    $(window).on("load", function(event) {
        $(".preloader").delay(500).fadeOut(500);
    });

    $(document).ready(function() {

        var headerArea = $(".sticky-active .primary-header"),
            headerClone = headerArea.clone();

        function menuSticky(w) {
            if (w.matches) {
                $(".header").after('<div class="sticky-header-wrap"></div>');
                $(".sticky-header-wrap").html(headerClone);
                $(window).on("scroll", function() {
                    var headerSelector = $(".sticky-header-wrap");
                    var scroll = $(window).scrollTop();
                    if (scroll >= 110) {
                        headerSelector.addClass("fixed");
                    } else {
                        headerSelector.removeClass("fixed");
                    }
                });
            }
        }

        var minWidth = window.matchMedia("(min-width: 992px)");
        if ($(".header").hasClass("sticky-active")) {
            menuSticky(minWidth);
        } else {
            windowOn.on("scroll", function() {
                var scroll = $(window).scrollTop();
                if (scroll < 100) {
                    $(".header").removeClass("header__sticky");
                } else {
                    $(".header").addClass("header__sticky");
                }
            });
        }

        //Mobile Menu Js
        $(".mobile-menu-items").meanmenu({
            meanMenuContainer: ".side-menu-wrap",
            meanScreenWidth: "991",
            meanMenuCloseSize: "30px",
            meanRemoveAttrs: true,
            meanExpand: ['<i class="fa-solid fa-caret-down"></i>'],
        });

        // Mobile Sidemenu
        $(".mobile-side-menu-toggle").on("click", function() {
            $(".mobile-side-menu, .mobile-side-menu-overlay").toggleClass("is-open");
        });

        $(".mobile-side-menu-close, .mobile-side-menu-overlay").on("click", function() {
            $(".mobile-side-menu, .mobile-side-menu-overlay").removeClass("is-open");
        });


        // Menu last class
        $('.default-deader-style .primary-header .mobile-menu-items.sub-menu > ul > li').slice(-1).addClass('menu-last');


        // Popup Search Box
        $(function() {
            $("#popup-search-box").removeClass("toggled");

            $(".dl-search-icon").on("click", function(e) {
                e.stopPropagation();
                $("#popup-search-box").toggleClass("toggled");
                $("#popup-search").focus();
            });

            $("#popup-search-box input").on("click", function(e) {
                e.stopPropagation();
            });

            $("#popup-search-box, body").on("click", function() {
                $("#popup-search-box").removeClass("toggled");
            });
        });

        // Popup Sidebox
        function sideBox() {
            $("body").removeClass("open-sidebar");
            $(document).on("click", ".sidebar-trigger", function(e) {
                e.preventDefault();
                $("body").toggleClass("open-sidebar");
            });
            $(document).on("click", ".sidebar-trigger.close, #sidebar-overlay", function(e) {
                e.preventDefault();
                $("body.open-sidebar").removeClass("open-sidebar");
            });
        }

        sideBox();


        // Magnific Popup Img View
        if ($(".popup-image").length) {
            $('.popup-image').magnificPopup({
                type: 'image',
                gallery: {
                    enabled: true
                }
            });
        }

        /* Magnific Popup Video View */
        if ($(".popup-video").length) {
            $('.popup-video').magnificPopup({
                type: 'iframe',
            });
        }


        // Venobox Video
        new VenoBox({
            selector: ".video-popup, .img-popup",
            bgcolor: "transparent",
            numeration: true,
            infinigall: true,
            spinner: "plane",
        });

        // Data Background
        $("[data-background").each(function() {
            $(this).css("background-image", "url( " + $(this).attr("data-background") + "  )");
        });

        /* Odometer */
        $(".odometer").waypoint(
            function() {
                var odo = $(".odometer");
                odo.each(function() {
                    var countNumber = $(this).attr("data-count");
                    $(this).html(countNumber);
                });
            }, {
                offset: "80%",
                triggerOnce: true,
            }
        );

        // Wow JS Active
        new WOW().init();

        // Isotop
        $(".filter-items").imagesLoaded(function() {
            // Add isotope click function
            $(".project-filter li").on("click", function() {
                $(".project-filter li").removeClass("active");
                $(this).addClass("active");

                var selector = $(this).attr("data-filter");
                $(".filter-items").isotope({
                    filter: selector,
                    animationOptions: {
                        duration: 750,
                        easing: "linear",
                        queue: false,
                    },
                });
                return false;
            });

            $(".filter-items").isotope({
                itemSelector: ".single-item",
                layoutMode: "fitRows",
                fitRows: {
                    gutter: 0,
                },
            });
        });

        // Price range slider
        var priceRange = $("#price-range"),
            priceOutput = $("#price-output span");
        priceOutput.html(priceRange.val());
        priceRange.on("change input", function() {
            priceOutput.html($(this).val());
        });


        // blog gallery activation
        if (jQuery(".blog_gallery_active").length > 0) {
            let ablogimgactive = new Swiper('.blog_gallery_active', {
                slidesPerView: 1,
                spaceBetween: 30,
                autoplay: true,
                // direction: 'vertical',
                loop: true,

                // If we need pagination
                pagination: {
                    el: '.swiper-pagination',
                    clickable: true,
                },

                // Navigation arrows
                navigation: {
                    nextEl: '.postbox-slider-button-next',
                    prevEl: '.postbox-slider-button-prev',
                },

                // And if we need scrollbar
                scrollbar: {
                    el: '.swiper-scrollbar',
                    dynamicBullets: true,
                },
                breakpoints: {
                    640: {
                        slidesPerView: 1,
                    },
                    768: {
                        slidesPerView: 1,
                    },
                    1024: {
                        slidesPerView: 1,
                    },
                }
            });
        }


        // Nice Select Js
        $('.sidebar__single select, .footer-widget select, .postbox__item select, .woocommerce-ordering select, .variations_form select', ).niceSelect();


        // Scroll To Top
        var scrollTop = $("#scrollup");
        $(window).on('scroll', function() {
            var topPos = $(this).scrollTop();
            if (topPos > 100) {
                $('#scrollup').removeClass('hide');
                $('#scrollup').addClass('show');

            } else {
                $('#scrollup').removeClass('show');
                $('#scrollup').addClass('hide');
            }
        });

        $(scrollTop).on("click", function() {
            $('html, body').animate({
                scrollTop: 0
            }, 0);
            return false;
        });
    });

    function aboutActive() {
        // Data Background
        $("[data-background").each(function() {
            $(this).css("background-image", "url( " + $(this).attr("data-background") + "  )");
        });

        // Venobox Video
        new VenoBox({
            selector: ".video-popup, .img-popup",
            bgcolor: "transparent",
            numeration: true,
            infinigall: true,
            spinner: "plane",
        });

        /* Magnific Popup Video View */
        if ($(".popup-video").length) {
            $('.popup-video').magnificPopup({
                type: 'iframe',
            });
        }
    }

    function testimonialActive(){
        // Testimonial Carousel
        var swiperTesti = new Swiper(".testimonial-carousel", {
            slidesPerView: 3,
            spaceBetween: 25,
            slidesPerGroup: 1,
            loop: true,
            autoplay: true,
            grabCursor: true,
            rtl: rtl_setting,
            speed: 600,
            pagination: {
                el: ".swiper-pagination",
                clickable: true,
            },
            breakpoints: {
                320: {
                    slidesPerView: 1,
                    slidesPerGroup: 1,
                    spaceBetween: 25,
                },
                767: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                    spaceBetween: 30,
                },
                1024: {
                    slidesPerView: 3,
                    slidesPerGroup: 1,
                },
            },
        });

        // Testimonial Carousel
        var swiperTesti = new Swiper(".testimonial-carousel-2", {
            slidesPerView: 2,
            spaceBetween: 25,
            slidesPerGroup: 1,
            loop: true,
            rtl: rtl_setting,
            autoplay: true,
            grabCursor: true,
            speed: 600,
            pagination: {
                el: ".swiper-pagination",
                clickable: true,
            },
            navigation: {
                nextEl: ".testimonial-carousel-2 .swiper-next",
                prevEl: ".testimonial-carousel-2 .swiper-prev",
            },
            breakpoints: {
                320: {
                    slidesPerView: 1,
                    slidesPerGroup: 1,
                    spaceBetween: 25,
                },
                767: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                    spaceBetween: 30,
                },
                1024: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                },
            },
        });
    }

    function serviceActive(){

        $("[data-background").each(function() {
            $(this).css("background-image", "url( " + $(this).attr("data-background") + "  )");
        });

        // Venobox Video
        new VenoBox({
            selector: ".video-popup, .img-popup",
            bgcolor: "transparent",
            numeration: true,
            infinigall: true,
            spinner: "plane",
        });

        // Service Carousel
        var swiperService = new Swiper(".service-carousel", {
            slidesPerView: 2,
            spaceBetween: 25,
            slidesPerGroup: 1,
            loop: true,
            autoplay: false,
            grabCursor: true,
            rtl: rtl_setting,
            speed: 600,
            pagination: {
                el: ".swiper-pagination",
                clickable: true,
            },
            navigation: {
                nextEl: ".service-section .swiper-prev",
                prevEl: ".service-section .swiper-next",
            },
            breakpoints: {
                320: {
                    slidesPerView: 1,
                    slidesPerGroup: 1,
                    spaceBetween: 25,
                },
                767: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                    spaceBetween: 30,
                },
                1024: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                },
            },
        });

    }

    function projectActive(){
        // Project Carousel 01
        var swiperProject = new Swiper(".project-carousel", {
            slidesPerView: 3,
            spaceBetween: 25,
            slidesPerGroup: 1,
            loop: true,
            autoplay: true,
            rtl: rtl_setting,
            grabCursor: true,
            speed: 600,
            pagination: {
                el: ".swiper-pagination",
                clickable: true,
            },
            navigation: {
                nextEl: ".project-section .swiper-prev",
                prevEl: ".project-section .swiper-next",
            },
            breakpoints: {
                320: {
                    slidesPerView: 1,
                    slidesPerGroup: 1,
                    spaceBetween: 25,
                },
                767: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                    spaceBetween: 30,
                },
                1024: {
                    slidesPerView: 3,
                    slidesPerGroup: 1,
                },
            },
        });


        // Project Carousel 02
        var swiperProject = new Swiper(".project-carousel-2", {
            slidesPerView: 3,
            spaceBetween: 25,
            slidesPerGroup: 1,
            loop: true,
            autoplay: true,
            grabCursor: true,
            rtl: rtl_setting,
            speed: 600,
            pagination: {
                el: ".swiper-pagination",
                clickable: true,
            },
            navigation: {
                nextEl: ".project-carousel-wrap .swiper-next",
                prevEl: ".project-carousel-wrap .swiper-prev",
            },
            breakpoints: {
                320: {
                    slidesPerView: 1,
                    slidesPerGroup: 1,
                    spaceBetween: 25,
                },
                767: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                    spaceBetween: 30,
                },
                1024: {
                    slidesPerView: 3,
                    slidesPerGroup: 1,
                },
            },
        });
    }

    function sponsorActive(){

        // Sponsor Carousel
        var swiperSponsor = new Swiper(".sponsor-carousel", {
            slidesPerView: 45,
            spaceBetween: 50,
            slidesPerGroup: 1,
            loop: true,
            autoplay: true,
            rtl: rtl_setting,
            grabCursor: true,
            speed: 400,
            breakpoints: {
                320: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                    spaceBetween: 25,
                },
                767: {
                    slidesPerView: 3,
                    slidesPerGroup: 1,
                    spaceBetween: 30,
                },
                1024: {
                    slidesPerView: 5,
                    slidesPerGroup: 1,
                },
            },
        });

        // Sponsor Carousel
        var swiperSponsor = new Swiper(".sponsor-carousel-2", {
            slidesPerView: 45,
            spaceBetween: 50,
            slidesPerGroup: 1,
            loop: true,
            autoplay: true,
            rtl: rtl_setting,
            grabCursor: true,
            rtl: rtl_setting,
            speed: 400,
            breakpoints: {
                320: {
                    slidesPerView: 2,
                    slidesPerGroup: 1,
                    spaceBetween: 25,
                },
                767: {
                    slidesPerView: 3,
                    slidesPerGroup: 1,
                    spaceBetween: 30,
                },
                1024: {
                    slidesPerView: 4,
                    slidesPerGroup: 1,
                },
            },
        });

    }

    function howitworkActive(){
        $("[data-background").each(function() {
            $(this).css("background-image", "url( " + $(this).attr("data-background") + "  )");
        });
    }

    function workprocessActive(){
        $("[data-background").each(function() {
            $(this).css("background-image", "url( " + $(this).attr("data-background") + "  )");
        });
    }


    // Activate rtl slider
    let rtl_setting = $('body').hasClass("rtl") ? true : false;


    // Product Slider Js
    var produc = new Swiper('.related-product-active', {
        slidesPerView: 4,
        spaceBetween: 30,
        slideToClickedSlide: true,
        loop: true,
        rtl: rtl_setting,
        autoplay: true,
        autoplay: {
            delay: 3000,
        },
        pagination: {
            el: ".product-pagination",
            clickable: true,
            renderBullet: function (index, className) {
                return '<span class="' + className + '">' + '<button>' + (index + 1) + '</button>' + "</span>";
            },
        },
        navigation: {
            nextEl: ".product-button-next",
            prevEl: ".product-button-prev",
        },
        speed: 1000,
        breakpoints: {
            '1400': {
                slidesPerView: 4,
            },
            '1200': {
                slidesPerView: 4,
            },
            '992': {
                slidesPerView: 3,
            },
            '768': {
                slidesPerView: 2,
            },
            '480': {
                slidesPerView: 1,
            },
            '0': {
                slidesPerView: 1,
            },
        },
    });


    // Elementor Slider Activation
    $(window).on('elementor/frontend/init', function() {
        elementorFrontend.hooks.addAction('frontend/element_ready/about.default', aboutActive);
        elementorFrontend.hooks.addAction('frontend/element_ready/logistec_testimonial_slider.default', testimonialActive);
        elementorFrontend.hooks.addAction('frontend/element_ready/logistec_services.default', serviceActive);
        elementorFrontend.hooks.addAction('frontend/element_ready/logistec_portfolio_slider.default', projectActive);
        elementorFrontend.hooks.addAction('frontend/element_ready/logistec_brand_slider.default', sponsorActive);
        elementorFrontend.hooks.addAction('frontend/element_ready/logistec_how_it_work.default', howitworkActive);
        elementorFrontend.hooks.addAction('frontend/element_ready/logistec_work_process.default', workprocessActive);
    });


})(jQuery);