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

  var topBtn = $(".pagetop");
  topBtn.hide();

  // ボタンの表示設定
  $(window).scroll(function () {
    if ($(this).scrollTop() > 70) {
      // 指定px以上のスクロールでボタンを表示
      topBtn.fadeIn();
    } else {
      // 画面が指定pxより上ならボタンを非表示
      topBtn.fadeOut();
    }
  });

  // ボタンをクリックしたらスクロールして上に戻る
  topBtn.click(function () {
    $("body,html").animate(
      {
        scrollTop: 0,
      },
      300,
      "swing"
    );
    return false;
  });

  // ヘッダースクロールで色変わる
  var _window = $(window),
    _header = $(".header");
  _window.on("scroll", function () {
    changeHeight = 200;
    if (_window.scrollTop() > changeHeight) {
      _header.addClass("change");
    } else {
      _header.removeClass("change");
    }
  });
  _window.trigger("scroll");

  //ドロワーメニュー
  var st = $(window).scrollTop();
  $(".hamburger").click(function () {
    $(this).toggleClass("open");
    $(".header__nav--sp").toggleClass("open");
    $(".header__inner").toggleClass("open");
    $(".header-w").toggleClass("open");
    $("html").toggleClass("is-fixed");
  });
  // $(".sp-nav__bg").click(function () {
  //   $(".header__hamburger").removeClass("open");
  //   $(".header__nav--sp").removeClass("open");
  //   $("html").removeClass("is-fixed");
  // });

  // アコーディオン
  $(".js-accordion").click(function () {
    $(this).toggleClass("open");
    $(".sp-second__list").slideToggle();
  });

  // スムーススクロール (絶対パスのリンク先が現在のページであった場合でも作動)

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

  var swiper1 = new Swiper("#fv-swiper", {
    speed: 2000,
    loop: true,
    centeredSlides: true,
    slidesPerView: 1,
    autoplay: {
      delay: 3000,
      stopOnLastSlide: false,
      disableOnInteraction: false,
      reverseDirection: false,
    },
    effect: "fade",
    freeMode: true,
  });

  var swiper2 = new Swiper("#top-works-swiper", {
    speed: 1000,
    loop: true,
    centeredSlides: true,
    slidesPerView: 1.3,
    spaceBetween: 16,
    breakpoints: {
      // 768px以上の場合
      769: {
        slidesPerView: 4,
        spaceBetween: 40,
      },
    },
    autoplay: {
      delay: 3000,
    },
    navigation: {
      nextEl: ".swiper-button-next",
      prevEl: ".swiper-button-prev",
    },
    effect: "",
  });

  // パラメータを検索してクラス付与
  $(document).ready(function () {
    // URLのパラメータを取得
    var params = new URLSearchParams(window.location.search);
    console.log(params);
    var selectedCategory = params.get("cat") || "";
    var tabs = document.querySelectorAll(".works-tab__item");
    tabs.forEach(function (tab) {
      var tabCategory = tab.getAttribute("data-category");
      if (tabCategory === selectedCategory) {
        tab.classList.add("selected");
      } else {
        tab.classList.remove("selected");
      }
    });
  });


// お問い合わせフロー図のCSS付与
  window.onload = function () {
    var contactForm = document.querySelector("#mw_wp_form_mw-wp-form-68");
    var input = document.querySelector(".contact-flow__item--input");
    var confirm = document.querySelector(".contact-flow__item--confirm");

    if (contactForm && contactForm.classList.contains("mw_wp_form_input")) {
      input.classList.add("current");
    }
    if (contactForm && contactForm.classList.contains("mw_wp_form_confirm")) {
      confirm.classList.add("current");
    }
  };
	

	// ローディングアニメーション
const loadingTl = gsap.timeline();

// sessionStorageでアニメーションの再生状態を確認
if (sessionStorage.getItem("loadingAnimationPlayed") !== "true") {
    loadingTl
    .to(".glitch", {
      duration: 1,
      ease: "power2.easeIn",
      //delay: 0.5,
      autoAlpha: 1,
      onComplete: () => {
        setTimeout(() => {
          document.querySelector(".glitch").classList.add("anime");
        }, 0);
      },
    })
    .to(".loading", {
      autoAlpha: 0,
      ease: "power4.easeOut",
      duration: 0.5,
      delay: 0.5,
    })
    .to(".loading-title", {
      autoAlpha: 1,
      ease: "power4.easeIn",
      duration: 0.5,
    })
    .to(".loading-fadein", {
      autoAlpha: 1,
      x: 0,
      y: 0,
      duration: 0.5,
      delay: 1.0,
    })
    .to(
      "#gt_float_wrapper",
      {
        autoAlpha: 1,
        x: 0,
        y: 0,
        duration: 0.5,
      },
      "<"
    );

  // アニメーションが再生されたことをsessionStorageに記録
  sessionStorage.setItem("loadingAnimationPlayed", "true");
}else{
	loadingTl
//     .to(".glitch", {
//       duration: 1,
//       ease: "power2.easeIn",
//       autoAlpha: 1,
//       onComplete: () => {
//         setTimeout(() => {
//           document.querySelector(".glitch").classList.add("anime");
//         }, 0);
//       },
//     })
    .to(".loading", {
      autoAlpha: 0,
    })
    .to(".loading-title", {
      autoAlpha: 1,
    })
    .to(".loading-fadein", {
      autoAlpha: 1,
      x: 0,
      y: 0,
    })
    .to(
      "#gt_float_wrapper",
      {
        autoAlpha: 1,
        x: 0,
        y: 0,
      },
      "<"
    );
}


  // アタッチメントページ動画再生
  $(function () {
    //Mobile判定
    var mobile = false;
    var ua = navigator.userAgent;
    if (
      ua.indexOf("iPhone") > 0 ||
      ua.indexOf("iPod") > 0 ||
      ua.indexOf("iPad") > 0 ||
      (ua.indexOf("Android") > 0 && ua.indexOf("Mobile") > 0)
    ) {
      mobile = true;
    }
    //スマホの場合muted属性追加
    if (mobile) {
      $("iframe").each(function () {
        $(this).attr("muted", "");
      });
    }
    $(".thumb").click(function () {
      var player = $(this).next("iframe")[0].contentWindow;
      player.postMessage(
        '{"event":"command","func":"playVideo","args":""}',
        "*"
      );
      $(this).hide();
    });
  });
});

document.addEventListener('DOMContentLoaded', function() {
  // 画像リンクのクリックイベントを設定
  document.querySelectorAll('figure.wp-block-image a').forEach(link => {
    link.addEventListener('click', function(e) {
      e.preventDefault();
      const modal = document.getElementById('image-modal');
      modal.style.display = "block";
      const modalImage = modal.querySelector('.modal-content');
      modalImage.src = e.currentTarget.href;
      const captionText = modal.querySelector('#caption');
      const figCaption = e.currentTarget.parentElement.querySelector('figcaption');
      if (figCaption) {
        captionText.innerHTML = figCaption.innerHTML;
      }
    });
  });

  // モーダルの閉じるボタンのイベントを設定
  document.querySelectorAll('.close').forEach(button => {
    button.addEventListener('click', function(e) {
      const modal = e.currentTarget.closest('.modal');
      modal.style.display = "none";
    });
  });
});

jQuery(document).ready(function($){
	$('#wpcf7-f5137-o1 .wpcf7-form').validate({
        rules: {
            company: {
                required: true
            },
            uname: {
                required: true
            },
            ruby: {
                required: true
            },
            utel: {
                required: true
            },
            umail: {
                required: true,
                email: true
            },
            udetail: {
                required: true
            },
            privacycheck: {
                required: true
            }
        },
        messages: {
            company: "未入力です。",
            uname: "未入力です。",
            ruby: "未入力です。",
            utel: "未入力です。",
            umail: "未入力です。",
            udetail: "未入力です。",
            privacycheck: "未入力です。",
        }
        
    });
	
	$('.btn_first .contact__btn--submit').click(function(e){
		e.preventDefault();
		if ($('#wpcf7-f5137-o1 .wpcf7-form').valid()) {
			var company_id = $('#company_id').val();
			var uname_id = $('#uname_id').val();
			var ruby_id = $('#ruby_id').val();
			var utel_id = $('#utel_id').val();
			var uemail_id = $('#uemail_id').val();
			var udetails_id = $('#udetails_id').val();

			$('.first_screen_show').fadeOut();
			$('.second_screen_show').fadeIn();

			// value get
			$('#company_1').text(company_id);
			$('#company_2').text(uname_id);
			$('#company_3').text(ruby_id);
			$('#company_4').text(utel_id);
			$('#company_5').text(uemail_id);
			$('#company_6').text(udetails_id);
		}
	});

	$('.contact__btn.contact__btn--back').click(function(e){
		e.preventDefault();
		$('.first_screen_show').fadeIn();
		$('.second_screen_show').fadeOut();
	});
	
	
	document.addEventListener( 'wpcf7mailsent', function( event ) {
		// Redirect to another page after form submission
		window.location.href = "https://oval3.com/contact/complete";
	}, false );
	

});