// JavaScript Document

jQuery.easing.quart = function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function(){
	
	nowDate = new Date();
	nowHour = nowDate.getHours();
	nowDay = nowDate.getDay();
	dayArray = new Array("日","月","火","水","木","金","土");

	
	if(nowDay >= 1 && nowDay <= 4 && nowHour >= 16 && nowHour < 19){
		jQuery("#city > a").click(function(){
			open_radiko();
		});
	}else if(nowDay == 2 && nowHour >= 1 && nowHour < 4){
		jQuery("#rover > a").click(function(){
			open_radiko();
		});
	}
	
	function open_radiko(){
		
		window.open(
			"http://radiko.jp/player/player.html#FMT",
			"radiko_player",
			"width=248, height=679, menubar=no, toolbar=no, scrollbars=no, resizable=no"
		);
		
		return false;
	}
	
	jQuery("#content .article .main_image, .more a").live("click", function(){
		
		var id = jQuery(this).parents(".article").attr("id");
		
		if(jQuery("#" + id + " .article_inner").css("display") == "none"){
			
			var scrollFlag = false;
		
			jQuery(".main_image img", "#content").show();
			jQuery(".main_image .video_object", "#content").hide();
			jQuery(".comment", "#content").hide();
			jQuery(".commentform", "#content").hide();
			jQuery(".article_foot", "#content").css("height", "");
			jQuery(".article_inner", "#content").slideUp();
			jQuery(".read:hidden, .more:hidden", "#content").slideDown("normal", function(){
				var targetOffset = jQuery("#" + id).offset().top - 10;
				jQuery("html, body").animate({ scrollTop : targetOffset }, 800, "quart");	
				scrollFlag = true;		
			});
		
			jQuery.ajax({
				type:"POST",
				url:"http://dplab.cc/wp-content/themes/new_dp/ajax_article.php",
				data:"post_id=" + id,
				beforeSend:function(){			
					var targetOffset = jQuery("#" + id).offset().top - 10;
					if(!scrollFlag){
						jQuery("html, body").animate({ scrollTop : targetOffset }, 800, "quart");
						jQuery(".read, .more", "#" + id + " > .article_foot").hide();
						jQuery("#" + id + " > .article_foot").append('<div class="ajax_load"><img src="http://dplab.cc/wp-content/themes/new_dp/images/ajax-loader.gif"></div>');
					}
				},
				success:function(data){
						if(jQuery("#" + id + " .main_image img").attr("src").match(/i.ytimg.com\/vi\/(\w+?)\/0.jpg/)){
							youtube_id = RegExp.$1;
							jQuery("#" + id + " .main_image img").hide();
							jQuery("#" + id + " .main_image").append('<div class="video_object"><object width="450" height="362"><param name="movie" value="http://www.youtube.com/v/' + youtube_id + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + youtube_id +'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="450" height="362"></embed></object></div>');
						}
						jQuery("#" + id + " > .article_foot .ajax_load").remove();
						jQuery("#" + id + " .article_foot").css("height", "auto").find(".full_text").html(data);
						jQuery("#" + id + " .article_inner").slideDown("slow", function(){
							jQuery("#" + id + " .comment").show();
						});
					}
			});
				
			return false;
			
		}
		
	});
	
	jQuery("#content .post_comment").click(function(){
		
		var id = jQuery(this).parents(".article").attr("id");
		jQuery("#" + id + " .commentform").fadeIn();
		
	});
	
	var archive_flag = false;
	var archive_no = 1;
	
	jQuery("#archive_left").click(function(){
		
		if(!archive_flag){
			
			archive_no--;
			archive_flag = true;
			
			jQuery("#archives_inner").animate({marginLeft: "+=240px"}, 500, "swing", function(){
				if(archive_no < 2){
					jQuery("#archive_left a").hide();
				}
				archive_flag = false;
				jQuery("#archive_center").html(archive_no);							
			});
		
		}
		
	});
	
	jQuery("#archive_right").click(function(){
		
		if(!archive_flag){
			
			archive_no++;
			
			if(!jQuery("#archive_" + archive_no).is("*")){
			
				jQuery.ajax({
					type:"POST",
					url:"http://dplab.cc/wp-content/themes/new_dp/ajax_archives.php",
					data:"page_no=" + archive_no,
					beforeSend:function(){
						archive_flag = true;
						jQuery("#archive_center").html('<img src="http://dplab.cc/wp-content/themes/new_dp/images/ajax-loader.gif">');				
					},
					success:function(data){
						jQuery("#archives_inner").append(data);
						jQuery("#archives_inner").animate({marginLeft: "-=240px"}, 500, "swing", function(){
							if(archive_no >= 2){
								jQuery("#archive_left a").show();
							}
							jQuery("#archive_center").html(archive_no);	
						});
					},
					complete:function(){
						archive_flag = false;
					}
				});
			
			}else{
				
				archive_flag = true;
				
				jQuery("#archives_inner").animate({marginLeft: "-=240px"}, 500, "swing", function(){
					if(archive_no >= 2){
						jQuery("#archive_left a").show();
					}
					archive_flag = false;
					jQuery("#archive_center").html(archive_no);	
				});
			
			}
		
		}
		
	});
	
	function archives_ajax(){
			
	}
	
//	jQuery("#pageNavi .wp-pagenavi a").live("click", function(){
//	
//		var page_no = jQuery("this").text();
//		var targetOffset = jQuery("#content").offset().top - 10;
//		
//		jQuery("html, body").animate({ scrollTop : targetOffset }, 800, "quart");	
//		
//		jQuery.ajax({
//			type:"POST",
//			url:"http://dplab.cc/wp-content/themes/new_dp/ajax_page.php",
//			data:"page_no=" + page_no,
//			beforeSend:function(){
//				jQuery("#content").empty();
//				jQuery("#content").append('<div class="ajax_load"><img src="http://dplab.cc/wp-content/themes/new_dp/images/ajax-loader.gif"></div>');
//			},
//			success:function(data){
//				jQuery("#content").html(data);
//			}
//		})
//		
//		return false;
//		
//	})

	//jTweetAnywareの設定
	
	jQuery('#topTweet').jTweetsAnywhere({
		searchParams: 'q=dplab',
		count: 6,
		showTweetFeed: {
        	paging: { mode: 'prev-next' }
    	}
	});

})
