$(function () {
  //acodion
  $(".js-aco").on("click", function () {
    var index = $(".js-aco").index(this);
    $(".js-aco_comm").eq(index).slideToggle(500);
  });
  $(".js-aco-close").on("click", function () {
    var index = $(".js-aco-close").index(this);
    $(".js-aco_comm").eq(index).slideToggle(500);
  });

  // #で始まるアンカーをクリックした場合に処理
  $('a[href^="#"]').click(function () {
    var speed = 400;
    var href = $(this).attr("href");
    var target = $(href == "#" || href == "" ? "html" : href);
    var position = target.offset().top;
    $("body,html").animate({ scrollTop: position }, speed, "swing");
    return false;
  });
});

$(document).ready(function () {
  $("#con_an").hide();
  $(window).on("scroll", function () {
    if ($(this).scrollTop() > 100) {
      $("#con_an").fadeIn("fast");
    } else {
      $("#con_an").fadeOut("fast");
    }
  });
});

//チェックボックス連動

document.addEventListener("DOMContentLoaded", function () {
  $('input[name="purpose"]:radio').change(function () {
    let iii = $(this).attr("id");
    $(".--" + iii).prop("checked", true);
    let val = $('input[name="c_addr"]:checked').val();
    console.log(val);
  });
  //エラーページ連
  if ($('input[name="purpose"]:radio').val()) {
    let iii = $('input[name="purpose"]:radio:checked').attr("id");
    $(".--" + iii).prop("checked", true);
    let val = $('input[name="c_addr"]:checked').val();
  }
});

$(function () {
  $('[name="place"]:radio').change(function () {
    if ($("[id=a]").prop("checked")) {
      $(".hiden").fadeOut();
      $(".hiden01").fadeIn();
    } else if ($("[id=b]").prop("checked")) {
      $(".hiden").fadeOut();
      $(".hiden02").fadeIn();
    }
  });
});

//gmenu
$(document).ready(function () {
  let scrollPosi;
  const layer = $("#layer-menu");
  const hmg = $("#hmg");
  let bodyElm = $("body");
  let flag = false;

  const bodyFix = () => {
    scrollPosi = $(window).scrollTop();
    bodyElm.css({
      position: "fixed",
      width: "100%",
      top: -scrollPosi + "px",
    });
    flag = true;
  };

  const bodyFixReset = (target) => {
    bodyElm.css({
      position: "",
      width: "",
      top: "",
    });
    if (target !== true) {
      $(window).scrollTop(scrollPosi);
    }
  };

  const menuClose = (target, scrollToId) => {
    bodyFixReset(target);
    flag = false;
    hmg.removeClass("is_active");
    layer.removeClass("is_active");
    if (scrollToId) {
      $("html, body").animate(
        {
          scrollTop: $("#" + scrollToId).offset().top,
        },
        600
      ); // スムーズスクロールの速度は600ms
    }
  };

  hmg.on("click", function (e) {
    e.preventDefault();
    if (!flag) {
      bodyFix();
      hmg.addClass("is_active");
      layer.addClass("is_active");
    } else {
      menuClose(false);
    }
  });

  layer.on("click", function (e) {
    e.preventDefault();
    menuClose(false);
  });

  // アンカーリンクのスムーズスクロールを実装
  $('a[href^="#"]').on("click", function (e) {
    e.preventDefault();
    const href = $(this).attr("href").substring(1);
    menuClose(false, href);
  });
});
