(function () {
  const TOOLS = window.TOOLS || {};
TOOLS.noopener = (function () {
  Array.prototype.forEach.call(document.querySelectorAll('a[target="_blank"]'), function(elem) {
    elem.setAttribute('rel','noopener');
  });
})();

TOOLS.links = (function () {
  Array.prototype.forEach.call(document.querySelectorAll('.js-link'), function(elem) {
    elem.addEventListener('click', function() {
      if (this.getAttribute('data-href')) location.href = this.getAttribute('data-href');
    });
  });
})();

TOOLS.lazy = (function () {
  tools_observer = lozad('.lazy');
  tools_observer.observe();
})();

TOOLS.smoothScroll = (function () {
  const anchorButtons = document.querySelectorAll('[data-anchor-to]');

  if (anchorButtons) {
    Array.prototype.forEach.call(anchorButtons, function(button) {
      const anchorTo = button.getAttribute('data-anchor-to');
      const anchor = document.querySelector('[data-anchor="' + anchorTo + '"');

      button.addEventListener('click', function (event) {
        event.preventDefault();
        anchor.scrollIntoView({
          behavior: 'smooth',
        });
      });
    });
  }
})();

TOOLS.tab = (function () {
  //.js-tab-button,.js-tab-contentsに共通の[data-tab="name"]を追加する　同じページで名前が重複しないように注意
  const tabs = document.querySelectorAll('.js-tab-button');
  function tabChange(){
    const data = this.dataset.tab
    const targetTabs = document.querySelectorAll('.js-tab-button[data-tab='+data+']');
    document.querySelectorAll('.js-tab-button[data-tab='+data+'].is-active')[0].classList.remove('is-active');
    this.classList.add('is-active');
    document.querySelectorAll('.js-tab-contents[data-tab='+data+'].is-show')[0].classList.remove('is-show');
    const arrayTabs = Array.prototype.slice.call(targetTabs);
    const index = arrayTabs.indexOf(this);
    document.querySelectorAll('.js-tab-contents[data-tab='+data+']')[index].classList.add('is-show');
  };
  for(let i = 0; i < tabs.length; i++) {
    tabs[i].addEventListener('click', tabChange, false);
  }
})();

TOOLS.loading = {
  show: function(){
    $('body').append('<div class="m-loader-wrap js-loader"><div class="m-loader"><div></div><div></div><div></div></div></div>');
  },
  hide: function(){
    $target = $('.js-loader');
    if($target.length > 0){
      $target.remove();
    }
  }
}

// レコメンド取得
TOOLS.getRecommend = function (params) {
  return new Promise(function (resolve, reject) {
    axios.get(TOOLS.recUrl, {
      adapter: axiosJsonpAdapter,
      params: params
    }).then(function(res) {
      console.log(res);
      if (res.status === 200) {
        resolve(res.data);
      } else {
        reject(res);
      }
    });
  });
};

// レコメンドクリック通知
TOOLS.notifyRecommend = (function () {
  $(document).on('click', '.js-notify', function () {
    var href = $(this).attr('href');
    axios.get(TOOLS.recNotifyUrl, {
      adapter: axiosJsonpAdapter,
      params: {
        prod: $(this).data('prod'),
        merch: 'tbsnews',
        spec: $(this).data('spec'),
        cookie: ISMLIB.ismuc,
        cref: $(this).data('cref')
      }
    }).then(function() {
      location.href = href;
    }).catch(function(res) {
      console.log(res);
    });

    return false;
  });
})();

// ストレージのversion
TOOLS.getVersion = function (name) {
  return name + TOOLS.versions[name];
};

TOOLS.signage = function() {
  var $signage = $('.js-signage');
  var $webTicker = $('.js-webticker');
  $signage.show();

  if ($webTicker.length > 0) {
    $webTicker.webTicker({
      hoverpause: false,
      duplicate: true, 
      startEmpty: false
    });
  }
}();

// 広告表示
TOOLS.toggleAd = function() {
  const closeBtnEl = document.querySelector(".js-ad-close");
  if (closeBtnEl) {
    closeBtnEl.addEventListener('click', function(e) {
      e.stopPropagation();
      if (closeBtnEl.parentElement) {
        closeBtnEl.parentElement.style.display = 'none';
      }
    }, true);
  }
}();

// ページトップに戻るボタン
TOOLS.backtotop = function() {
  $('.js-backto-top').click(function () {
    $('body, html').animate({ scrollTop: 0 }, 500);
  });
}();
  const MODULE = window.MODULE || {};
/* ヘッダー */
MODULE.header = (function() {
  var body = $('body');
  var $header = $('.js-header');
  var $toolsMain = $('.js-main');
  var $search = $('.js-header-search');
  var $menu = $('.js-header-menu');
  var $overlay =  $('.js-overlay');
  var $burgerBorder = $('.js-border');

  /* 上スクロールしたらヘッダーが表示される */
  var scroll = 0;
  $(window).on('scroll', function() {
    if($(this).scrollTop() > $header.outerHeight()) {
       body.addClass('is-fixed-header');
    } else {
      body.removeClass('is-fixed-header');
      $header.removeClass('--is-transition');
    }
  
    if($(this).scrollTop() < $header.outerHeight() + 200) {
      $header.removeClass('--is-active');
    } else if($(this).scrollTop() < scroll){
      $header.addClass('--is-active');
      $header.addClass('--is-transition');
    } else {
      $header.removeClass('--is-active');
    }

    scroll = $(this).scrollTop();
  });

  /* 検索フォームを表示ボタン */
  $('.js-search').click(function () {
    $toolsMain.addClass('--is-out');
    $search.addClass('--is-in');
  });

  /* 検索フォームを閉じるボタン */
  $('.js-closed').click(function () {
    $toolsMain.removeClass('--is-out');
    $search.removeClass('--is-in');
  });

  /* バーガーメニューのアイコンを変形&バーガーメニューを展開 */
  $('.js-burger').click(function () {
    $menu.toggleClass('--is-show');
    $overlay.toggleClass('--is-show');

    if ($menu.hasClass('--is-show')) {
      body.css('overflow', 'hidden');
    } else {
      body.css('overflow', 'visible');
    }

    $burgerBorder.toggleClass('--is-active');
  });

  /* バーガーメニュー展開中に周りの暗い部分をクリックしたらバーガーメニューを閉じる */
  $overlay.click(function(){
    $(this).removeClass('--is-show');
    $menu.removeClass('--is-show');
    body.css('overflow', 'visible');
    $burgerBorder.removeClass('--is-active');
  });
})();/* タブ */
MODULE.tab = (function() {
  $('.js-tab').on('click', function(){
    // タブのactive判定
    var tabWrap = $(this).parents('.js-tab-wrap');
    var tabs = tabWrap.find('.js-tab');
    for (i=0; i<tabs.length; i++) {
      if ($(tabs[i]).hasClass('--is-active')) {
        $(tabs[i]).removeClass('--is-active');
      }
    }
    $(this).addClass('--is-active');
    var contents = $(tabWrap).find('.js-tab-content');
    var tabNum = tabs.index(this);
    for (i=0; i<contents.length; i++) {
      tabNum == i ? $(contents[i]).addClass('--is-active') : $(contents[i]).removeClass('--is-active');
    }
  });
  
})();})();