(function ($, root, undefined) {
  $(function () {
    "use strict";

    $(document).ready(function () {
      const vendorFix = () => {
        $("body").find(".vendor-navbar").addClass("vendor-fix");
        if ($(".vendor-navbar").hasClass("vendor-fix")) {
          $(window).scroll(function () {
            if ($(this).scrollTop() >= $(".vendor-slider").offset().top) {
              $(".vendor-fix").addClass("header-fixed");
            } else {
              $(".vendor-fix").removeClass("header-fixed");
            }
          });
        } else {
          $(window).scroll(function () {
            if ($(this).scrollTop() > 100) {
              $("header").addClass("header-fixed");
              $("main").css({
                "margin-top": $("header").outerHeight(),
              });
              $(".fix-col").removeClass("col-xl-9");
              $(".fix-col").addClass("col-xl-7");
            } else {
              $("header").removeClass("header-fixed");
              $("main").css({
                "margin-top": 0,
              });
              $(".fix-col").addClass("col-xl-9");
              $(".fix-col").removeClass("col-xl-7");
            }
          });
        }
      };

      $("a[href='#']").click(function (e) {
        e.preventDefault();
      });
      $("#toTop").click(function () {
        $("html, body").animate({ scrollTop: 0 }, 50);
      });

      if ($("body").magnificPopup != undefined) {
        $(".popup-youtube, .popup-vimeo, .popup-gmaps").magnificPopup({
          // disableOn: 700,
          type: "iframe",
          mainClass: "mfp-fade",
          removalDelay: 160,
          preloader: false,

          fixedContentPos: false,
          zoom: {
            enabled: true,
            duration: 300,
            easing: "ease-in-out",
            opener: function (openerElement) {
              return openerElement.is("img")
                ? openerElement
                : openerElement.find("img");
            },
          },
        });
      }

      const eHFix = () => {
        let mh = $(".eH-area");
        mh.each(function () {
          $(this).find(".eH").matchHeight();
          $(this).find(".eH2").matchHeight();
          $(this).find(".eH3").matchHeight();
        });
      };

      $("#best-sell-tab button").on("click", function () {
        eHFix();
      });

      $(".search-page").on("click", function () {
        $(".offcanvas__search input").focus();
      });

      const productSlider = () => {
        if ($(".swiper-gallery-activate").length == 1) {
          var slider = new Swiper(".gallery-slider", {
            slidesPerView: 1,
            centeredSlides: true,
            loop: true,
            loopedSlides: 6,
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
          var thumbs = new Swiper(".gallery-thumbs", {
            slidesPerView: "auto",
            spaceBetween: 10,
            centeredSlides: true,
            loop: true,
            slideToClickedSlide: true,
          });
          slider.controller.control = thumbs;
          thumbs.controller.control = slider;
        }
        if ($(".swiper-gallery-activate-rtl").length == 1) {
          var slider = new Swiper(".gallery-slider-rtl", {
            slidesPerView: 1,
            centeredSlides: true,
            loop: true,
            // dir: rtl,
            loopedSlides: 6,
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
          var thumbs = new Swiper(".gallery-thumbs", {
            slidesPerView: "auto",
            spaceBetween: 10,
            centeredSlides: true,
            loop: true,
            slideToClickedSlide: true,
          });
          slider.controller.control = thumbs;
          thumbs.controller.control = slider;
        }
      };

      $(".radio-area")
        .find("[type='radio']")
        .on("click", function () {
          $(".radio-area [type='radio']").prop("checked", false);
          $(this).prop("checked", true);
        });

      const cartquantity = () => {
        $(".cart-quantity-plus").on("click", function () {
          var $qty = $(this)
            .closest(".cart-quantity-area")
            .find(".cart-quantity-input");
          var currentVal = parseInt($qty.val());
          if (!isNaN(currentVal)) {
            $qty.val(currentVal + 1);
          }
        });
        $(".cart-quantity-minus").on("click", function () {
          var $qty = $(this)
            .closest(".cart-quantity-area")
            .find(".cart-quantity-input");
          var currentVal = parseInt($qty.val());
          if (!isNaN(currentVal) && currentVal > 1) {
            $qty.val(currentVal - 1);
          }
        });
      };

      if ($("body").owlCarousel != undefined) {
        $(".content-carousel").owlCarousel({
          loop: true,
          margin: 10,
          nav: false,
          dots: true,
          responsive: {
            0: {
              items: 1,
            },
            600: {
              items: 3,
            },
            1000: {
              items: 5,
            },
          },
        });
        $(".content-carousel-rtl").owlCarousel({
          loop: true,
          margin: 10,
          nav: false,
          dots: true,
          rtl:true,
          responsive: {
            0: {
              items: 1,
            },
            600: {
              items: 3,
            },
            1000: {
              items: 5,
            },
          },
        });
      }

      if ($("body").magnificPopup != undefined) {
        $(".popup-rtl").magnificPopup({
          type: "image",
          mainClass: 'mfp-with-zoom',
          rtl: true,
          gallery: {
            enabled: true, 
            preload: [0,2],
            navigateByImgClick: true,
            arrowMarkup: '<button title="%title%" type="button" class="mfp-arrow mfp-arrow-%dir%"></button>',
            tPrev: 'Previous (Left arrow key)',
            tNext: 'Next (Right arrow key)',
            tCounter: '<span class="mfp-counter">%curr% من %total%</span>'
          },
          zoom: {
            enabled: true,
            duration: 300,
            easing: 'ease-in-out',
            opener: function(openerElement) {
              return openerElement.is('img') ? openerElement : openerElement.find('img');
            }
          }
        });
        $(".popup").magnificPopup({
          type: "image",
          mainClass: 'mfp-with-zoom',
          gallery: {
            enabled: true, 
            preload: [0,2],
            navigateByImgClick: true,
            arrowMarkup: '<button title="%title%" type="button" class="mfp-arrow mfp-arrow-%dir%"></button>',
            tPrev: 'Previous (Left arrow key)',
            tNext: 'Next (Right arrow key)',
            tCounter: '<span class="mfp-counter">%curr% of %total%</span>'
          },
          zoom: {
            enabled: true,
            duration: 300,
            easing: 'ease-in-out',
            opener: function(openerElement) {
              return openerElement.is('img') ? openerElement : openerElement.find('img');
            }
          }
        });
      }



      $(window).scroll(() => {
        if($("header").hasClass("header-fixed")){
          $("main").css({
            "margin-top": $(".header__bottom").outerHeight(),
          })
        }
      });

      eHFix();
      productSlider();
      // vendorFix();
      cartquantity();
    });
  });
})(jQuery, this);
