/* ===================================================================
    
    Author          : Valid Theme
    Template Name   : Healdi - Medical & Health Template
    Version         : 1.0
    
* ================================================================= */

(function ($) {
  "use strict";

  $(document).ready(function () {
    /* ==================================================
            # Wow Init
         ===============================================*/
    var wow = new WOW({
      boxClass: "wow", // animated element css class (default is wow)
      animateClass: "animated", // animation css class (default is animated)
      offset: 0, // distance to the element when triggering the animation (default is 0)
      mobile: true, // trigger animations on mobile devices (default is true)
      live: true, // act on asynchronously loaded content (default is true)
    });
    wow.init();

    /* ==================================================
            # Tooltip Init
        ===============================================*/
    $('[data-toggle="tooltip"]').tooltip();

    /* ==================================================
            # Smooth Scroll
         ===============================================*/
    $("body").scrollspy({
      target: ".navbar-collapse",
      offset: 200,
    });
    $("a.smooth-menu").on("click", function (event) {
      var $anchor = $(this);
      var headerH = "75";
      $("html, body")
        .stop()
        .animate(
          {
            scrollTop: $($anchor.attr("href")).offset().top - headerH + "px",
          },
          1500,
          "easeInOutExpo",
        );
      event.preventDefault();
    });

    /* ==================================================
            # Banner Animation
        ===============================================*/
    function doAnimations(elems) {
      var animEndEv = "webkitAnimationEnd animationend";

      elems.each(function () {
        var $this = $(this),
          $animationType = $this.data("animation");

        $this.addClass($animationType).one(animEndEv, function () {
          $this.removeClass($animationType);
        });
      });
    }

    var $immortalCarousel = $(".animate_text");

    var $firstAnimatingElems = $immortalCarousel
      .find(".carousel-item:first")
      .find("[data-animation ^= 'animated']");

    $immortalCarousel.carousel({
      interval: 2500,
    });

    doAnimations($firstAnimatingElems);

    $immortalCarousel.on("slid.bs.carousel", function (e) {
      var $animatingElems = $(e.relatedTarget).find(
        "[data-animation ^= 'animated']",
      );
      doAnimations($animatingElems);
    });
    /* ==================================================
            # imagesLoaded active
        ===============================================*/
    $("#portfolio-grid,.blog-masonry").imagesLoaded(function () {
      /* Filter menu */
      $(".mix-item-menu").on("click", "button", function () {
        var filterValue = $(this).attr("data-filter");
        $grid.isotope({
          filter: filterValue,
        });
      });

      /* filter menu active class  */
      $(".mix-item-menu button").on("click", function (event) {
        $(this).siblings(".active").removeClass("active");
        $(this).addClass("active");
        event.preventDefault();
      });

      /* Filter active */
      var $grid = $("#portfolio-grid").isotope({
        itemSelector: ".pf-item",
        percentPosition: true,
        masonry: {
          columnWidth: ".pf-item",
        },
      });

      /* Filter active */
      $(".blog-masonry").isotope({
        itemSelector: ".blog-item",
        percentPosition: true,
        masonry: {
          columnWidth: ".blog-item",
        },
      });
    });

    /* ==================================================
            # Fun Factor Init
        ===============================================*/
    $(".timer").countTo();
    $(".fun-fact").appear(
      function () {
        $(".timer").countTo();
      },
      {
        accY: -100,
      },
    );

    /* ==================================================
            # Magnific popup init
         ===============================================*/
    $(".popup-link").magnificPopup({
      type: "image",
      // other options
    });

    $(".popup-gallery").magnificPopup({
      type: "image",
      gallery: {
        enabled: true,
      },
      // other options
    });

    $(".popup-youtube, .popup-vimeo, .popup-gmaps").magnificPopup({
      type: "iframe",
      mainClass: "mfp-fade",
      removalDelay: 160,
      preloader: false,
      fixedContentPos: false,
    });

    $(".magnific-mix-gallery").each(function () {
      var $container = $(this);
      var $imageLinks = $container.find(".item");

      var items = [];
      $imageLinks.each(function () {
        var $item = $(this);
        var type = "image";
        if ($item.hasClass("magnific-iframe")) {
          type = "iframe";
        }
        var magItem = {
          src: $item.attr("href"),
          type: type,
        };
        magItem.title = $item.data("title");
        items.push(magItem);
      });

      $imageLinks.magnificPopup({
        mainClass: "mfp-fade",
        items: items,
        gallery: {
          enabled: true,
          tPrev: $(this).data("prev-text"),
          tNext: $(this).data("next-text"),
        },
        type: "image",
        callbacks: {
          beforeOpen: function () {
            var index = $imageLinks.index(this.st.el);
            if (-1 !== index) {
              this.goTo(index);
            }
          },
        },
      });
    });

    /* ==================================================
            # Doctor Carousel
         ===============================================*/
    $(".doctors-carousel").owlCarousel({
      loop: false,
      nav: false,
      margin: 30,
      dots: true,
      autoplay: true,
      items: 1,
      navText: [
        "<i class='fa fa-angle-left'></i>",
        "<i class='fa fa-angle-right'></i>",
      ],
    });

    /* ==================================================
            # Tips Carousel
         ===============================================*/
    $(".tips-carousel").owlCarousel({
      loop: false,
      nav: false,
      margin: 30,
      dots: true,
      autoplay: true,
      items: 1,
      navText: [
        "<i class='fa fa-angle-left'></i>",
        "<i class='fa fa-angle-right'></i>",
      ],
    });

    /* ==================================================
            # Department Carousel
         ===============================================*/
    $(".department-carousel").owlCarousel({
      loop: false,
      margin: 30,
      nav: false,
      navText: [
        "<i class='fa fa-angle-left'></i>",
        "<i class='fa fa-angle-right'></i>",
      ],
      dots: true,
      autoplay: false,
      responsive: {
        0: {
          items: 1,
        },
        800: {
          items: 2,
        },
        1000: {
          items: 3,
        },
      },
    });

    /* ==================================================
            # Testimonials Carousel
         ===============================================*/
    $(".testimonials-carousel").owlCarousel({
      loop: true,
      nav: false,
      margin: 30,
      dots: true,
      autoplay: false,
      items: 1,
      navText: [
        "<i class='fa fa-angle-left'></i>",
        "<i class='fa fa-angle-right'></i>",
      ],
      responsive: {
        767: {
          items: 2,
        },
        1000: {
          stagePadding: 100,
          items: 2,
        },
      },
    });

    $(".owl-carousel-01").owlCarousel({
      loop: true,
      margin: 20,
      nav: false,
      dots: true,
      autoplay: true,
      autoplayTimeout: 4000,
      autoplayHoverPause: true,

      responsive: {
        0: {
          items: 1,
        },
        768: {
          items: 1,
        },
        1024: {
          items: 1,
        },
      },
    });

    $(".appointment-slider").owlCarousel({
      loop: true,
      margin: 20,
      nav: false,
      dots: true,
      autoplay: true,
      autoplayTimeout: 3000,
      navText: [
        '<i class="fas fa-chevron-left"></i>',
        '<i class="fas fa-chevron-right"></i>',
      ],
      responsive: {
        0: { items: 1 },
        600: { items: 2 },
        1000: { items: 3 },
      },
    });

    // Back to Top:
    $(window).on("scroll", function () {
      if ($(window).scrollTop() > 0) {
        $(".back2top").fadeIn();
      } else {
        $(".back2top").fadeOut();
      }
      var bottom_pad =
        parseInt($(".footer_wrapper").height()) +
        parseInt($(".footer_wrapper").attr("data-pad-top")) +
        parseInt($(".footer_wrapper").attr("data-pad-bottom")) +
        30;
      if (
        $(window).scrollTop() >
        $(document).height() - $(window).height() - bottom_pad
      ) {
        $(".back2top").css({ bottom: bottom_pad + "px" });
      } else {
        $(".back2top").css({ bottom: "30px" });
      }
    });
    $(".back2top").on("click", function () {
      $("body,html").animate(
        {
          scrollTop: 0,
        },
        400,
      );
      return false;
    });

    /* ==================================================
            Nice Select Init
         ===============================================*/
    $("select").niceSelect();

    /* ==================================================
            Contact Form Validations
        ================================================== */
    $(".contact-form").each(function () {
      var formInstance = $(this);
      formInstance.submit(function () {
        var action = $(this).attr("action");

        $("#message").slideUp(750, function () {
          $("#message").hide();

          $("#submit")
            .after('<img src="assets/img/ajax-loader.gif" class="loader" />')
            .attr("disabled", "disabled");

          $.post(
            action,
            {
              name: $("#name").val(),
              email: $("#email").val(),
              phone: $("#phone").val(),
              comments: $("#comments").val(),
            },
            function (data) {
              document.getElementById("message").innerHTML = data;
              $("#message").slideDown("slow");
              $(".contact-form img.loader").fadeOut("slow", function () {
                $(this).remove();
              });
              $("#submit").removeAttr("disabled");
            },
          );
        });
        return false;
      });
    });

    // fancybox gallery
    Fancybox.bind("[data-fancybox]", {
      Carousel: {
        Zoomable: {
          Panzoom: {
            protected: true,
          },
        },
      },
    });

    // counter
    $(".shortcode-counter").each(function () {
      var el = $(this);
      var countEl = el.find(".stat-count");
      var count = countEl.data("count");
      var hasPlus = countEl.data("plus");

      $({ num: 0 }).animate(
        { num: count },
        {
          duration: 3000,
          step: function () {
            let text = Math.floor(this.num);
            if (hasPlus) text += "+";
            countEl.text(text);
          },
          complete: function () {
            let text = this.num;
            if (hasPlus) text += "+";
            countEl.text(text);
          },
        },
      );
    });
  }); // end document ready function
  /* ==================================================
        Preloader Init
        ===============================================*/
  $(document).ready(function () {
    // safety fallback: hide after 3 seconds anyway
    setTimeout(function () {
      $(".preloader").fadeOut(600);
    }, 3000);
  });

  $(window).on("load", function () {
    $(".preloader").fadeOut(600);
  });

  
  


})(jQuery); // End jQuery
