jQuery(function ($) {
    var pagetop = $('#page_topper');
    pagetop.hide();
    $(window).scroll(function () {
        if ($(this).scrollTop() > 1200) {  
            pagetop.fadeIn();
        } else {
            pagetop.fadeOut();
        }
    });
    pagetop.click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 500); 
        return false;
    });

    // DOMが読み終わったら実行
    $(document).ready(function() {
        // ページビューカウント
        var post_ID = $('div.cardBody.contents').data('post-id');
        var post_type = $('div.cardBody.contents').data('post-type');
        if (post_type == "post") {
            $.ajax({
                type: 'POST',
                url: '/wp-admin/admin-ajax.php',
                cache: false,
                data: {
                    action: 'set_page_view_count',
                    post_id: post_ID,
                    post_type: post_type
                }
            });
        }

        // スマホ向け広告は削除
        $('div.ai-viewport-2').remove();
        // 広告が見出しの間にある場合は上に移動させる
        $('.ai-viewport-1').each(function (index, element) {
            if ($(this).prev().is('h2')) {
                $(this).insertBefore($(this).prev());
            } else if ($(this).prev().is('h3')) {
                if ($(this).prev().prev().is('h2')) {
                    $(this).insertBefore($(this).prev().prev());
                } else {
                    $(this).insertBefore($(this).prev());
                }
            }
        });
        $('.ai-viewport-1').each(function (index, element) {
            // 縦に連続する広告は削除
            if ($(this).next().hasClass('ai-viewport-1')) {
                $(this).next().addClass('delete_ad');
            }
            // 広告位置調整
            if ($(this).parents('.wp-block-media-text')) {
                $(this).insertBefore($(this).parents('.wp-block-media-text'));
            }
            if ($(this).parents('.wp-block-columns')) {
                $(this).insertBefore($(this).parents('.wp-block-columns'));
            }
        });
        $('.delete_ad').remove();

        // テーブルの横スクロール対応
        var center_width = $('#mainCont .center .contents').width();
        $('table').each(function (index, element) {
            // テーブル幅チェック、超えていたらdivで囲む（横スクロール表示用）
            if ($(this).width() > center_width) {
                $(this).wrap('<div>');
                $(this).parent().addClass('table_wrap');
                $(this).parent().append('<div class="table_scroll_hint">' +
                    '<p>横スクロールできます</p>' +
                    '<img src="https://' + window.location.hostname + '/wp-content/themes/wiki_theme/images/icon/swipe_icon.png">' +
                    '</div>');
            }
        });
        // スクロール後、横スクロールヒント非表示に
        $('.table_wrap').scroll(function () {
            $(this).find('div.table_scroll_hint').fadeOut();
        });
    });


    $('.tab').on('click',function(){
        const group = $(this).parents('.tabBody'); 
        group.find('.is-active').removeClass('is-active');
        $(this).addClass('is-active');
        group.find('.is-show').removeClass('is-show');
        
        const index = $(this).index();
        
        group.find(".panel").eq(index).addClass('is-show');
    });

    // 投票
	$('div.wp-polls div.listVote input[type="button"]').hover(function(){
        $('div.wp-polls div.listVote input[name=poll_' + $(this).attr('class') + ']').val([$(this).attr('name')]);
    });

	// 返信ボタン クリック時
	$('.comment-reply-link').click(function () {
        $('#comment').val('>>' + $(this).data('comment-no') + '\n');
        $('#comment').focus();
        $("html,body").animate({scrollBottom:$('#comment').offset().top});
    });

	// 返信先コメントNO クリック時
	$('span.reply-comment-no').click(function (e) {
        var target_comment_no = $(this).text(),
            target_offset = $(this).offset(),
        target_parent_comment_id = $(this).data('parent-id');
        target_comment_no = target_comment_no.replace(/[^0-9]/g, '');
        $('#comment_clone div').remove();
        $('#reply-comment-'+target_parent_comment_id + '-' + target_comment_no).clone(true).appendTo("#comment_clone");
        $('#comment_clone .comments-wrapper').css('border', 'none');
        $('#comment_clone .comments-wrapper').css('margin-bottom', '-10px');
        $("#comment_modal").css('top', target_offset.top);
        $("#comment_clone #reply-comment-" + target_parent_comment_id + "-" + target_comment_no).css('display','');
        // ダイアログを表示する
        $("#comment_modal").fadeIn(250);
    });
    $("#comment_modal_close").click(function () {
        $('#comment_modal').fadeOut(250);
        return false;
    });

    // 画像のモーダル表示
    $('.img_modal_open').on('click',function(){
        $('#img_modal_content img').remove();
        var full_size_img = $(this).prop('href');
        $("#img_modal_content").append("<img src='"+full_size_img+"' class='attachment'>");
        $('#img_modal').fadeIn(300);
        return false;
    });
    $('.img_modal_close, #img_modal_content').on('click',function(){
        $('#img_modal').fadeOut(300);
        return false;
    });

    // ゲームウィキ.jp共通ヘッダー：アカウント情報 ホバー時
    $('#gamewikijp_header #wp-admin-bar-gamewikijp-account').hover(
        function(){
            $(this).find('.ab-sub-wrapper').css('display', 'block');
        },
        function(){
            $(this).find('.ab-sub-wrapper').css('display', 'none');
        }
    );

    // ページ内リンク クリック時（ヘッダのズレ修正）
    var headerHight = 39; //ヘッダの高さ
    $('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;
    });

    // 記事のモバイルプレビュー
    $(document).on('click', '#wp-admin-bar-mobile', function(){
        var url = $(this).children("a").prop("href") + "&frame=true";
        $("#mobile_preview_modal #frame1").prop("src",url);
        $("#mobile_preview_modal").fadeIn(300);
        return false;
    });

    $('.mobile_preview_modal_close').on('click',function(){
        $('#mobile_preview_modal').fadeOut(300);
        return false;
    });

    // PCオーバーレイ閉じる処理
    $('#pc_overlay_close').click(function(){
        $('#pc_overlay').remove();
        return false;
    });

    // タームを開くボタン
    $('.term-open-icon').on('click',function () {
       if($(this).hasClass('ico-open')) {
           if ($(this).parent()[0] == $('ul.term-menu').last().prev()[0]) {
               $(this).parent().removeClass('term-open-li');
           }
           $(this).removeClass('ico-open');
           $(this).parent().next('ul.term-menu').removeClass('term-open');
       } else {
           if ($(this).parent()[0] == $('ul.term-menu').last().prev()[0]) {
               $(this).parent().addClass('term-open-li');
           }
           $(this).addClass('ico-open');
           $(this).parent().next('ul.term-menu').addClass('term-open');
       }
    });

    // アコーディオンメニューの展開
    $("label.acd-label").on("click", function() {
        $(this).next().slideToggle('fast');
    });
});