
$(window).on('scroll', function(){
  if ($(window).scrollTop() > 180) {
    $('#anc_top').addClass('true');
  } else {
    $('#anc_top').removeClass('true');
  }
});


$(function(){

// #nav_gl
  const $body = $('body')
  const $header = $('#header');
  const $nav = $('#nav_gl');
  const fixedClass = 'stic ani';
  let _navHeight;

  function getHeight($e){
    return $e.outerHeight();
  }

  $(window).on('load scroll', function(e) {
    if (window.matchMedia('(max-width: 819px)').matches) {
      let scroll = $(this).scrollTop();
      const $navDef = $('#nav_gl:not(.stic)');
      _navHeight = getHeight($navDef);

          if ( scroll > getHeight($header) + getHeight($nav) ) {
              $nav.addClass(fixedClass);
              $body.css('margin-top', _navHeight);
          } else {
              $nav.removeClass(fixedClass);
              $body.css('margin-top', '0');
          }
    }
  });

  $(window).on('resize', function(e) {
    if (!window.matchMedia('(max-width: 819px)').matches) {
      $body.css('margin-top', '0');
    }
  });


// current & toggle
  const pathname = location.pathname;
  const hash = location.hash;
  const $navMenu = $('.nav_menu [class*=ctg_] a');
  const $toggle = $('.js_toggle');

  const openClass = '_open';
  const currentClass = '_current';
  const duration = 300;

  const navHashList = $.map($navMenu,function(element){
    if(element.hash) return element.pathname + element.hash;
  });

  // ページ遷移時
  let $currentElement;

  function addCurrent($e) {
    $e.addClass(currentClass);
    $e.closest('div').prev('.js_toggle').addClass(openClass).next('div').show();
  }

  $currentElement = $navMenu.filter(function(){
    const _this = this;
    return pathname + hash == _this.pathname + _this.hash;
  });

  if(!$currentElement.length){
    $currentElement = $navMenu.filter(function(){
      const _this = this;
      return _this.pathname + _this.hash == pathname;
    })
  }

  addCurrent($currentElement);

  // hashchange
  $(window).on('hashchange', function() {
    const _hash = location.hash;
    const matchNavHash = navHashList.some(function(_e){
      return _e == pathname + _hash;
    });
    let $currentElement;

    if(matchNavHash){
      $currentElement = $navMenu.filter(function(){
        const _this = this;
        return (pathname + _hash) == (_this.pathname + _this.hash)
      });

      $navMenu.removeClass(currentClass);
      addCurrent($currentElement);
    }
  });

  $toggle.on('click', function(){
    $(this).toggleClass(openClass);
    $(this).next('div').slideToggle(duration);
  });

// anc
  $('a[href^="#"]').on('click', function(){
    const speed = 500;
    const paddingPC = 15;
    const paddingSP = 60;
    const hash = this.hash;
    const $target = $(hash);
    let position;
    let scrollPadding;

    function setScroll (position, scrollPadding) {
      $('html, body').animate({scrollTop:position - scrollPadding}, speed, 'swing');
    }

    if(!$target.length) {
      return;
    }

    if(hash === '#header') {
      setScroll(0,0);
      return false;
    }

    position = $target.offset().top;
    scrollPadding = paddingPC;

    if(window.matchMedia('(max-width: 819px)').matches) {
      scrollPadding = paddingSP;
    }

    setScroll(position,scrollPadding);

  });

// modal
  $('.js_modal').modaal({
    type: 'iframe'
  });
  $('.js_modal_glos').modaal({
    type: 'iframe',
    width: 1000,
    height: 300
  });
  $('.js_modal_inline').modaal();
  $(".js_modal_video").modaal({
    type:'video'
  });

});

