'use strict';

(function () {
  // const navi = document.querySelector('.navi');
  // if (navi) {
  //   let naviPosY = window.pageYOffset + navi.getBoundingClientRect().top;
  //   let scrollY = window.pageYOffset;
  //   window.addEventListener('load', () => {
  //     naviPosY = window.pageYOffset + navi.getBoundingClientRect().top;
  //     scrollY = window.pageYOffset;
  //     if (scrollY < naviPosY) {
  //       navi.classList.remove('fixed');
  //     } else {
  //       navi.classList.add('fixed');
  //     }
  //     window.addEventListener('scroll', () => {
  //       scrollY = window.pageYOffset;
  //       if (scrollY < naviPosY) {
  //         navi.classList.remove('fixed');
  //       } else {
  //         navi.classList.add('fixed');
  //       }
  //     }, false);
  //   }, false);
  // }

  // 目次 アコーディオン
  var tocAccordionBtn = document.querySelectorAll('.toc-sublist-button');
  window.addEventListener('load', function () {
    if (tocAccordionBtn.length < 1) return false;
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
      for (var _iterator = tocAccordionBtn[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var tocAccordionItem = _step.value;

        var tocAccordionWrapper = tocAccordionItem.parentNode.parentNode;
        tocAccordionWrapper.classList.toggle('toc-sublist-open');
        tocAccordionItem.addEventListener('click', function (e) {
          e.target.parentNode.parentNode.classList.toggle('toc-sublist-open');
        }, false);
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.return) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }
  }, false);

  // 本文 アコーディオン
  var detailAccordionBtn = document.querySelectorAll('.accordion-button');
  window.addEventListener('load', function () {
    if (detailAccordionBtn.length < 1) return false;
    var _iteratorNormalCompletion2 = true;
    var _didIteratorError2 = false;
    var _iteratorError2 = undefined;

    try {
      for (var _iterator2 = detailAccordionBtn[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
        var detailAccordionItem = _step2.value;

        var detailAccordionWrapper = detailAccordionItem.parentNode.parentNode;
        detailAccordionWrapper.classList.toggle('accordion-open');
        var flg_session = false;
//        if(typeof window.sessionStorage !== 'undefined'){
//          if(('sessionStorage' in window) && (window.sessionStorage !== null)) {
//            flg_session = true;
//          }
//        }
//        if (flg_session) {
//          if (window.sessionStorage.getItem(detailAccordionWrapper.attributes.id.nodeValue) === 'true') {
//            detailAccordionWrapper.classList.toggle('accordion-open', true);
//          }
//        }
        
        detailAccordionItem.addEventListener('click', function (e) {
          if( e.target.classList.contains('accordion-button') ) {
            e.target.parentNode.parentNode.classList.toggle('accordion-open');
//            if (flg_session) {
//              window.sessionStorage.setItem(e.target.parentNode.parentNode.id,e.target.parentNode.parentNode.classList.contains('accordion-open'));
//            }
          } else {
            e.target.parentNode.parentNode.parentNode.classList.toggle('accordion-open');
//            if (flg_session) {
//              window.sessionStorage.setItem(e.target.parentNode.parentNode.parentNode.id,e.target.parentNode.parentNode.parentNode.classList.contains('accordion-open'));
//            }
          }
        }, false);
      }
    } catch (err) {
      _didIteratorError2 = true;
      _iteratorError2 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion2 && _iterator2.return) {
          _iterator2.return();
        }
      } finally {
        if (_didIteratorError2) {
          throw _iteratorError2;
        }
      }
    }
  }, false);

  /*-------------------------------------------------------------
   おすすめ機能
   -------------------------------------------------------------- */
  var toggleCards = function toggleCards(property, value) {
    property = property.replace(/ /g, '');
    if (typeof value == 'undefined') {
    	value = "";
    }
    var value = value.replace(/ /g, '');
    var cards = document.querySelectorAll('.js-function-cards-item');
    var classValue = property + '-' + value;
    if (classValue === 'category-all') {
      var _iteratorNormalCompletion3 = true;
      var _didIteratorError3 = false;
      var _iteratorError3 = undefined;

      try {
        for (var _iterator3 = cards[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
          var card = _step3.value;

          card.classList.remove("hide-category");
        }
      } catch (err) {
        _didIteratorError3 = true;
        _iteratorError3 = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion3 && _iterator3.return) {
            _iterator3.return();
          }
        } finally {
          if (_didIteratorError3) {
            throw _iteratorError3;
          }
        }
      }
      cardResize();
      return;
    }

    if (classValue === 'purpose-all') {
      var _iteratorNormalCompletion4 = true;
      var _didIteratorError4 = false;
      var _iteratorError4 = undefined;

      try {
        for (var _iterator4 = cards[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
          var _card = _step4.value;

          _card.classList.remove("hide-purpose");
        }
      } catch (err) {
        _didIteratorError4 = true;
        _iteratorError4 = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion4 && _iterator4.return) {
            _iterator4.return();
          }
        } finally {
          if (_didIteratorError4) {
            throw _iteratorError4;
          }
        }
      }
      cardResize();
      return;
    }

    var _iteratorNormalCompletion5 = true;
    var _didIteratorError5 = false;
    var _iteratorError5 = undefined;

    try {
      for (var _iterator5 = cards[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
        var _card2 = _step5.value;

        if (_card2.classList.contains(classValue)) {
          _card2.classList.remove('hide-' + property);
        } else {
          _card2.classList.add('hide-' + property);
        }
      }
    } catch (err) {
      _didIteratorError5 = true;
      _iteratorError5 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion5 && _iterator5.return) {
          _iterator5.return();
        }
      } finally {
        if (_didIteratorError5) {
          throw _iteratorError5;
        }
      }
    }
    cardResize();
  };

  // タブ切り替え
  var categoriesBtns = document.querySelectorAll('.js-function-category');
  if (categoriesBtns.length > 0) {

    // 初期化
	document.querySelector('input[name="radio"]').value = 'all';
	document.querySelector('select[name="select"]').value = 'all';
    var cards = document.querySelectorAll('.js-function-cards-item');
    
    window.addEventListener('load', function () {
      var _iteratorNormalCompletion6 = true;
      var _didIteratorError6 = false;
      var _iteratorError6 = undefined;

      try {
        var _loop = function _loop() {
          var categoriesBtn = _step6.value;

          var target = categoriesBtn;
          categoriesBtn.addEventListener('click', function () {
            var value = target.getAttribute('data-category');
            toggleCards("category", value);
            categoriesBtns.forEach(function(cB){
            	cB.classList.remove("current")
            });
            target.classList.add("current");
            if (value == "all"){
                var elem_radio = document.getElementsByClassName("function-radio_inputs_item");
                for (var idx_radio = 1; idx_radio < elem_radio.length; idx_radio++){
                  elem_radio[idx_radio].classList.remove("hide-radio");
                }
            } else {
	            var category_name = target.innerText.replace(/ /g, '');
	            
	            var elem_radio = document.getElementsByClassName("function-radio_inputs_item");
	            for (var idx_radio = 1; idx_radio < elem_radio.length; idx_radio++){
	              var radio_name = elem_radio[idx_radio].innerText.replace(/ /g, '');
	              elem_radio[idx_radio].classList.add("hide-radio");
	              
	              cards.forEach(function(card){
	                var flg_exist = false;
	                if (card.classList.contains("category-" + category_name)){
	                  if (card.classList.contains("purpose-" + radio_name)){
	                    flg_exist = true;
	                  }
	                }
	                
	                if (flg_exist == true){
	                  elem_radio[idx_radio].classList.remove("hide-radio");
	                }
	              });
	              
	              if (elem_radio[idx_radio].classList.contains("hide-radio")) {
	                for (var idx_child = 0; idx_child < elem_radio[idx_radio].childNodes.length; idx_child++) {
	                  if (elem_radio[idx_radio].childNodes[idx_child].checked == true) {
	                    elem_radio[0].childNodes[idx_child].checked = true;
	                    toggleCards("purpose", "all");
	                  }
	                }
	              }
	            }
	        }
          }, false);
        };

        for (var _iterator6 = categoriesBtns[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
          _loop();
        }
      } catch (err) {
        _didIteratorError6 = true;
        _iteratorError6 = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion6 && _iterator6.return) {
            _iterator6.return();
          }
        } finally {
          if (_didIteratorError6) {
            throw _iteratorError6;
          }
        }
      }
      cardResize();
    }, false);

    // ラジオボタン切り替え
    document.querySelectorAll('input[name="radio"]').forEach(function(cards){
	    cards.addEventListener('change', (event) => {
	      var value = cards.getAttribute("id");
	      toggleCards("purpose", value);
	      cardResize();
	    });
	});
    // リスト切り替え(縮小時のラジオボタン)
    var select_select = document.querySelector('select[name="select"]');
    select_select.addEventListener('change', (event) => {
      var value = select_select.value;
      toggleCards("purpose", value);
      cardResize();
    });
  }
  
  cardResize();
  
  // テキストのリサイズ(高さ調節) - ウィンドウサイズ変更ごとに設定
  window.addEventListener('resize', function(){
  	cardResize();
  },false);
  
})();

function cardResize(){
	var agent = window.navigator.userAgent.toLowerCase();
	var flg_chrome = false;
	if(agent.indexOf("msie") > -1){
		; // Edgeのエージェントにchromeの文字があるため、先にIE系列を除外
	} else if (agent.indexOf("chrome") > -1) {
		flg_chrome = true;
	}
	
	// テキストのリサイズ(高さ調節)
	var cardTexts = document.querySelectorAll('.function-cards_item_text');
	if (cardTexts.length > 0) {
		for ( var idx_text = 0; cardTexts.length > idx_text; idx_text++ ) {
			var tmp_font_size = 14;
			var tmp_line_height = 20;
			
			for ( var idx_child = 0; cardTexts[idx_text].childNodes.length > idx_child; idx_child++ ){
				if ( cardTexts[idx_text].childNodes[idx_child].nodeType == 1 ){ 
					// CSSに記載されている初期値を設定
					cardTexts[idx_text].childNodes[idx_child].style.fontSize = "14px";
					cardTexts[idx_text].childNodes[idx_child].style.lineHeight = "20px";
resize:
					while (1) {
						// 条件を満たすまで処理を繰り返す
						if ( cardTexts[idx_text].clientHeight > 90 ) {
							// パネルの高さが90を超える場合、文字サイズを0.9倍に縮小していく
							tmp_font_size = tmp_font_size * 0.9;
							tmp_line_height = tmp_line_height * 0.9;
							cardTexts[idx_text].childNodes[idx_child].style.fontSize = tmp_font_size + "px";
							cardTexts[idx_text].childNodes[idx_child].style.lineHeight = tmp_line_height + "px";
							if ( flg_chrome == true ) {
								if ( tmp_font_size < 10 ){
									// Chromeの場合のみ、一定値より小さくなると縮小を停止する
									// *Chromeは文字の表示を10pt未満にはしないため、中断する必要がある
									cardTexts[idx_text].childNodes[idx_child].style.fontSize = "10px";
									cardTexts[idx_text].childNodes[idx_child].style.lineHeight = "12px";
									break resize;
								}
							}
						} else {
							break resize;
						}
					}
				}
			}
		}
	}
	return;
}