$(function () {
  // TOP MVスライダー
  $(".top_mv .slider")
    .not(".slick-initialized")
    .slick({
      slidesToShow: 2,
      slidesToScroll: 1,
      autoplay: true,
      dots: true,
      arrows: true,
      autoplaySpeed: 4500,
      speed: 2000,
      centerMode: true,
      variableWidth: true,
      responsive: [
        {
          breakpoint: 768,
          settings: {
            slidesToShow: 1,
            slidesToScroll: 1,
            fade: true,
            arrows: false,
            centerMode: false,
            variableWidth: false
          }
        }
      ]
    });
  $(".top_mv").addClass("lazy");
  $(".body_container").addClass("lazy");
  function getWindowWidth() {
    return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  }

  // TOP お客様の声スライダー
  function initSlider1() {
    $(".slider.A").not(".slick-initialized").slick({
      slidesToShow: 2,
      slidesToScroll: 1,
      dots: false,
      infinite: true,
      arrows: true,
      autoplay: true,
      autoplaySpeed: 3000,
      speed: 2000,
      variableWidth: true
    });
  }
  $(window).resize(function () {
    if (getWindowWidth() < 768) {
      $(".top09 .slider").slick("unslick");
    } else {
      initSlider1();
    }
  });
  if (getWindowWidth() >= 768) {
    initSlider1();
  }

  // service 排水管清掃
  $(".movie .slider")
    .not(".slick-initialized")
    .slick({
      autoplay: true,
      dots: false,
      arrows: true,
      autoplaySpeed: 4500,
      speed: 1000,
      centerMode: true,
      centerPadding: "15%",
      variableWidth: false,
      responsive: [
        {
          breakpoint: 768,
          settings: {
            // fade: true,
            // arrows: false,
            centerMode: false
          }
        }
      ]
    });

  // グロナビ追加仕様
  var headerHeightPc = $(".pc_header.absolute").height();
  var headerHeightSp = $(".sp_header.absolute").height();

  $(window).scroll(function () {
    var scrollDistance = $(this).scrollTop();

    if (scrollDistance >= headerHeightPc + 50) {
      $(".pc_header.fix").addClass("active");
    } else {
      $(".pc_header.fix").removeClass("active");
    }

    if (scrollDistance >= headerHeightSp) {
      $(".sp_header.fix").addClass("active");
    } else {
      $(".sp_header.fix").removeClass("active");
    }
  });

  // グロナビ　メガメニュー
  var timeout;
  var $activeMegaMenu;
  $(".main_nav > .mega_btn")
    .mouseenter(function () {
      clearTimeout(timeout);
      if ($activeMegaMenu && !$activeMegaMenu.is($(this).children(".mega_menu"))) {
        // $activeMegaMenu.slideUp();
        $activeMegaMenu.removeClass("active");
      }
      var $megaMenu = $(this).children(".mega_menu");
      timeout = setTimeout(function () {
        // $megaMenu.slideDown();
        $megaMenu.addClass("active");
        $("body").addClass("overlay");
        $activeMegaMenu = $megaMenu;
      }, 100);
    })
    .mouseleave(function () {
      clearTimeout(timeout);
      var $megaMenu = $(this).children(".mega_menu");
      timeout = setTimeout(function () {
        // $megaMenu.slideUp();
        $megaMenu.removeClass("active");
        $("body").removeClass("overlay");
      }, 100);
    });
  $(document).mouseleave(function () {
    if ($activeMegaMenu) {
      // $activeMegaMenu.slideUp();
      $megaMenu.removeClass("active");
      $("body").removeClass("overlay");
    }
  });
  //グロナビ　ハンバーガー
  $(document).on("touchstart", ".hamburger", function (e) {
    const $t = $(e.currentTarget);
    $t.toggleClass("open");
    $t.closest(".global_nav").toggleClass("open");
    // bodyをスクロール不可にする
    if ($t.hasClass("open")) {
      window.addEventListener("touchmove.noScroll", function (e) {
        e.preventDefault();
      });
      document.querySelector("body").style.overflow = "hidden";
    } else {
      window.removeEventListener("touchmove.noScroll", function (e) {
        e.preventDefault();
      });
      document.querySelector("body").style.overflow = "auto";
    }
  });

  //アコーディオン
  $(document).on("click", ".ac_ttl", function (e) {
    const $t = $(e.currentTarget);
    const $next = $t.next();
    if ($next.length > 0) {
      $t.toggleClass("open");
      $next.slideToggle();
      return false;
    }
  });

  // SPのみアコーディオン
  var width = $(window).width();
  $(".ac_sp > .ttl").toggleClass("ac_ttl", width <= 767);
  $(window).resize(function () {
    var width = $(window).width();
    $(".ac_sp > .ttl").toggleClass("ac_ttl", width <= 767);
  });

  // 2025アコーディオン
  jQuery(document).ready(function ($) {
    // PC（幅768px以上）の場合は aco_box--pc-open を開いた状態にする
    if ($(window).width() >= 768) {
      $(".aco_box--pc-open").addClass("open").find(".aco_content").show();
    }

    $(".aco_head").on("click", function () {
      var $accordion = $(this).closest(".aco_box");
      var $content = $accordion.find(".aco_content");

      $content.stop(true, true).slideToggle(300); // スライドアニメーション
      $accordion.toggleClass("open");
    });
  });

  //top01
  $(window).scroll(function () {
    $(".top01_list li").each(function () {
      var hit = $(this).offset().top;
      var scroll = $(window).scrollTop();
      var wHeight = $(window).height();
      var customTop = 200;
      if (typeof $(this).data("effect") !== "undefined") {
        // data-effect="任意の値" が設定されている場合
        customTop = $(this).data("effect");
      }
      if (hit + customTop < wHeight + scroll) {
        $(this).addClass("active");
      }
    });
  });

  //hover不可の時
  // $(window).on('load resize', function () {
  //     var winW = $(window).width();
  //     var devW = 767;
  //     if (winW <= devW) {
  //         $(function () {
  //             $(".main_nav > .mega_btn").on("click", function () {
  //                 $(this).children('.mega_menu').slideToggle();
  //             });
  //         });
  //     } else {
  //         $(function () {
  //             $('.main_nav > .mega_btn').hover(function () {
  //                 $(this).children('.mega_menu').stop().slideToggle();
  //                 $('body').toggleClass('overlay');
  //             });
  //         });
  //     }
  // });

  // スムーススクロール
  $('a[href^="#"]').click(function () {
    var speed = 400;
    var href = $(this).attr("href");
    var target = $(href == "#" || href == "" ? "html" : href);
    var headerHeight = $("header").outerHeight() || 0;
    var position = target.offset().top - headerHeight;
    $("body,html").animate({ scrollTop: position }, speed, "swing");
    return false;
  });

  // フッター追従
  var footerFix = $(".footer_fix");
  var btn = $(".footer_fix .to_top");
  $(window).on("load scroll", function () {
    if ($(this).scrollTop() > 500) {
      footerFix.addClass("active");
    } else {
      footerFix.removeClass("active");
    }
  });
  btn.on("click", function () {
    $("body,html").animate({
      scrollTop: 0
    });
  });

  // スクロールヒント
  new ScrollHint(".js-scrollable", {
    i18n: {
      scrollable: ""
    }
  });

  // 営業所詳細のfv改行
  const breakpoint = 767.1;
  $(window).on("resize", function () {
    if ($(window).width() > breakpoint) {
      $(".page_fv .store_ttl:not(.no_js)").html(function () {
        const text = $(this).text();
        return text.replace("クラシアン", "クラシアン<br>");
      });
    } else {
      $(".page_fv .store_ttl:not(.no_js)").html(function () {
        const text = $(this).text();
        return text.replace("<br>", "");
      });
    }
  });
  $(window).trigger("resize");

  // 営業所詳細のコメントの見出しから「クラシアン」削除
  $(".comment_ttl").each(function () {
    const text = $(this).text();
    if (text.includes("クラシアン")) {
      $(this).text(text.replace("クラシアン", ""));
    }
  });
});

//郵便番号フォームのPC/SPでの出しわけ
// $(function(){
//   if ($(window).width() <= 767) {
//       $('.post_code').attr('placeholder', '0000000');
//       $('.post_code').attr('type', 'number');
//   }
//   $(window).resize(function(){
//       if ($(window).width() <= 767) {
//           $('.post_code').attr('placeholder', '0000000');
//           $('.post_code').attr('type', 'number');
//       } else {
//           $('.post_code').attr('placeholder', '000-0000');
//           $('.post_code').attr('type', 'text');
//       }
//   });
// });
