// source --> https://e-yane.co.jp/wp-content/themes/taulabo/js/functions.js 
jQuery(function () {
    jQuery(window).load(function () {
        var fadeSpeed = 2500;
        jQuery('.fade_slow').each(function (i) {
            jQuery(this).delay(i * 400).animate({
                'opacity': 1
            }, fadeSpeed);
        });
    });
});

//スクロール時 ふわっと表示
$(function () {
    $(window).scroll(function () {
        $(".slide_off").each(function () {
            var imgPos = $(this).offset().top;
            var scroll = $(window).scrollTop();
            var windowHeight = $(window).height();
            if (scroll > imgPos - windowHeight + windowHeight / 6) {
                $(this).addClass("slide_on");
            } else {
                //スクロールを戻した場合、再アニメさせないため
                //$(this).removeClass("slide_on");
            }
        });
    });
});

//タイピング風演出
jQuery(window).on('load', function () {
    // ここから文字を<span></span>で囲む記述
    jQuery('.typ').children().andSelf().contents().each(function () {
        if (this.nodeType == 3) {
            jQuery(this).replaceWith($(this).text().replace(/(\S)/g, '<span>$1</span>'));
        }
    });
    // ここから一文字ずつフェードインさせる記述
    jQuery('.typ').css({ 'opacity': 1 });
    for (var i = 0; i <= $('.typ').children().size(); i++) {
        jQuery('.typ').children('span:eq(' + i + ')').delay(100 * i).animate({ 'opacity': 1 }, 700);
    }
});

//タイピング風演出2
jQuery(window).on('load', function () {

    setTimeout(function () {

        // ここから文字を<span></span>で囲む記述
        jQuery('.typ2').children().andSelf().contents().each(function () {
            if (this.nodeType == 3) {
                jQuery(this).replaceWith($(this).text().replace(/(\S)/g, '<span>$1</span>'));
            }
        });
        // ここから一文字ずつフェードインさせる記述
        jQuery('.typ2').css({ 'display': 'block', 'opacity': 1 });
        for (var i = 0; i <= $('.typ').children().size(); i++) {
            jQuery('.typ2').children('span:eq(' + i + ')').delay(120 * i).animate({ 'opacity': 1 }, 800);
        }

        setTimeout(function () {
            jQuery('.typ2').css({ 'opacity': 0 });
        }, 6200);

        setTimeout(function () {
            jQuery('.typ3').children().andSelf().contents().each(function () {
                if (this.nodeType == 3) {
                    jQuery(this).replaceWith($(this).text().replace(/(\S)/g, '<span>$1</span>'));
                }
            });
            // ここから一文字ずつフェードインさせる記述
            jQuery('.typ3').css({ 'opacity': 1 });
            for (var i = 0; i <= $('.typ').children().size(); i++) {
                jQuery('.typ3').children('span:eq(' + i + ')').delay(120 * i).animate({ 'opacity': 1 }, 800);
            }
        }, 6100);

        // setTimeout(function () {
        //   jQuery('.typ3').css({'opacity':0});
        // }, 13000);

    }, 4600);
});

// フッターのGoogleマップ
// 緯度経度と会社名或いはサイト名を2箇所書き換える

jQuery(document).ready(function ($) {
    function initialize() {
        var latlng = new google.maps.LatLng(32.840700, 129.974023);
        var myOptions = {
            zoom: 15,/*拡大比率*/
            scrollwheel: false,/*マウスホイールでの拡大縮小*/
            center: latlng,/*表示枠内の中心点*/
            mapTypeControlOptions: { mapTypeIds: ['sample', google.maps.MapTypeId.ROADMAP] }/*表示タイプの指定*/
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

        /*アイコン設定▼*/
        var icon = new google.maps.MarkerImage('/wp-content/themes/taulabo/images/icon/00_map_icon_1.png',
            new google.maps.Size(72, 72),/*アイコンサイズ設定*/
            new google.maps.Point(0, 0)/*アイコン位置設定*/
        );
        var markerOptions = {
            position: latlng,
            map: map,
            icon: icon,
            title: '共栄住建 株式会社'
        };
        var marker = new google.maps.Marker(markerOptions);
        /*アイコン設定ここまで▲*/

        /*取得スタイルの貼り付け*/
        var styleOptions = [
            {
                "elementType": "geometry",
                "stylers": [
                    {
                        "hue": "#ff4400"
                    },
                    {
                        "saturation": -68
                    },
                    {
                        "lightness": -4
                    },
                    {
                        "gamma": 0.72
                    }
                ]
            },
            {
                "featureType": "road",
                "elementType": "labels.icon"
            },
            {
                "featureType": "landscape.man_made",
                "elementType": "geometry",
                "stylers": [
                    {
                        "hue": "#0077ff"
                    },
                    {
                        "gamma": 3.1
                    }
                ]
            },
            {
                "featureType": "water",
                "stylers": [
                    {
                        "hue": "#00ccff"
                    },
                    {
                        "gamma": 0.44
                    },
                    {
                        "saturation": -33
                    }
                ]
            },
            {
                "featureType": "poi.park",
                "stylers": [
                    {
                        "hue": "#44ff00"
                    },
                    {
                        "saturation": -23
                    }
                ]
            },
            {
                "featureType": "water",
                "elementType": "labels.text.fill",
                "stylers": [
                    {
                        "hue": "#007fff"
                    },
                    {
                        "gamma": 0.77
                    },
                    {
                        "saturation": 65
                    },
                    {
                        "lightness": 99
                    }
                ]
            },
            {
                "featureType": "water",
                "elementType": "labels.text.stroke",
                "stylers": [
                    {
                        "gamma": 0.11
                    },
                    {
                        "weight": 5.6
                    },
                    {
                        "saturation": 99
                    },
                    {
                        "hue": "#0091ff"
                    },
                    {
                        "lightness": -86
                    }
                ]
            },
            {
                "featureType": "transit.line",
                "elementType": "geometry",
                "stylers": [
                    {
                        "lightness": -48
                    },
                    {
                        "hue": "#ff5e00"
                    },
                    {
                        "gamma": 1.2
                    },
                    {
                        "saturation": -23
                    }
                ]
            },
            {
                "featureType": "transit",
                "elementType": "labels.text.stroke",
                "stylers": [
                    {
                        "saturation": -64
                    },
                    {
                        "hue": "#ff9100"
                    },
                    {
                        "lightness": 16
                    },
                    {
                        "gamma": 0.47
                    },
                    {
                        "weight": 2.7
                    }
                ]
            }
        ];
        var styledMapOptions = { name: '共栄住建' }
        var sampleType = new google.maps.StyledMapType(styleOptions, styledMapOptions);
        map.mapTypes.set('sample', sampleType);
        map.setMapTypeId('sample');
    }

    // コンパチスクリプトを実行する ※重要
    window.addEventListener('load', function () {
        google.maps.event.addDomListener(window, 'load', initialize);
    });

    google.maps.event.addDomListener(window, 'load', initialize);
});

//スクロールで特定の位置に来たら処理を実行クラスを付与する
var posi_top, wih_half, current_view;
$(window).load(function () {
    var wih = window.innerHeight;
    var wih_half = wih / 1.2;
    current_view = wih_half;
    set_posi();
});
$(window).resize(function () {
    var wih = window.innerHeight;
    var wih_half = wih / 1.2;
    current_view = wih_half;
    set_posi();
});
$(window).scroll(function () {
    var wih = window.innerHeight;
    var wih_half = wih / 1.2;
    current_view = $(this).scrollTop() + wih_half;
    set_posi();
});

//特定のクラスを付与（主にアニメーションの為）
//付与したクラスにis-showする
function set_posi() {
    jQuery('h2, h3, h4, h5, h6, .doctor_career, .point_list, .list_box_medical, .flow, #hover-contact, .h4_title, .col-lg-6, .box_contact01,.contact-waku, .contact-flow, .tit_main01, .tit_sec01, .tit_sec02,.tit_uline ,.ul_reason li, .heading').each(function () {
        var posi = jQuery(this).offset();
        posi_top = posi.top;
        //初回のみ
        if (current_view > posi_top) {
            jQuery(this).addClass('is-show');
        }
    });

    jQuery('.demo_center_toggle').each(function () {
        var posi = jQuery(this).offset();
        posi_top = posi.top;
        //戻ればなんども
        if (current_view > posi_top) {
            jQuery(this).addClass('is-show');
        } else {
            jQuery(this).removeClass('is-show');
        }
    });
}

//メインロゴの1個目を表示→非表示、2個目を表示させる
jQuery(window).on('load', function () {
    setTimeout(function () {
        jQuery('.main-title-01').css({ 'opacity': 0 });
    }, 4200);
    setTimeout(function () {
        jQuery('.main-title-02').css({ 'opacity': 1 });
    }, 4300);
});

//ページトップ
$(function () {
    var topBtn = $('#page-top');
    //スクロールが100に達したらボタン表示
    $(window).scroll(function () {
        if ($(this).scrollTop() > 150) {
            topBtn.addClass('is-show');
        } else {
            topBtn.removeClass('is-show');
        }
    });
});

//画像サイズに合わせてTOP動画の読み込みを変更する
$(window).resize(function () {
    var w = $(window).width();
    var x = 768;
    if (w <= x) {
        $('.video_pc').css({
            display: 'none'
        });
        $('.video_sp').css({
            display: 'block'
        });
    } else {
        $('.video_pc').css({
            display: 'block'
        });
        $('.video_sp').css({
            display: 'none'
        });
    }
});


// header スクロールフェードイン
$(function() {
	var headerBg = $('#ghd');
	//スクロールが1に達したらボタン表示
	$(window).scroll(function () {
		if ($(this).scrollTop() > 1) {
			//ボタンの表示方法
			headerBg.addClass('scroll');
		} else {
			//ボタンの非表示方法
			headerBg.removeClass('scroll');
		}
	});
});

//headerの直ガキ 20200322

//コンタクトページでお問い合わせボタンを非表示にする
$(window).bind("load", function() {
  if (document.URL.match(/contact/)) {
    $('#hover-contact').css('display', 'none'),
      $('.foot-mail').css('display', 'none'),
      $('.foot-tel').css('display', 'none');
  }
});

//コンタクトページでお問い合わせボタンを非表示にする
$(window).bind("load", function() {
  if (document.URL.match(/recruit-entry/)) {
    $('#hover-contact').css('display', 'none'),
      $('.foot-mail').css('display', 'none'),
      $('.foot-tel').css('display', 'none');
  }
});


$(function() {
  setTimeout(function() {
    $(".top_logo").css({
      opacity: "0.0"
    }).animate({
      opacity: "1.0"
    }, 2600);
  }, 2000);
});
$(function() {
  // 現在ページのURL取得
  var url = location.href;
  if (url == "https://doinet.taulabo.com/") {
    setTimeout(function() {
      $(".header_menu").css({
        opacity: "0.0"
      }).animate({
        opacity: "1.0"
      }, 2600);
    }, 3000);
  } else {
    setTimeout(function() {
      $(".header_menu").css({
        opacity: "0.0"
      }).animate({
        opacity: "1.0"
      }, 2600);
    }, 400);
  }
});
$(function() {
  setTimeout(function() {
    $(".cover").css({
      opacity: "0.0"
    }).animate({
      opacity: "1.0"
    }, 2600);
  }, 8000);
});
$(function() {
  setTimeout(function() {
    $(".scroll-btn").css({
      opacity: "0.0"
    }).animate({
      opacity: "1.0"
    }, 2600);
  }, 7700);
});



//SPボタンによるロゴの表示・非表示の切り替え
$(function() {
  $('.navBtn').on('click', function() {
    $(this).toggleClass('active');
    $('nav').toggleClass('active');
    $('.sp-logo').toggleClass('active');
    $('header').toggleClass('active');
    $('#header_logo_sp').toggleClass('no-active');
    // if (.toggleClass == 'active') {
    // }
  });
});


$(function () {
var headerHight = 0; //ヘッダの高さ
$('a[href^=#]').click(function(){
    var href= $(this).attr("href");
      var target = $(href == "#" || href == "" ? 'html' : href);
       var position = target.offset().top-headerHight; //ヘッダの高さ分位置をずらす
    $("html, body").animate({scrollTop:position}, 550, "swing");//この数値は移動スピード
       return false;
  });
});

/*****************************************************
* Vendor
*****************************************************/
//
// Splide
//
let splideInitialized = false; // 重複初期化を防ぐフラグ

function initSplide() {
    // 既に初期化済みの場合は実行しない
    if (splideInitialized) {
        return;
    }
    
    if (typeof Splide === 'undefined') {
        console.log('Splide library not yet loaded, retrying...');
        setTimeout(initSplide, 100);
        return;
    }
    
    const splideElement = document.querySelector('.-home-hero-bnr .splide');
    if (!splideElement) {
        console.error('Splide element not found');
        return;
    }
    
    console.log('Initializing Splide...');
    
    Splide.defaults = {
        mediaQuery: 'min',
    };

    const splide = new Splide('.-home-hero-bnr .splide', {
        type: 'loop',
        speed: 1500,
        arrows: true, // arrow → arrows に修正
        pagination: true,
        autoplay: true,
        interval: 6000,
        pauseOnHover: true,
        focus: 'center',
        fixedWidth: '100%',
        gap: '.5rem',
        padding: {
            left: '10%',
            right: '10%'
        },
        breakpoints: {
            1024: {
                fixedWidth: '580px',
                padding: {
                    left: '40%',
                    right: '40%'
                },
                gap: '1rem'
            },
            768: {
                fixedWidth: '480px',
                padding: {
                    left: '10%',
                    right: '10%'
                },
                gap: '1rem'
            },
            560: {
                fixedWidth: '90%',
                padding: {
                    left: '5%',
                    right: '5%'
                },
                gap: '0.5rem'
            }
        }
    });
    
    try {
        splide.mount();
        splideInitialized = true; // 初期化完了フラグ
        console.log('Splide initialized successfully');
    } catch (error) {
        console.error('Splide initialization failed:', error);
    }
}

// 複数のタイミングで初期化を試行
document.addEventListener('DOMContentLoaded', initSplide);
window.addEventListener('load', initSplide);

// さらに保険として、少し遅延させて再試行
setTimeout(initSplide, 500);