// source --> https://wineyouwant.com/wp-content/themes/wineyouwant/assets/scripts/functions.js?ver=1742981144 
var $ = $ || jQuery;

$(window).scroll(onScroll);

jQuery(document).ready(function () {

    jQuery(window).scroll(function() {
        if (jQuery(window).scrollTop() > 1) {
            jQuery("#header").addClass("scrolled");
        } else {
            jQuery("#header").removeClass("scrolled");
        }
    });

  /* A activer selon les besoins

      /////// SROLLREVEAL ANIMATIONS ///////
  window.sr = ScrollReveal({ // Initialisation + Les Options Modifiable
      origin: 'top', // 'bottom', 'left', 'top', 'right'
      distance: '0px', // '100rem', '100%', '100vw', etc.
      duration: 500, // Duree
      delay: 0, // Délai
      rotate: { x: 0, y: 0, z: 0 }, // Rotation
      opacity: 0, // Opacite de depart
      scale: 0.5, // Scale au depart
      easing: 'cubic-bezier(0.6, 0.2, 0.1, 1)', // Accepts any valid CSS easing, e.g. 'ease', 'ease-in-out', 'linear', etc.
      mobile: false, // Animation sur Mobile
      reset: false, // True : elements declenché à chaque fois qu'on les voit
      viewFactor: 0.2, //0.20 = 20% de l'élement doit être visible pour se declencher
      viewOffset: { top: 0, right: 0, bottom: 0, left: 0 }, // Valeurs de pixels qui modifient les limites du conteneur. Ex si header top fixe de 48px Definir `{top: 48}`
  });
  sr.reveal('h1'); // Configuration par defaut appliqué aux h1
  sr.reveal('h1', { duration: 1000 }); // SurClasse configuration
   sr.reveal('#container_masonry .item', { duration: 2000 }, 50); // Listing configuration

  sr.reveal('.titre_40px_light .barre', { // Animation specifique
   beforeReveal: function(anim_titre){  anim_titre.classList.add('animate'); },
   opacity:1
   });




*/


    jQuery('.tablist button').click(function() {

        let container = jQuery(this).closest('.container');

        container.find('.tablist button').removeClass('actif');
        jQuery(this).addClass('actif');
        container.find('.tab-content').hide().removeClass('anim');
        var tabId = $(this).attr('id');
        jQuery('#panel-' + tabId).fadeIn(800).addClass('anim');
        var headerHeight = jQuery('#header').height();
        jQuery('html, body').animate({
            scrollTop: container.find('.tablist').offset().top  - headerHeight - 20
        }, 500);
    });


    /////// MENU MOBILE ///////
    jQuery(".hamburger").on("click", function () {
        if (jQuery(this).hasClass("is-active")) {
            jQuery("#hmenu > ul > li").removeClass('open_accordeon');
        }
        jQuery(this).toggleClass('is-active');
        jQuery("#hmobile").toggleClass("open");
        jQuery("html").toggleClass("menu-open");

    });


  /////// HEADER STICKY ///////
    if (jQuery('.wk_sticky').length > 0) {
        var sticky_onscroll = jQuery('.sticky_onscroll');
        var height_sticky_elts = sticky_onscroll.height();
        jQuery(".wk_sticky").css("height", height_sticky_elts);
        jQuery(window).scroll(function () {
            var position_top_sticky = jQuery('.wk_sticky').offset().top;

            if (jQuery(window).scrollTop() >= position_top_sticky) {
                sticky_onscroll.addClass('fixed');
            } else {
                sticky_onscroll.removeClass('fixed');
            }
        });
    }





    if(jQuery.magnificPopup){
        jQuery('.popin_html').magnificPopup({ removalDelay: 300, type: 'inline' });
    }






});

jQuery(window).on('load', function () {
  jQuery(window).scroll(); // Pour corriger une erreur sous Chrome, déclenche le menu "scroll" si on rafraichit la page en étant au milieu de celle-ci.
});

function onScroll () {
  // ANIMATION DES CHIFFRES
  jQuery("[data-number-animate-to]").each(function (k, v) {

    if (isScrolledIntoView($(v))) {

      var to = $(v).data('number-animate-to');
      var from = $(v).html();

      $(v).removeAttr('data-number-animate-to');

      $(v).prop('Counter', from).animate({
        Counter: to
      }, {
        duration: 3000,
        easing: 'swing',
        step: function (now) {
          $(this).text(Math.ceil(now));
        }
      });
    }
  });
}

/** GESTION AJAX **/
jQuery(document).on('submit', '.ajax-form', function (e) {

  e.preventDefault();

  let that = jQuery(this);

  let action = that.find('input[name="action"]').val();

  jQuery.ajax({
    url: ajax_url,
    method: 'post',
    dataType: 'json',
    data: that.serialize(),
    success: function (response) {
      window.dispatchEvent(
        new CustomEvent(
          'ajax_response_' + action,
          {
            detail: { response },
            bubbles: false,
            cancelable: true,
          }
        )
      );
    }, error: function (response) {
      console.log(response);
    }
  });
  return false;
});


/** HELPERS **/
function isScrolledIntoView (elem) {
  var docViewTop = $(window).scrollTop();
  var docViewBottom = docViewTop + $(window).height();

  var elemTop = $(elem).offset().top;
  var elemBottom = elemTop + $(elem).height();

  return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

function scroll_to (elmt, offset_adjustment) {
  var width = jQuery(window).width();
  var offset = $(elmt).offset().top;

  offset -= jQuery("#header").height();

  if (offset_adjustment)
    offset += offset_adjustment;

  /*if(width >= 768)
      offset -= 25;*/

  $('html, body').animate({ scrollTop: offset }, 500);




};