$(function(){       
        $(".scrolltop img").click(function(){
		$("body,html").scrollTop(0);
	});
	
        /*align対応*/
        $(".article_contentblock *[align]").each(function(){
                var align = $(this).attr("align");
                $(this).css("text-align", align);
        });
        
        $(".newsdetail_detail_content *[align]").each(function(){
                var align = $(this).attr("align");
                $(this).css("text-align", align);
        });
        
	$(".article_contentblock img").each(function(){
                var w = $(this).attr("width");
                var h = $(this).attr("height");
                
                if(w != null) {
                    if(450 < w) {
                        $(this).width(450);
                        if(h != null) {
                            $(this).height(Math.round(h * (450 / w)));
                        }
                    }
                }
	});
        
	$(".article_contentblock iframe").each(function(){
                var w = $(this).attr("width");
                var h = $(this).attr("height");
                
                if(w != null) {
                    if(450 < w) {
                        $(this).width(450);
                        if(h != null) {
                            $(this).height(Math.round(h * (450 / w)));
                        }
                    }
                }
	});
        
        /* fancybox */
        $('.fancybox').fancybox();
        
        /* modalForm */
        $('.modalForm').on('click', function(event) {
            event.preventDefault();
            $('body').height($(window).height());
            $('body').addClass('noscroll');
            var formUrl = $(this).attr('href');
            $('#formiframe iframe').attr({src:formUrl});
            $('.mfForm').show();
        });
        
        $('.mfForm .close, .mfForm .modalBK').on('click', function(event) {
            event.preventDefault();
            $('.mfForm').hide();
            $('body').removeClass('noscroll');
        });
        
	$(window).load(function(){
		/*画像スライド*/
		if ( $(".slideimages_block").size() > 0 ) {
			var slidediv = $(".slideimages_block .slidescroll");
			var imagecount = slidediv.find("div.slidescrollcontent").size();
			var imagewidth = slidediv.find("div.slidescrollcontent").eq(0).outerWidth(true);
			var slidewidth = -imagewidth;
			//alert(imagecount);
			//alert(imagewidth);
			//slidediv.css("width", imagewidth*imagecount);
			
			//画像が3枚(動画有りのときは2枚)より多いときのみスライド実行
			var slidecount = $(".slideimages_block .movie").size() > 0 ? 2 : 3;
			if (  imagecount > slidecount ) {
				setInterval(function(){
					slidediv.animate({left:slidewidth}, 800, function () {
						slidediv.append(slidediv.find("div.slidescrollcontent").eq(0).clone());
						slidediv.find("div.slidescrollcontent").eq(0).remove();
						slidediv.css("left", 0);
					});
				},5000);//5秒ごとに動かす
			}
			
		}
		
		
		/*トップページキービジュアル*/
		if ( $(".slidercontent").size() > 1 ) {//画像が2枚以上のときのみ実行
			$(".slidercontrolcontent").eq(0).css("display", "block");
			for ( i =1; i < $(".slidercontent").size(); i++ ) {
				var contentname = "index" + i;
				$(".slidercontrol").append($(".slidercontrolcontent").eq(0).clone());
			}
			$(".slidercontrolcontent").eq(0).addClass("select");

			//キービジュアル自動切替
			var index1 = $(".slidercontent").index($(".slidercontent:visible"));
			var index2 = $(".slidercontent").size() > index1+1 ? index1+1 : 0;
			keyVisualTimer = setTimeout(function(){
				changeKeyVisual(index1, index2);
			},5000);//5秒ごとに動かす
		}
		
		/*キービジュアル手動切替*/
		$(".slidercontrolcontent").click(function(){
			clearTimeout(keyVisualTimer);
			var index1 = $(".slidercontent").index($(".slidercontent:visible"));
			var index2 = $(".slidercontrolcontent").index($(this));
			changeKeyVisual(index1, index2);
		});
		
		if (!($.support.borderRadius)) {
			//IEの角丸処理
			$("div.slidescrollcontent").corner("10px");
			$("div.slidercontent").corner("10px");
		}
	});
	
	// YouTube
        (function() {
            
            var triggerElement = $('.showYoutubeLayer');
            var playlistId = triggerElement.attr('id');
            var thumbnailElement = $('.youtubeThumbnail');
            var noVideoThumbnailUrl = function(id) {
                    return 'http://i.ytimg.com/vi/' + id + '/hqdefault.jpg';
            };
            var enableVideoLayer = function() {
                YTL.YouTubeLayerController.init(playlistId);
                triggerElement.unbind('click').bind('click', function(e) {
                    YTL.YouTubeLayerController.show();
                    return false;
                });
            };
            // サムネイル取得エラー時コールバック
            var errorHandler = function(response) {
                thumbnailElement.attr('src', noVideoThumbnailUrl(playlistId));
                enableVideoLayer();
            };
            // サムネイル取得成功時コールバック
            var successHandler = function(response) {
                // プレイリストサムネイルセット
                var thumbnailUrl = noVideoThumbnailUrl(playlistId);
                try {
                    thumbnailUrl = response.items[0].snippet.thumbnails.high.url;
                }catch(err) {
                }
                thumbnailElement.attr('src', thumbnailUrl);
                enableVideoLayer();
            };

            if ( triggerElement.size() > 0 ) {
                // サムネイル情報取得実行
                YTL.YouTubePlaylistData.getPlaylistInfoWithId(playlistId, YTL.YOUTUBE_API_KEY, successHandler, errorHandler);
            }
        }());

        // smartphone navigation
        (function() {
            var SHOW_NAVI1_CLASS_NAME = 'smartphoneNavi-top-show';
            var HIDE_NAVI1_CLASS_NAME = 'smartphoneNavi-top-hide';
            var SHOW_NAVI2_CLASS_NAME = 'smartphoneNavi-bottom-show';
            var HIDE_NAVI2_CLASS_NAME = 'smartphoneNavi-bottom-hide';
            
            var isAndroid = /android/.test(window.navigator.userAgent.toLowerCase());
            
            var pcGlobalNavi    = $('.menu');
            var pcLeftContent   = $('#left_block .content1');
            var smartphoneNavi1 = $('#smartphoneNavi-top');
            var smartphoneNavi2 = $('#smartphoneNavi-bottom');
            
            if ( smartphoneNavi1.size() === 0 || smartphoneNavi2.size() === 0 ) {
                return;
            }
            
            var smartphoneNavi1Inner = smartphoneNavi1.children('.smartphoneNavi_inner');
            var smartphoneNavi2Inner = smartphoneNavi2.children('.smartphoneNavi_inner');
            
            var pcGlobalNaviOffset = pcGlobalNavi.offset();
            var pcGlobalNaviHeight = pcGlobalNavi.height();
            
            var pcLeftContentOffset = pcLeftContent.offset();
            var pcLeftContentWidth = pcLeftContent.width();
            var pcLeftContentHeight = pcLeftContent.height();
            
            var showSmartphoneNavi1 = function() {
                smartphoneNavi1.removeClass(HIDE_NAVI1_CLASS_NAME).addClass(SHOW_NAVI1_CLASS_NAME);
            };
            var hideSmartphoneNavi1 = function() {
                smartphoneNavi1.removeClass(SHOW_NAVI1_CLASS_NAME).addClass(HIDE_NAVI1_CLASS_NAME);
            };
            var showSmartphoneNavi2 = function() {
                smartphoneNavi2.removeClass(HIDE_NAVI2_CLASS_NAME).addClass(SHOW_NAVI2_CLASS_NAME);
            };
            var hideSmartphoneNavi2 = function() {
                smartphoneNavi2.removeClass(SHOW_NAVI2_CLASS_NAME).addClass(HIDE_NAVI2_CLASS_NAME);
            };
            
            var navigationHandler = function() {
                var zoomer = document.body.clientWidth / window.innerWidth;
                
                if ( isAndroid ) {
                    var aaa = Math.floor((window.innerWidth - smartphoneNavi1Inner.width()) / 2);
                    var bbb = Math.floor((window.innerWidth - smartphoneNavi2Inner.width()) / 2);
                    smartphoneNavi1Inner.css('left', aaa + 'px');
                    smartphoneNavi2Inner.css('left', bbb + 'px');
                }
                
                if ( zoomer > 1.9 ) {
                    if ( smartphoneNavi1.hasClass(SHOW_NAVI1_CLASS_NAME) ) {
                        hideSmartphoneNavi1();
                    }
                    if ( smartphoneNavi2.hasClass(SHOW_NAVI2_CLASS_NAME) ) {
                        hideSmartphoneNavi2();
                    }
                } else {
                    if($(window).scrollTop() > pcGlobalNaviOffset.top + pcGlobalNaviHeight) {
                        showSmartphoneNavi1();
                    } else if ( smartphoneNavi1.hasClass(SHOW_NAVI1_CLASS_NAME) ) {
                        hideSmartphoneNavi1();
                    }

                    if($(window).scrollTop() > pcLeftContentOffset.top + pcLeftContentHeight - 70 || $(window).scrollLeft() > pcLeftContentWidth ) {
                        showSmartphoneNavi2();
                    } else if ( smartphoneNavi2.hasClass(SHOW_NAVI2_CLASS_NAME) ) {
                        hideSmartphoneNavi2();

                    }
                }
            };
            
            if ( isAndroid ) {
                smartphoneNavi1Inner.css('position', 'absolute');
                smartphoneNavi2Inner.css('position', 'absolute');
                $(window).bind('resize', setTimeout(navigationHandler, 250));
            }
            
            $(window).scroll(function () {
                clearTimeout($.data(this, 'scrollTimer'));
                $.data(this, 'scrollTimer', setTimeout(navigationHandler, 250));
            });
            
            navigationHandler();
            
        }());
}); 
var keyVisualTimer; 
function changeKeyVisual(index1, index2) {
	$(".slidercontent").eq(index1).css("z-index", "0");
	$(".slidercontent").eq(index2).css("z-index", "100").fadeTo(1000, 1, function(){
		$(".slidercontent").eq(index1).css("opacity", "0").css("display", "none");
	}).css("display", "block");
	$(".slidercontrolcontent").eq(index1).removeClass("select");
	$(".slidercontrolcontent").eq(index2).addClass("select");
	
	var index3 = $(".slidercontent").size() > index2+1 ? index2+1 : 0;
	keyVisualTimer = setTimeout(function(){
		changeKeyVisual(index2, index3);
	},5000);
}

window.onmessage = function(e){
    var msg = e.data;
    if(msg == "mfClose") {
        $('.mfForm').hide();
        $('body').removeClass('noscroll');
    }
}
