// Prevent offsetHeight error
const header = document.querySelector('.main-header');
if (header) {
  window.addEventListener('scroll', function() {
    // your sticky logic here
  });
}


/** ============================================================
 *  JS File for Kipya Template
 *===============================================================*/
$(document).ready(function() {
    $('.menu-item').hover(
        function() {
            $(this).children('.sub-menu').stop(true, true).slideDown(200); // Adjust the speed as needed
        },
        function() {
            $(this).children('.sub-menu').stop(true, true).slideUp(200);
        }
    );
});
/** ============================================================================
 * HEADER SCROLL
*=============================================================================*/
document.addEventListener('DOMContentLoaded', function() {
    const header = document.querySelector('.menu-section');
    const stickyHeaderClass = 'sticky-header';
    
    function handleScroll() {
        if (window.scrollY > header.offsetHeight) {
            header.classList.add(stickyHeaderClass);
        } else {
            header.classList.remove(stickyHeaderClass);
        }
    }

    window.addEventListener('scroll', handleScroll);
    handleScroll(); // Initial check
});
/**====================================================
   * Slides - Animation JS
*====================================================**/
function thmSwiperInit() {
    // swiper slider
    if ($(".thm-swiper__slider").length) {
      $(".thm-swiper__slider").each(function () {
        let elm = $(this);
        let options = elm.data('swiper-options');
        let thmSwiperSlider = new Swiper(elm, options);
      });
    }
  }
  if ($(".banner-bg-slide").length) {
    $(".banner-bg-slide").each(function () {
      var Self = $(this);
      var bgSlideOptions = Self.data("options");
      var bannerTwoSlides = Self.vegas(bgSlideOptions);
    });
  }
  if ($(".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();
  }
    // window load event
    $(window).on("load", function () {

      thmSwiperInit();
    //   thmOwlInit();
    //   handlePreloader();
    //   languageSwitcher();
    //   projectMasonaryLayout();
  
  
      //Jquery Spinner / Quantity Spinner
      if ($('.quantity-spinner').length) {
        $("input.quantity-spinner").TouchSpin({
          verticalbuttons: true
        });
      }
  
    });
/**====================================
* AOS
=====================================**/
     document.addEventListener('DOMContentLoaded', function () {
    // Initialize AOS for each column
    if (typeof AOS !== 'undefined') {
        AOS.init({
            duration: 1000,
            easing: 'ease-in-out',
            once: true,
            mirror: false,
        });
    }
});
 /**====================================
     * HIDE OVERFLOW
=====================================**/
     
(function ($, root, undefined) {
	
	$(function () {
		
		'use strict';
		
		// DOM ready, take it away
		$("html").attr("style", "overflow-x:hidden;");
		
	});
	
})(jQuery, this);


// Popup Modal
$(document).ready(function() {
    $('.popup-modal').magnificPopup({
        type: 'inline',
        midClick: true // Allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source.
    });
});

/** ===============================================
 * Back to Top
 *=================================================*/
function select(selector) {
  return document.querySelector(selector);
}

// Check if the back-to-top element exists
var backtotop = select('.back-to-top');
if (backtotop) {
  const toggleBacktotop = () => {
      if (window.scrollY > 200) { // Change threshold to 200
          backtotop.classList.add('active');
      } else {
          backtotop.classList.remove('active');
      }
  };

  window.addEventListener('load', toggleBacktotop);
  window.addEventListener('scroll', toggleBacktotop);
  
  // Scroll to top on click
  backtotop.addEventListener('click', () => {
      window.scrollTo({
          top: 0,
          behavior: 'smooth' // Smooth scroll to top
      });
  });
}

/**====================================
* Custom Fadeup
=====================================**/
document.addEventListener("DOMContentLoaded", function () {
  const fadeElements = document.querySelectorAll(".fadeup");

  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.classList.add("animate");
        observer.unobserve(entry.target);
      }
    });
  }, {
    threshold: 0.2
  });

  fadeElements.forEach(el => observer.observe(el));
});

/**====================================
* Preloader
=====================================**/
document.body.classList.remove('kipya-loaded');

// Hide when page is fully loaded
window.addEventListener('load', function() {
  setTimeout(function() {
    document.body.classList.add('kipya-loaded');
  }, 500);
});

// Fallback in case load event doesn't fire
setTimeout(function() {
  document.body.classList.add('kipya-loaded');
}, 5000);


/**====================================
* About Page Intro Sections
=====================================**/

// Intersection Observer for intro sections animation
document.addEventListener('DOMContentLoaded', function() {
    const sections = document.querySelectorAll('.intro-sections .wp-block-columns');
    
    // Create intersection observer
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('animate-in');
                observer.unobserve(entry.target); // Stop observing once animated
            }
        });
    }, {
        threshold: 0.2, // Trigger when 20% of element is visible
        rootMargin: '0px 0px -50px 0px' // Start animation 50px before element is fully visible
    });
    
    // Observe all sections
    sections.forEach(section => {
        observer.observe(section);
    });
});