"use strict";
(function ($) {
  // ==========================================
  //      Start Document Ready function
  // ==========================================
  $(document).ready(function () {
    // ====================== SEO Tool Search JS Start ====================
    $(" .seo-tool-search").each(function (index, form) {
      var searchInput = $(form).find('input[type="text"]');
      var searchList = $(form).find(".seo-tool-search-list");
      var seoToolCards = $(".seo-tool-section .seo-tool-card").not(
        ".seo-tool-card-btn"
      );

      var len = 0;
      var clickLink = 0;
      var search = null;
      var process = false;

      searchInput.on("input", function () {
        var searchValue = $(this).val().toLowerCase();

        if (searchValue.length) {
          searchList.removeClass("d-none");
          searchList.html("");
        } else {
          searchList.addClass("d-none");
          return;
        }

        var matches = seoToolCards
          .filter(function (index, item) {
            return $(item)
              .find(":not(.seo-tool-card__ribbon)")
              .text()
              .trim()
              .toLowerCase()
              .indexOf(searchValue) >= 0
              ? item
              : null;
          })
          .sort();

        if (matches.length) {
          // Matches Found

          console.log(matches);

          matches.each(function (index, item) {
            var itemIconAlt = $(item).find("img").attr("alt");
            var itemIconURL = $(item).find("img").attr("src");
            var itemHref = $(item).attr("href");
            var itemText = $(item)
              .find(":not(.seo-tool-card__ribbon)")
              .text()
              .trim();
            var itemRibbon = $(item).data("ribbon");
            searchList.append(`
              <li class="seo-tool-search-list__item">
                  <a href="${itemHref}" class="seo-tool-search-list__link" ${itemRibbon ? 'data-ribbon="' + itemRibbon + '"' : ""}>
                      <img src="${itemIconURL}" alt="${itemIconAlt}">
                      <span>${itemText}</span>
                  </a>
              </li>
            `);
          });
        } else {
          // Matches Not Found
          searchList.append(`
            <li class="seo-tool-search-list__item">
                <span class="seo-tool-search-list__text">No results found</span>
            </li>
          `);
        }
      });

      searchInput.on("keydown", function (e) {
        var searchItemsLength = searchList.find("li").length;

        if (search != $(this).val() && process) {
          len = 0;
          clickLink = 0;
          searchList.find(`li:eq(${len}) a`).focus();
          $(this).focus();
        }

        // Down Key
        if (e.keyCode == 40 && searchItemsLength) {
          process = true;
          var contra = false;
          if (len < clickLink && clickLink < searchItemsLength) {
            len += 2;
          }

          searchList.find("li.active").removeClass("active");
          searchList.find(`li:eq(${len})`).addClass("active");
          searchList.find(`li:eq(${len}) a`).focus();
          $(this).focus();

          clickLink = len;
          if (!searchList.find(`li:eq(${clickLink}) a`).length) {
            searchList.find(`li:eq(${len})`).addClass("active");
          }

          len += 1;
          if (searchItemsLength == Math.abs(clickLink)) {
            len = 0;
          }

          // Up Key
        } else if (e.keyCode == 38 && searchItemsLength) {
          process = true;
          if (len > clickLink && len != 0) {
            len -= 2;
          }

          searchList.find("li.active").removeClass("active");
          searchList.find(`li:eq(${len})`).addClass("active");
          searchList.find(`li:eq(${len}) a`).focus();
          $(this).focus();

          clickLink = len;
          if (!searchList.find(`li:eq(${clickLink}) a`).length) {
            searchList.find(`li:eq(${len})`).addClass("active");
          }

          len -= 1;
          if (searchItemsLength == Math.abs(clickLink)) {
            len = 0;
          }
          // Enter Key
        } else if (e.keyCode == 13) {
          e.preventDefault();
          if (searchList.find(`li:eq(${clickLink}) a`).length && process) {
            searchList.find(`li:eq(${clickLink}) a`)[0].click();
          }

          // Retry Key
        } else if (e.keyCode == 8) {
          len = 0;
          clickLink = 0;
          searchList.find(`li:eq(${len}) a`).focus();
          $(this).focus();
        }

        search = $(this).val();
      });
    });
    // ====================== SEO Tool Search JS End ======================

    // ============== Header Hide Click On Body Js Start ========
    $(".header-button").on("click", function () {
      $(".body-overlay").toggleClass("show");
      $(".header").toggleClass("add-bg");
    });
    $(".body-overlay").on("click", function () {
      $(".header-button").trigger("click");
      $(this).removeClass("show");
      $(".header").removeClass("add-bg");
    });
    // =============== Header Hide Click On Body Js End =========

    // ========================== Header Hide Scroll Bar Js Start =====================
    $(".navbar-toggler.header-button").on("click", function () {
      $("body").toggleClass("scroll-hide-sm");
    });
    $(".body-overlay").on("click", function () {
      $("body").removeClass("scroll-hide-sm");
    });
    // ========================== Header Hide Scroll Bar Js End =====================

    // ========================== Small Device Header Menu On Click Dropdown menu collapse Stop Js Start =====================
    $(".dropdown-item").on("click", function () {
      $(this).closest(".dropdown-menu").addClass("d-block");
    });
    // ========================== Small Device Header Menu On Click Dropdown menu collapse Stop Js End =====================

    // ========================== Add Attribute For Bg Image Js Start =====================
    $(".bg-img").css("background", function () {
      var bg = "url(" + $(this).data("background-image") + ")";
      return bg;
    });
    // ========================== Add Attribute For Bg Image Js End =====================

    // ========================== add active class to ul>li top Active current page Js Start =====================
    function dynamicActiveMenuClass(selector) {
      let fileName = window.location.pathname.split("/").reverse()[0];
      selector.find("li").each(function () {
        let anchor = $(this).find("a");
        if ($(anchor).attr("href") == fileName) {
          $(this).addClass("active");
        }
      });
      // if any li has active element add class
      selector.children("li").each(function () {
        if ($(this).find(".active").length) {
          $(this).addClass("active");
        }
      });
      // if no file name return
      if ("" == fileName) {
        selector.find("li").eq(0).addClass("active");
      }
    }
    if ($("ul.sidebar-menu-list").length) {
      dynamicActiveMenuClass($("ul.sidebar-menu-list"));
    }
    if ($("ul").length) {
      dynamicActiveMenuClass($("ul"));
    }
    // ========================== add active class to ul>li top Active current page Js End =====================

    // ================== Password Show Hide Js Start ==========
    $(".toggle-password").on("click", function () {
      $(this).toggleClass("fa fa-eye");
      var input = $(this).siblings("input");
      if (input.attr("type") == "password") {
        input.attr("type", "text");
      } else {
        input.attr("type", "password");
      }
    });
    // =============== Password Show Hide Js End =================

    // ========================= Slick Slider Js Start ==============
    $(".testimonial-slider").slick({
      slidesToShow: 3,
      slidesToScroll: 1,
      autoplay: false,
      autoplaySpeed: 2000,
      speed: 1500,
      dots: true,
      pauseOnHover: true,
      arrows: false,
      prevArrow:
        '<button type="button" class="slick-prev"><i class="fas fa-long-arrow-alt-left"></i></button>',
      nextArrow:
        '<button type="button" class="slick-next"><i class="fas fa-long-arrow-alt-right"></i></button>',
      responsive: [
        {
          breakpoint: 1199,
          settings: {
            arrows: false,
            slidesToShow: 2,
            dots: true,
          },
        },
        {
          breakpoint: 991,
          settings: {
            arrows: false,
            slidesToShow: 2,
          },
        },
        {
          breakpoint: 464,
          settings: {
            arrows: false,
            slidesToShow: 1,
          },
        },
      ],
    });
    // ========================= Slick Slider Js End ===================

    // ========================= Client Slider Js Start ===============
    // $('.client-slider').slick({
    // slidesToShow: 6,
    // slidesToScroll: 1,
    // autoplay: true,
    // autoplaySpeed: 1000,
    // pauseOnHover: true,
    // speed: 2000 ,
    // dots: false,
    // arrows: false,
    // prevArrow: '<button type="button" class="slick-prev"><i class="fas fa-long-arrow-alt-left"></i></button>',
    // nextArrow: '<button type="button" class="slick-next"><i class="fas fa-long-arrow-alt-right"></i></button>',
    // responsive: [
    //     {
    //       breakpoint: 1199,
    //       settings: {
    //         slidesToShow:6,
    //       }
    //     },
    //     {
    //       breakpoint: 991,
    //       settings: {
    //         slidesToShow: 5
    //       }
    //     },
    //     {
    //       breakpoint: 767,
    //       settings: {
    //         slidesToShow: 4
    //       }
    //     },
    //     {
    //       breakpoint: 400,
    //       settings: {
    //         slidesToShow: 3
    //       }
    //     }
    //   ]
    // });
    // ========================= Client Slider Js End ===================

    // ================== Sidebar Menu Js Start ===============
    // Sidebar Dropdown Menu Start
    $(".has-dropdown > a").click(function () {
      $(".sidebar-submenu").slideUp(200);
      if ($(this).parent().hasClass("active")) {
        $(".has-dropdown").removeClass("active");
        $(this).parent().removeClass("active");
      } else {
        $(".has-dropdown").removeClass("active");
        $(this).next(".sidebar-submenu").slideDown(200);
        $(this).parent().addClass("active");
      }
    });
    // Sidebar Dropdown Menu End
    // Sidebar Icon & Overlay js
    $(".navigation-bar").on("click", function () {
      $(".sidebar-menu").addClass("show-sidebar");
      $(".sidebar-overlay").addClass("show");
    });
    $(".sidebar-menu__close, .sidebar-overlay").on("click", function () {
      $(".sidebar-menu").removeClass("show-sidebar");
      $(".sidebar-overlay").removeClass("show");
    });
    // Sidebar Icon & Overlay js
    // ===================== Sidebar Menu Js End =================

    // ==================== Dashboard User Profile Dropdown Start ==================
    $(".user-info__button").on("click", function () {
      $(".user-info-dropdown").toggleClass("show");
    });
    $(".user-info__button").attr("tabindex", -1).focus();

    $(".user-info__button").on("focusout", function () {
      $(".user-info-dropdown").removeClass("show");
    });
    // ==================== Dashboard User Profile Dropdown End ==================

    // ========================= Odometer Counter Up Js End ==========
    $(".counterup-item").each(function () {
      $(this).isInViewport(function (status) {
        if (status === "entered") {
          for (
            var i = 0;
            i < document.querySelectorAll(".odometer").length;
            i++
          ) {
            var el = document.querySelectorAll(".odometer")[i];
            el.innerHTML = el.getAttribute("data-odometer-final");
          }
        }
      });
    });
    // ========================= Odometer Up Counter Js End =====================
  });
  // ==========================================
  //      End Document Ready function
  // ==========================================

  // ========================= Preloader Js Start =====================
  $(window).on("load", function () {
    $(".preloader").fadeOut();
  });
  // ========================= Preloader Js End=====================

  //============================ Scroll To Top Icon Js Start =====================
  var scrollTopBtn = $(".scroll-top");

  scrollTopBtn.on("click", function (e) {
    e.preventDefault();
    $("html, body").animate({ scrollTop: 0 }, "300");
  });

  $(window).on("scroll", function () {
    if ($(window).scrollTop() > 300) {
      scrollTopBtn.addClass("show");
    } else {
      scrollTopBtn.removeClass("show");
    }
  });

  // ========================= Header Sticky Js Start =====================
  $(window).on("scroll", function () {
    if ($(window).scrollTop() >= 300) {
      $(".header").addClass("fixed-header");
    } else {
      $(".header").removeClass("fixed-header");
    }
  });
  // ========================= Header Sticky Js End=====================
})(jQuery);
