// source --> https://ia-foods.com/wp-content/themes/ia_theme/assets/js/script.min.js?ver=1.0.1 
"use strict";

jQuery(function ($) {
  // この中であればWordpressでも「$」が使用可能になる

  // 要素のフェードイン
  function fadeInView() {
    var windowTop = $(window).scrollTop();
    var windowBottom = windowTop + $(window).height();
    $('.js-fade').each(function () {
      var elementTop = $(this).offset().top + 100;
      var elementBottom = elementTop + $(this).outerHeight();
      if (elementBottom >= windowTop && elementTop <= windowBottom) {
        $(this).addClass('is-fade');
      }
    });
  }
  $(document).ready(function () {
    fadeInView();
  });
  $(window).scroll(function () {
    fadeInView();
  });

  // スムーススクロール
  $(document).on('click', 'a[href*="#"]', function () {
    var time = 400;
    var header = $('.p-header').innerHeight();
    var target = $(this.hash);
    if (!target.length) return;
    var targetY = target.offset().top - header;
    $('html,body').animate({
      scrollTop: targetY
    }, time, 'swing');
    return false;
  });

  // ハッシュがある場合のスムーススクロール
  // var header = $('.p-header').outerHeight();
  var urlHash = location.hash;
  if (urlHash) {
    $('body,html').stop().scrollTop(0);
    setTimeout(function () {
      var target = $(urlHash);
      var position = target.offset().top;
      $('body,html').stop().animate({
        scrollTop: position
      }, 500);
    }, 100);
  }

  // linearスライダー
  $('.js-linear-slider').slick({
    autoplay: true,
    autoplaySpeed: 0,
    speed: 10000,
    cssEase: 'linear',
    arrows: false,
    swipe: false,
    dots: false,
    loop: true,
    slidesToScroll: 1,
    variableWidth: true,
    pauseOnHover: false,
    pauseOnFocus: false
  });
  $('.js-linear-slider--reverse').slick({
    autoplay: true,
    autoplaySpeed: 0,
    speed: 10000,
    cssEase: 'linear',
    arrows: false,
    swipe: false,
    dots: false,
    variableWidth: true,
    pauseOnHover: false,
    pauseOnFocus: false,
    rtl: true
  });

  // カーソルホバーエフェクト
  $('.js-play-btn').each(function () {
    $(this).on('mouseover', function () {
      $(this).addClass('is-cursor');
    }).on('mouseout', function () {
      $(this).removeClass('is-cursor');
    });
    function mouseMove(e) {
      $(this).find('.js-more-btn').css({
        'top': e.clientY,
        'left': e.clientX
      });
    }
    $(this).on('mousemove', mouseMove);
  });

  // センターモードスライダー
  $('.js-lineup-slider').slick({
    centerMode: true,
    slidesToShow: 3,
    arrows: false,
    variableWidth: true,
    autoplay: true,
    speed: 3000
  });

  // フェードスライダー
  $('.js-fade-slider').slick({
    fade: true,
    speed: 1500,
    autoplaySpeed: 3000,
    arrows: false,
    autoplay: true,
    slidesToShow: 1,
    slidesToScroll: 1,
    dots: false,
    infinite: true
  });

  // ハンバーガーメニュー
  $('.js-hamburger').click(function () {
    $(this).toggleClass('is-active');
    $('.p-drawer').toggleClass('is-active');
  });
  $('.p-drawer__btn').click(function () {
    var $parent = $(this).parent();
    var $content = $(this).next();
    if ($content.css('display') === 'none' || $content.css('display') === '') {
      $parent.addClass('is-active');
      $content.slideDown();
    } else {
      $parent.removeClass('is-active');
      $content.slideUp();
    }
  });

  // モーダルウィンドウ
  $('.js-modal-open').click(function (event) {
    event.preventDefault();
    $(this).next().fadeIn();
    $('html').addClass('is-fixed');
  });
  $('.js-modal-close').click(function () {
    $('.p-modal').fadeOut();
    $('html').removeClass('is-fixed');
  });
    
    // フォームラジオ切り替えイベント
    $('input[name="formSelect"]').on('change', function () {
      $('.tabcontent').hide();
      $('#' + this.value).show();
    });
    var checkedRadio = $('input[name="formSelect"]:checked');
    if (checkedRadio.length) {
      $('#' + checkedRadio.val()).show();
    }

});

// Scrollifyの初期化
// jQuery(function ($) {
//   $.scrollify({
//     section: ".section",
//     standardScrollElements: ".js-play-btn",
//     scrollSpeed: 600,
//     offset: 0,
//     easing: "easeOutExpo",
//     updateHash: false
//   });
//   var isDragging = false;
//   jQuery('.normal-scroll').on('mousedown touchstart', function (event) {
//     isDragging = true;
//     $.scrollify.disable();
//   });
//   jQuery(document).on('mouseup touchend', function (event) {
//     if (isDragging) {
//       isDragging = false;
//       $.scrollify.enable();
//     }
//   });
// });
// source --> https://ia-foods.com/wp-content/themes/ia_theme/assets/js/index.min.js?ver=1.0.1 
// GSAPのアクティブリンクエフェクト
jQuery(function($){
  gsap.registerPlugin(ScrollTrigger);
  const topLink = document.querySelector('.p-float__link a[href="#top"]').parentElement;
  
  
  document.querySelectorAll(".p-float__link a").forEach((link) => {
    const targetID = link.getAttribute("href");
    const section = document.querySelector(targetID);
  
    if (section) {
      ScrollTrigger.create({
        trigger: section,
        start: "top center",
        end: "bottom center",
        onEnter: () => {
          document.querySelectorAll(".p-float__link").forEach((el) => el.classList.remove("is-active"));
          link.parentElement.classList.add("is-active");
        },
        onLeaveBack: () => {
          document.querySelectorAll(".p-float__link").forEach((el) => el.classList.remove("is-active"));
          link.parentElement.classList.add("is-active");
        }
      });
    }
  });
  
  ScrollTrigger.create({
    trigger: "#aboutus",
    start: "top center",
    onLeaveBack: () => {
      document.querySelectorAll(".p-float__link").forEach((el) => el.classList.remove("is-active"));
      topLink.classList.add("is-active");
    }
  });
});




// Scrollifyのスクロールインジケータ
jQuery(function ($) {
  $.scrollify({
      section: ".section",
      standardScrollElements: ".js-play-btn, .normal-scroll",
      scrollSpeed: 500,
      offset: 0,
      easing: "linear",
      updateHash: false,

      before: function (index) {
          const sections = document.querySelectorAll(".section");
          const sectionId = sections[index].id;

          document.querySelectorAll(".p-float__link").forEach((el) => el.classList.remove("is-active"));

          if (sectionId === "top" || index === 0) {
              document.querySelector('.p-float__link a[href="#top"]').parentElement.classList.add("is-active");
          } else {
              const activeLink = document.querySelector(`.p-float__link a[href="#${sectionId}"]`);
              if (activeLink) {
                  activeLink.parentElement.classList.add("is-active");
              }
          }

          sections[index].classList.remove("is-scrolled");
      },
      after: function (index) {
          const sections = document.querySelectorAll(".section");

          sections.forEach((section, i) => {
              if (i < index) {
                  section.classList.add("is-scrolled");
              }
          });

          sections.forEach((section, i) => {
              if (i > index) {
                  section.classList.add("is-scrolled");
              }
          });
      }
  });

  let isDragging = false;
  let isTouchDevice = 'ontouchstart' in window;

  const startEvent = isTouchDevice ? 'touchstart' : 'mousedown';
  const endEvent = isTouchDevice ? 'touchend' : 'mouseup';

  function checkTabletPortrait() {
      return window.innerWidth <= 768 && window.innerHeight > window.innerWidth;
  }

  function toggleScrollify() {
      if (checkTabletPortrait()) {
          jQuery('.normal-scroll').on(startEvent, function(event) {
              isDragging = true;
              $.scrollify.disable();
          });
          jQuery(document).on(endEvent, function(event) {
              if (isDragging) {
                  isDragging = false;
                  $.scrollify.enable();
              }
          });
      } else {
          jQuery('.normal-scroll').off(startEvent);
          jQuery(document).off(endEvent);
          $.scrollify.enable();
      }
  }

  toggleScrollify();
  $(window).on('resize', toggleScrollify);
});