// source --> https://yell-navi.jp/js/scripts.js 
// レスポンシブブレークポイント
var ResponsiveBreakPoint = 640;

// UA判別
var _ua = (function(u){
  return {
    Tablet:(u.indexOf("windows") != -1 && u.indexOf("touch") != -1 && u.indexOf("tablet pc") == -1)
    || u.indexOf("ipad") != -1
    || (u.indexOf("android") != -1 && u.indexOf("mobile") == -1)
    || (u.indexOf("firefox") != -1 && u.indexOf("tablet") != -1)
    || u.indexOf("kindle") != -1
    || u.indexOf("silk") != -1
    || u.indexOf("playbook") != -1,
    Mobile:(u.indexOf("windows") != -1 && u.indexOf("phone") != -1)
    || u.indexOf("iphone") != -1
    || u.indexOf("ipod") != -1
    || (u.indexOf("android") != -1 && u.indexOf("mobile") != -1)
    || (u.indexOf("firefox") != -1 && u.indexOf("mobile") != -1)
    || u.indexOf("blackberry") != -1
  }
})(window.navigator.userAgent.toLowerCase());

// ウィンドウサイズ判別
var wid = window.matchMedia('(max-width: 640px)');

// ウィンドウサイズマッチでスマホ表示切替
jQuery(function(){
  if( wid.matches ){
    initializeStylesSMP();
  }
});

// ウィンドウリサイズ時に表示調整
var timer = false;
jQuery(window).resize(function() {
  if (timer !== false) {
    clearTimeout(timer);
  }
  timer = setTimeout(function() {
    if ( wid.matches ) {
      initializeStylesSMP();
    } else {
      initializeStylesPC();
    }
  }, 200);
});

// スマホ表示に切り替える
function initializeStylesSMP() {

  // スライドサイズを画面幅とパディング幅を元に設定
  var windowWidth = jQuery(window).width();
  var paddingWidth = parseInt( jQuery('#main_col').css('padding-left'), 10);
  var slideWidth = windowWidth - ( paddingWidth*2 + ( windowWidth - paddingWidth*2 ) * 2 / 100 );
  var slideHeight = slideWidth * 0.5076923076923077;
  jQuery('.item.slick-slide').css('height', slideHeight + 'px');
  jQuery('.item.slick-slide img').css('width', slideWidth + 'px');
  jQuery('.item.slick-slide img').css('height', slideHeight*0.98 + 'px');

}

// PC表示に切り替える
function initializeStylesPC() {
}

// メニュー開閉
function openmenu(obj) {
  parent_li = jQuery(obj).parent();
  parent_ul = parent_li.parent();
  if ( parent_ul.hasClass('visible') ) {
    parent_ul.removeClass('visible');
  } else {
    parent_ul.addClass('visible');
  }
}

// ========================================================================================================================
// Check Tag

function isUnknownElement(tag){
  var el = document.createElement(tag);
  if (tag.indexOf('-') > -1) {
    return (
      el.constructor === window.HTMLUnknownElement ||
      el.constructor === window.HTMLElement
    );
  } else {
    return /HTMLUnknownElement/.test(el.toString());
  }
}

// ========================================================================================================================
// Picturefill Helper

if ( isUnknownElement('picture') ) {

  output_mode = "";
  elm_pic = jQuery('picture');
  picturefill();
  jQuery(window).resize(function() {
    picturefill();
  });

  function picturefill() {
    var width = jQuery(window).innerWidth();
    if ( width > ResponsiveBreakPoint ) {

      if ( output_mode != "pc" ) {
        output_mode = "pc";
        elm_pic.each(function(){
          jQuery(this).children('img').removeAttr('width');
        });
      }

    } else {

      if ( output_mode != "sp" ) {
        output_mode = "sp";
        elm_pic.each(function(){
          var img = new Image();
          var imgsrc = jQuery(this).children('source[media*="'+ResponsiveBreakPoint+'px"]').attr('srcset');
          var pd = imgsrc.match(/ \dx$/)[0];
          if ( pd ) {
            imgsrc = imgsrc.replace(pd, '');
            pd = pd.charAt(1);
          } else {
            pd = 1;
          }
          img.src = imgsrc;
          img.setAttribute('data-pd', pd);
          var imgelm = jQuery(this).children('img');
          img.onload = function(){
            if ( output_mode == "sp" ) {
              imgelm.attr('width', ( img.width / pd ) + 'px' );
            }
          }
        });
      }
    }
  }
};