(function (Ksh) {
  "use strict";



  if (Ksh(".range-slider-month").length) {

  }

  if (Ksh(".range-slider-count").length) {

  }

   if (Ksh(".range-slider-interest").length) {

  }

  if (Ksh("#loan-calculator").length) {

    var monthRange = document.getElementById("range-slider-month");
    var interestRange = document.getElementById("range-slider-interest");
    var countRange = document.getElementById("range-slider-count");


    var limitFieldMinMonth = document.getElementById("min-value-rangeslider-month");
    var limitFieldMaxMonth = document.getElementById("max-value-rangeslider-month");

    var limitFieldMinInterest = document.getElementById("min-value-rangeslider-interest");
    var limitFieldMaxInterest = document.getElementById("max-value-rangeslider-interest");

    var limitFieldMinCount = document.getElementById("min-value-rangeslider-count");
    var limitFieldMaxCount = document.getElementById("max-value-rangeslider-count");


    noUiSlider.create(monthRange, {
      start: 8,
      behaviour: "snap",
      step: 1,
      tooltips: [wNumb({ decimals: 0 })],
      connect: [true, false],
      range: {
        min: 1,
        max: 12
      }
    });

    noUiSlider.create(interestRange, {
      start: 4,
      behaviour: "snap",
      step: 0.1,
      tooltips: [wNumb({ decimals: 1, postfix:'%'})],
      connect: [true, false],
      range: {
        min: 1,
        max: 15
      }
    });



    noUiSlider.create(countRange, {
      start: 16000,
      step: 1000,
      tooltips: [wNumb({ decimals: 0, prefix: "Ksh" })],
      behaviour: "snap",
      connect: [true, false],
      range: {
        min: 1000,
        max: 100000
      }
    });


    monthRange.noUiSlider.on("update", function (values, handle) {
      (handle ? Ksh(limitFieldMaxMonth) : Ksh(limitFieldMinMonth)).attr(
        "value",
        values[handle]
      );
      let loanMoney = limitFieldMinCount.value;
      let interestRate = limitFieldMinInterest.value;
      let interestRatePercent = parseFloat(interestRate, 10) / 100;
      let totalPay = loanMoney * interestRatePercent + parseInt(loanMoney, 10);
      let monthlyPay = totalPay / parseInt(values[handle],10);

      Ksh("#loan-month").html(parseInt(values[handle], 10));
      Ksh("#loan-monthly-pay").html(parseInt(monthlyPay, 10));
      Ksh("#loan-total").html(parseInt(totalPay, 10));
    });

    interestRange.noUiSlider.on("update", function (values, handle) {
      (handle ? Ksh(limitFieldMaxInterest) : Ksh(limitFieldMinInterest)).attr(
        "value",
        values[handle]
      );
      let loanMoney = limitFieldMinCount.value;
      let loanMonth = limitFieldMinMonth.value;
      let interestRatePercent = parseFloat(values[handle], 10) / 100;
      let totalPay = loanMoney * interestRatePercent + parseInt(loanMoney, 10);
      let monthlyPay = totalPay / parseInt(loanMonth,10);

      Ksh("#loan-month").html(parseInt(values[handle], 10));
      Ksh("#loan-monthly-pay").html(parseInt(monthlyPay, 10));
      Ksh("#loan-total").html(parseInt(totalPay, 10));
    });

    countRange.noUiSlider.on("update", function (values, handle) {
      (handle ? Ksh(limitFieldMaxCount) : Ksh(limitFieldMinCount)).attr(
        "value",
        values[handle]
      );

      let loanMonth = limitFieldMinMonth.value;
      let interestRate = limitFieldMinInterest.value;
      let interestRatePercent = parseFloat(interestRate, 10) / 100;
      let totalPay = values[handle] * interestRatePercent + parseInt(values[handle], 10);
      let monthlyPay = totalPay / parseInt(loanMonth,10);

      Ksh("#loan-month").html(parseInt(loanMonth, 10));
      Ksh("#loan-monthly-pay").html(parseInt(monthlyPay, 10));
      Ksh("#loan-total").html(parseInt(totalPay, 10));

    });

    let loanMoney = limitFieldMinCount.value;
    let loanMonth = limitFieldMinMonth.value;
    let interestRate = limitFieldMinInterest.value;
    let interestRatePercent = parseInt(interestRate, 10) / 100;
    let totalPay = loanMoney * interestRatePercent + parseInt(loanMoney, 10);
    let monthlyPay = totalPay / parseInt(loanMonth,10);

    Ksh("#loan-month").html(parseInt(loanMonth, 10));
    Ksh("#loan-monthly-pay").html(parseInt(monthlyPay, 10));
    Ksh("#loan-total").html(parseInt(totalPay, 10));

  }

  if (Ksh(".scroll-to-target").length) {
    Ksh(".scroll-to-target").on("click", function () {
      var target = Ksh(this).attr("data-target");
      // animate
      Ksh("html, body").animate(
        {
          scrollTop: Ksh(target).offset().top
        },
        1000
      );

      return false;
    });
  }

  if (Ksh(".contact-form-validated").length) {
    Ksh(".contact-form-validated").validate({
      // initialize the plugin
      rules: {
        name: {
          required: true
        },
        email: {
          required: true,
          email: true
        },
        message: {
          required: true
        },
        subject: {
          required: true
        }
      },
      submitHandler: function (form) {
        // sending value with ajax request
        Ksh.post(Ksh(form).attr("action"), Ksh(form).serialize(), function (
          response
        ) {
          Ksh(form).parent().find(".result").append(response);
          Ksh(form).find('input[type="text"]').val("");
          Ksh(form).find('input[type="email"]').val("");
          Ksh(form).find("textarea").val("");
        });
        return false;
      }
    });
  }

  // mailchimp form
  if (Ksh(".mc-form").length) {
    Ksh(".mc-form").each(function () {
      var Self = Ksh(this);
      var mcURL = Self.data("url");
      var mcResp = Self.parent().find(".mc-form__response");

      Self.ajaxChimp({
        url: mcURL,
        callback: function (resp) {
          // appending response
          mcResp.append(function () {
            return '<p class="mc-message">' + resp.msg + "</p>";
          });
          // making things based on response
          if (resp.result === "success") {
            // Do stuff
            Self.removeClass("errored").addClass("successed");
            mcResp.removeClass("errored").addClass("successed");
            Self.find("input").val("");

            mcResp.find("p").fadeOut(10000);
          }
          if (resp.result === "error") {
            Self.removeClass("successed").addClass("errored");
            mcResp.removeClass("successed").addClass("errored");
            Self.find("input").val("");

            mcResp.find("p").fadeOut(10000);
          }
        }
      });
    });
  }

  if (Ksh(".video-popup").length) {
    Ksh(".video-popup").magnificPopup({
      disableOn: 700,
      type: "iframe",
      mainClass: "mfp-fade",
      removalDelay: 160,
      preloader: true,
      
      fixedContentPos: false
    });
  }

  if (Ksh(".img-popup").length) {
    var groups = {};
    Ksh(".img-popup").each(function () {
      var id = parseInt(Ksh(this).attr("data-group"), 10);

      if (!groups[id]) {
        groups[id] = [];
      }

      groups[id].push(this);
    });

    Ksh.each(groups, function () {
      Ksh(this).magnificPopup({
        type: "image",
        closeOnContentClick: true,
        closeBtnInside: false,
        gallery: {
          enabled: true
        }
      });
    });
  }

  function dynamicCurrentMenuClass(selector) {
    let FileName = window.location.href.split("/").reverse()[0];

    selector.find("li").each(function () {
      let anchor = Ksh(this).find("a");
      if (Ksh(anchor).attr("href") == FileName) {
        Ksh(this).addClass("current");
      }
    });
    // if any li has .current elmnt add class
    selector.children("li").each(function () {
      if (Ksh(this).find(".current").length) {
        Ksh(this).addClass("current");
      }
    });
    // if no file name return
    if ("" == FileName) {
      selector.find("li").eq(0).addClass("current");
    }
  }
  if (Ksh(".main-menu__list").length) {
    // dynamic current class
    let mainNavUL = Ksh(".main-menu__list");
    dynamicCurrentMenuClass(mainNavUL);
  }

  if (Ksh(".mobile-nav__container").length) {
    let navContent = document.querySelector(".main-menu").innerHTML;
    let mobileNavContainer = document.querySelector(".mobile-nav__container");
    mobileNavContainer.innerHTML = navContent;
  }
  if (Ksh(".sticky-header__content").length) {
    let navContent = document.querySelector(".main-menu").innerHTML;
    let mobileNavContainer = document.querySelector(".sticky-header__content");
    mobileNavContainer.innerHTML = navContent;
  }

  if (Ksh(".mobile-nav__container .main-menu__list").length) {
    let dropdownAnchor = Ksh(
      ".mobile-nav__container .main-menu__list .dropdown > a"
    );
    dropdownAnchor.each(function () {
      let self = Ksh(this);
      let toggleBtn = document.createElement("BUTTON");
      toggleBtn.setAttribute("aria-label", "dropdown toggler");
      toggleBtn.innerHTML = "<i class='fa fa-angle-down'></i>";
      self.append(function () {
        return toggleBtn;
      });
      self.find("button").on("click", function (e) {
        e.preventDefault();
        let self = Ksh(this);
        self.toggleClass("expanded");
        self.parent().toggleClass("expanded");
        self.parent().parent().children("ul").slideToggle();
      });
    });
  }

  if (Ksh(".mobile-nav__toggler").length) {
    Ksh(".mobile-nav__toggler").on("click", function (e) {
      e.preventDefault();
      Ksh(".mobile-nav__wrapper").toggleClass("expanded");
    });
  }

  if (Ksh(".search-toggler").length) {
    Ksh(".search-toggler").on("click", function (e) {
      e.preventDefault();
      Ksh(".search-popup").toggleClass("active");
    });
  }
  if (Ksh(".odometer").length) {
    Ksh(".odometer").appear(function (e) {
      var odo = Ksh(".odometer");
      odo.each(function () {
        var countNumber = Ksh(this).attr("data-count");
        Ksh(this).html(countNumber);
      });
    });
  }

  if (Ksh(".wow").length) {
    var wow = new WOW({
      boxClass: "wow", // animated element css class (default is wow)
      animateClass: "animated", // animation css class (default is animated)
      mobile: true, // trigger animations on mobile devices (default is true)
      live: true // act on asynchronously loaded content (default is true)
    });
    wow.init();
  }

  if (Ksh("#donate-amount__predefined").length) {
    let donateInput = Ksh("#donate-amount");
    Ksh("#donate-amount__predefined")
      .find("li")
      .on("click", function (e) {
        e.preventDefault();
        let amount = Ksh(this).find("a").text();
        donateInput.val(amount);
        Ksh("#donate-amount__predefined").find("li").removeClass("active");
        Ksh(this).addClass("active");
      });
  }

  Ksh("#accordion .collapse").on("shown.bs.collapse", function () {
    Ksh(this).prev().addClass("active");
    Ksh(this).prev().parent().addClass("active");
  });

  Ksh("#accordion .collapse").on("hidden.bs.collapse", function () {
    Ksh(this).prev().removeClass("active");
    Ksh(this).prev().parent().removeClass("active");
  });

  Ksh("#accordion").on("hide.bs.collapse show.bs.collapse", (e) => {
    Ksh(e.target).prev().find("i:last-child").toggleClass("fa-plus fa-minus");
  });

  // window load event

  Ksh(window).on("load", function () {
    if (Ksh(".preloader").length) {
      Ksh(".preloader").fadeOut();
    }

    // swiper slider
    const swiperElm = document.querySelectorAll(".thm-swiper__slider");
    swiperElm.forEach(function (swiperelm) {
      const swiperOptions = JSON.parse(swiperelm.dataset.swiperOptions);
      let thmSwiperSlider = new Swiper(swiperelm, swiperOptions);
    });
  });

  // window load event

  Ksh(window).on("scroll", function () {
    if (Ksh(".stricked-menu").length) {
      var headerScrollPos = 130;
      var stricky = Ksh(".stricked-menu");
      if (Ksh(window).scrollTop() > headerScrollPos) {
        stricky.addClass("stricky-fixed");
      } else if (Ksh(this).scrollTop() <= headerScrollPos) {
        stricky.removeClass("stricky-fixed");
      }
    }
    if (Ksh(".scroll-to-top").length) {
      var strickyScrollPos = 100;
      if (Ksh(window).scrollTop() > strickyScrollPos) {
        Ksh(".scroll-to-top").fadeIn(500);
      } else if (Ksh(this).scrollTop() <= strickyScrollPos) {
        Ksh(".scroll-to-top").fadeOut(500);
      }
    }
  });
})(jQuery);
