(function ($) {
  'use strict';

  //モーダルを開いた時のスクロール位置を保持
  var scrollPosition;
  //iOS（iPadOSを含む）かどうかのUA判定
  var ua = window.navigator.userAgent.toLowerCase();
  var isiOS = ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1 || ua.indexOf('macintosh') > -1 && 'ontouchend' in document;

  //bodyのスクロール固定
  function ban_scroll() {
      if(isiOS){
          // iOSの場合
          scrollPosition = $(window).scrollTop();
      $('body').css('width', '100vw');
          $('body').css('position', 'fixed');
          $('body').css('top', '-' + scrollPosition + 'px');
      }else {
          // それ以外
          $('body').css('overflow', 'hidden');
      }
  }

  //bodyのスクロール固定を解除
  function go_scroll() {
      if(isiOS){
          // iOSの場合
          $('body').css('position', '');
          $('body').css('top', '');
      $('body').css('width', '');
          $(window).scrollTop(scrollPosition);
      }else {
          // それ以外
          $('body').css('overflow', '');
      }
  }

  function anchorScroll(oft) {
    var headHeight = parseInt($('.header').innerHeight());
    $('body, html').stop().animate({
      scrollTop: oft - headHeight,
      duration: 1000,
      easing: 'easeInOutCubic'
    });
    return;
  }

  // アンカーリンク
  (function () {
    var $anchor = $('a.js-anchor');

    $anchor.on('click', function (e) {
      e.preventDefault();
      var href = $(this).attr('href');
      var $t = $(href).length ? $(href) : $('html');

      // スクロール
      anchorScroll($t.offset().top);
    });
  })();


  // scroll
  (function () {
    const elm2 = document.querySelectorAll(".js-scroll");
    if (elm2.length) {
      let options2 = { rootMargin: "-20% 0px" };
      const observer2 = new IntersectionObserver(callback2,options2);
      elm2.forEach(function(value) {
          var $this = $(value);
          var innH = $(window).innerHeight() * 0.7;
          var oft = parseInt($(window).scrollTop() + innH);
          var pos = $this.offset().top;
          if (pos <= oft) {
              $this.addClass("is-active");
          } else {
              observer2.observe(value);
          }
      });
      function callback2(entries) {
        entries.forEach(function(value) {
            if(value.isIntersecting) {
                value.target.classList.add("is-active");
                observer2.unobserve(value.target);
            }
        });
      }
    }

    /*
    var obj = {
      init: function () {
        this.$wrap = $('.js-scroll');
        if (this.$wrap.length) {
          this._enabled();
          this._scroll();
        }
      },
      _enabled: function () {
        var _this = this;
        $(window).on('scroll', function () {
          _this._scroll();
        });
      },
      _scroll: function () {
        this.$wrap.each(this._decision);
      },
      _decision: function () {
        var $this = $(this);
        var innH = $(window).innerHeight() * 0.7;
        var oft = parseInt($(window).scrollTop() + innH);
        var pos = $this.offset().top;
        if (pos <= oft) $this.addClass('is-active');
      }
    }.init();
    */
  })();

  (function () {
    var $label = $('label.Label_fold');

    $label.on('click', function (e) {
      var href = $(this).closest('.read_more');
      var $t = $(href).length ? $(href) : $('html');

      // スクロール
      anchorScroll($t.offset().top - 50);
    });
  })();

  if (window.matchMedia('(min-width:751px)').matches) {
    $("#Language_pc").on("change", function() {
      if ($("#Language_pc").prop('checked')) {
        $(window).one("scroll", function (e) {
          $("#Language_pc").removeAttr("checked").prop("checked", false).change();
        });
      }
    });

    $(window).on("click", function (e) {
      if ($(e.target).prop("htmlFor") != "Language_pc") {
        if ($("#Language_pc").prop('checked')) {
          $("#Language_pc").removeAttr("checked").prop("checked", false).change();
        }
      }
    });
  }

  $(".toggle").on("click",function(e){
    e.stopPropagation();
  });
  $(".toggle_more").on("click",function(e){
    e.stopPropagation();
  });

  $("#drawer_input").on("change",function(){
    if($(this).prop('checked')){
      ban_scroll();
    } else {
      if ($("#Language_sp").prop('checked')) {
        $("#Language_sp").removeAttr("checked").prop("checked", false).change();
      }
      go_scroll();
    }
  });

  $(window).on('load', function() {
    var url = $(location).attr('href');
    if(url.indexOf("#") != -1){
        var anchor = url.split("#");
        var $t = $('#' + anchor[anchor.length - 1]);

        // スクロール
        anchorScroll($t.offset().top);
    }
  })

})(jQuery);