jQuery(document).ready(function ($) {

  $('.mobile-nav .toggle-button').on('click', function () {
    $('.mobile-nav .main-navigation').slideToggle();
  });

  $('.mobile-nav-wrap .close ').on('click', function () {
    $('.mobile-nav .main-navigation').slideToggle();

  });

  $('<button class="submenu-toggle"></button>').insertAfter($('.mobile-nav ul .menu-item-has-children > a'));
  $('.mobile-nav ul li .submenu-toggle').on('click', function () {
    $(this).next().slideToggle();
    $(this).toggleClass('open');
  });

  //accessible menu for edge
  $("#site-navigation ul li a").on('focus', function () {
    $(this).parents("li").addClass("focus");
  }).on('blur', function () {
    $(this).parents("li").removeClass("focus");
  });
});

var btn = jQuery('#button');

jQuery(window).scroll(function () {
  if (jQuery(window).scrollTop() > 300) {
    btn.addClass('show');
  } else {
    btn.removeClass('show');
  }
});
btn.on('click', function (e) {
  e.preventDefault();
  jQuery('html, body').animate({ scrollTop: 0 }, '300');
});

window.addEventListener('load', (event) => {
  jQuery(".preloader").delay(1000).fadeOut("slow");
});

jQuery(window).scroll(function () {
  var data_sticky = jQuery('#masthead').attr('data-sticky');

  if (data_sticky == 1) {
    if (jQuery(this).scrollTop() > 1) {
      jQuery('#masthead').addClass("sticky-head");
    } else {
      jQuery('#masthead').removeClass("sticky-head");
    }
  }
});

function preloderFunction() {
  setTimeout(function () {
    document.getElementById("page-top").scrollIntoView();

    $('#ctn-preloader').addClass('loaded');
    // Once the preloader has finished, the scroll appears 
    $('body').removeClass('no-scroll-y');

    if ($('#ctn-preloader').hasClass('loaded')) {
      // It is so that once the preloader is gone, the entire preloader section will removed
      $('#preloader').delay(1000).queue(function () {
        $(this).remove();

        // If you want to do something after removing preloader:
        afterLoad();

      });
    }
  }, 3000);
}
function afterLoad() {
  // After Load function body!
}

document.addEventListener("DOMContentLoaded", function () {
  // Function to handle modal behavior
  function setupModal(modalId, buttonId, closeButtonId) {
    const modal = document.getElementById(modalId);
    const openButton = document.getElementById(buttonId);
    const closeButton = document.getElementById(closeButtonId);
    const videoPlayer = document.getElementById("videoPlayer");

    if (modal && openButton && closeButton) {
      // Open modal
      openButton.addEventListener("click", function () {
        const videoSrc = this.getAttribute("data-modal-src");
        if (videoSrc) {
          videoPlayer.src = videoSrc;
          modal.style.display = "block";
        }
      });

      // Close modal via close button
      closeButton.addEventListener("click", function () {
        videoPlayer.src = "";
        modal.style.display = "none";
      });

      // Close modal by clicking outside
      window.addEventListener("click", function (e) {
        if (e.target === modal) {
          videoPlayer.src = "";
          modal.style.display = "none";
        }
      });
    }
  }

  // Initialize modals
  setupModal("myModal", "openModalButton", "closeModalButton");
  setupModal("myModal1", "openModalButton1", "closeModalButton1");
  setupModal("myModal2", "openModalButton2", "closeModalButton2");
});


// Slider
jQuery(document).ready(function ($) {
  var owl = jQuery('.test .owl-carousel');
  owl.owlCarousel({
    loop: true,
    margin: 0,
    nav: false,
    dots: true,
    rtl: false,
    items: 1,
    autoplay: true,
  });

  // Add numbers with leading zeros to the first and last dots
  var totalItems = jQuery('.owl-dot').length;
  jQuery('.owl-dot').each(function (index) {
    if (index === 0) {
      jQuery(this).append('<span class="dot-number top-number">01</span>');
    }
    if (index === totalItems - 1) {
      var lastNum = (index + 1).toString().padStart(2, '0');
      jQuery(this).append('<span class="dot-number bottom-number">' + lastNum + '</span>');
    }
  });
});