// **************************************************
// bodyタグにdata-pageとscrollクラスを付与
document.body.dataset.page = location.pathname.split("/")[1] || "top";

window.onscroll = () => {
  document.body.classList.toggle("scroll", window.scrollY > 500);
};

const headerHeight = document.querySelector("header").offsetHeight;
document.body.style.setProperty("--headerHeight", headerHeight + "px");

const sp_footerHeight = document.querySelector(".sp_footer").offsetHeight;
document.body.style.setProperty("--sp-footer-height", sp_footerHeight + "px");

// **************************************************
// スムーススクロール関数
function smoothScroll(position) {
  const start = window.scrollY;
  const distance = Math.min(
    position - start,
    document.documentElement.scrollHeight - window.innerHeight - start
  );
  const duration = 800;
  let startTime;
  function easeOutExpo(t, b, c, d) {
    return (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b;
  }
  function animate(time) {
    if (startTime === undefined) startTime = time;
    const elapsed = time - startTime;
    const scrollPos = easeOutExpo(elapsed, start, distance, duration);
    window.scrollTo(0, scrollPos);
    if (elapsed < duration) requestAnimationFrame(animate);
    else window.scrollTo(0, position);
  }
  requestAnimationFrame(animate);
}

// ページ内リンクのスムーススクロール
for (const link of document.querySelectorAll('a[href*="#"]')) {
  link.addEventListener("click", (e) => {
    const hash = e.currentTarget.hash;
    if (!hash || hash === "#top") {
      e.preventDefault();
      smoothScroll(1);
    } else {
      const target = document.getElementById(hash.slice(1));
      if (target) {
        e.preventDefault();
        const position =
          target.getBoundingClientRect().top +
          window.scrollY -
          parseFloat(window.getComputedStyle(target).marginTop) -
          100;
        smoothScroll(position);
        history.pushState(null, null, hash);
      }
    }
  });
}

// 別ページ遷移後のスムーススクロール
const hash = window.location.hash;
if (hash) {
  const target = document.getElementById(hash.slice(1));
  if (target) {
    window.addEventListener("load", () => {
      const position =
        target.getBoundingClientRect().top +
        window.scrollY -
        parseFloat(window.getComputedStyle(target).marginTop) -
        100;
      window.scrollTo(0, 0);
      smoothScroll(position);
    });
  }
}

// **************************************************
// nav開閉
window.addEventListener("load", () => {
  const nav = document.querySelector("header nav");
  const toggleNav = () => {
    nav.style.right = document.body.classList.toggle("onNav") ? "0" : "";
  };
  document.querySelector("#menu").addEventListener("click", toggleNav);
  nav
    .querySelectorAll("a")
    .forEach((link) => link.addEventListener("click", toggleNav));
});

// // **************************************************
// // アコーディオン開閉
function toggleShow(selector) {
  window.addEventListener("load", () => {
    document.querySelectorAll(selector).forEach((element) => {
      const first = element.children[0];
      const second = element.children[1];
      element.addEventListener("click", () => {
        first.classList.toggle("show");
        second.style.height = second.style.height
          ? null
          : `${second.scrollHeight}px`;
      });
    });
  });
}
toggleShow(".list_faq dl");
toggleShow(".has_cld");
