
var slide_index;
var slide_timer;

!function ($) {
	$(function(){
		$('img.imgover').rollover('_on');
		$('.wp-caption').removeAttr('style');
		// $('a:not(.imgover)').find('img').addClass('imgfade');
		// $('img.imgfade').live('mouseover', function () {
		// 	$(this).animate({opacity:'0.85'}, {duration: 100});
		// });
		// $('img.imgfade').live('mouseout', function () {
		// 	$(this).animate({opacity:'1.0'}, {duration: 100});
		// });

		$('a.blank').on('click', function(){ this.target = '_blank'; });
		$('.lightbox').lightbox();

		// $('.wp_rp_thumbnail').addClass('img-small-xs').addClass('img-small-sm').addClass('img-small-md').addClass('img-small-lg');

		if ($('body.home').size()) {
			$('.attachment-thumbnail').addClass('img-thumbnail');
			$('#box-slider .thumb').each(function(){
				imgsrc = $('img', this).attr('src');
				$(this).css({'background-image': 'url(' + imgsrc + ')'}).html(' ');
				$(this).removeClass('hide');
			});
			// $('#box-slider .photo').each(function(){
			// 	imgsrc = $('img', this).attr('src');
			// 	$('a', this).css({'background-image': 'url(' + imgsrc + ')'}).html(' ');
			// });


			// set_main_slide(0);
			// $('#box-slider .thumb').on('click', function(){
			// 	set_main_slide(parseInt($(this).data('index')));
			// }).find('img').addClass('imgfade');

			$('.blog-post').find('img:not([class])').addClass('aligncenter');

			// $('#tabSet').scrollTabs();
			// $('#tabSet a').click(function (e){
			// 	e.preventDefault();
			// 	$('#box-pickup .active.in').removeClass('active').removeClass('in');
			// 	$('#box-pickup ' + $(this).attr('href')).addClass('active').addClass('in');
			// });
			$('.tab-pane .wpp-list').addClass('list-group');
			$('.tab-pane').each(function(){
				var rand = 1;
				$('.wpp-post-rank', this).each(function(){
					$(this).addClass('no' + rand).text(rand);
					rand++;
				});
			});

			//$('.blog-post').find('img').addClass('thumbnail');
			//$('body.home').find('img.attachment-thumbnail').addClass('thumbnail');
			//$('body.home').find('img.wpp_cached_thumb').addClass('thumbnail');
		}
		//$('#box-slider, #sidebar .widget_newpostcatch, #sidebar .popular-posts').find('img').addClass('thumbnail');

		// $("#sidebar").css({position: 'relative'});
		// var contentHeight = $("html, body").height();
		// var windowHeight = $(window).height();
		// var target = $(".fix_box");
		// var targetHeight = target.outerHeight();
		// var targetPosition = target.position();
		// var footer = $("#footer");
		// var footerHeight = footer.outerHeight();
		// $(window).resize(function(){
		// 	windowHeight = $(this).height();
		// });
		// $(window).scroll(function(){
		// 	var scrollTop = $(this).scrollTop();
		// 	var visibleBottom = scrollTop + windowHeight;
		// 	var targetBottom = targetPosition.top + targetHeight;
		// 	if(visibleBottom >= targetBottom) {
		// 		if(footerHeight > contentHeight - visibleBottom) {
		// 			target.css({width: target.width(), position:"fixed", bottom: footerHeight - (contentHeight - visibleBottom)});
		// 		} else {
		// 			target.css({width: target.width(), position:"fixed", bottom: 0});
		// 		}
		// 	} else {
		// 		target.css({position:"static", bottom: "auto"});
		// 	}
		// });

		$('.pagenation a').addClass('btn btn-primary');

		$('ul.pagination > li > span.active').each(function(){
			$(this).closest('li').addClass('active');
		});

		// if ($('ul.pagination > li.active ~ li:first > a').size()) {
		// 	link = $('ul.pagination > li.active ~ li:first > a').attr('href');
		// 	$('ul.pagination').before('<a href="' + link + '" class="btn btn-lg btn-primary">次のページへ</a><br /><br />');
		// }

        $('a[rel="lightbox"]').click(function(){
            window.open(this.href, '_blank');
            return false;
        });

		$("#back-top").hide();
		$(window).scroll(function () {
			if ($(this).scrollTop() > 100) {
				$('#back-top').fadeIn();
			} else {
				$('#back-top').fadeOut();
			}
		});
		// scroll body to 0px on click
		$('#back-top a, #back-top2 a').click(function () {
			$('body,html').animate({scrollTop: 0}, 600);
			return false;
		});

		if ($('ul.pagination').size()) {
			setTimeout(function(){
				$('.adwords').insertBefore($('ul.pagination'));
				$('a.adimg').removeClass('hide');
				$('#non-adwords').removeClass('hide');
			}, 1000);
		} else if ($('.btn-sosial').size()) {
			setTimeout(function(){
				$('.adwords').insertAfter($('.btn-sosial'));
				$('a.adimg').removeClass('hide');
				$('#non-adwords').removeClass('hide');
			}, 1000);
		} else if ($('.wp-pagenavi').size()) {
			setTimeout(function(){
				$('.adwords').insertAfter($('.wp-pagenavi'));
				$('a.adimg').removeClass('hide');
				$('#non-adwords').removeClass('hide');
			}, 1000);
		}

		if ($('body.home').size()) {
			setInterval(function(){
				$('.row-eq-height > div').css('height', 'auto');
				$('.row-eq-height').each(function(){
					var maxHeight = 0;
					parent = this;
					$(parent).children('div').each(function(){
						if (parseInt($(this).height()) <= maxHeight) return;
						maxHeight = parseInt($(this).height());
					});
					$(parent).children('div').height(maxHeight);
				});
			}, 2000);
		}
	});

    //////////////////////////////////////////////
    //
    //   facebook 埋め込み：レスポンシブ対応
    //
    //////////////////////////////////////////////
    if ($('.fb-page').length) {

        // iframeリロードの[ON/OFF]を切り替えるウィンドウサイズ。
        var reloadWidth = 768;

        $(function(){
            // まずウインドウの横幅を変数に入れる
            var timer = false;
            var winWidth = $(window).width();
            var winWidth_resized;
        
            // ウインドウのリサイズイベントに処理をバインド
            $(window).on("resize", function(){
                // リサイズ後の放置時間が指定ミリ秒以下なら何もしない(リサイズ中に何度も処理が行われるのを防ぐ)
                if (timer !== false) {
                    clearTimeout(timer);
                }
                // 放置時間が指定ミリ秒以上なので処理を実行
                timer = setTimeout(function() {
                // リサイズ後のウインドウの横幅を取得
                winWidth_resized = $(window).width();
        
                // リサイズ前のウインドウ幅とリサイズ後のウインドウ幅が異なる場合
                if ( winWidth != winWidth_resized ) {

                var windowWidth = parseInt($(window).width());
                if(windowWidth >= reloadWidth) {
                    // 画面サイズ大のとき
                    //location.reload();
                } else {
                    // 画面サイズ小のとき
                    location.reload();
                }

                // 次回以降使えるようにリサイズ後の幅をウインドウ幅に設定する
                winWidth = $(window).width();
                    }
                }, 200);
            });
        });
    }

}(window.jQuery)

function set_main_slide (index) {
	// clearTimeout(slide_timer);
	if (jQuery('#thumb_slide > div').size() < index + 1) index = 0;
	var box = jQuery('#thumb_slide > div:eq(' + index + ')');
	jQuery('#main_slide').fadeOut('fast', function(){
		jQuery('#main_slide').css({'background-image': 'url(' + jQuery('.photo img', box).attr('src') + ')'});
		jQuery('#main_slide').attr('href', jQuery('.title a', box).attr('href'));
		jQuery('#main_slide .title').text(jQuery('.title a', box).text());
        // jQuery('#main_slide').fadeIn('fast', function(){
        //     slide_timer = setTimeout(function(){ set_main_slide(index + 1); }, 3000);
        // });
        jQuery('#main_slide').fadeIn('fast');
	});
}
