$(document).ready(function(){
	//sidebar
	sidebar();
	function sidebar(){
		$('.three_recent_comments').bind('click',function(){
			raise('.d_recent_comments','.d_radom_posts','.d_popular_posts');
			$(this).css({'color':'violet'});
			$('.three_radom_posts').css({'color':'#757575'});
			$('.three_popular_posts').css({'color':'#757575'});
		});
		$('.three_radom_posts').bind('click',function(){
			raise('.d_radom_posts','.d_recent_comments','.d_popular_posts');
			$('.three_recent_comments').css({'color':'#757575'});
			$(this).css({'color':'violet'});
			$('.three_popular_posts').css({'color':'#757575'});
		});
		$('.three_popular_posts').bind('click',function(){
			raise('.d_popular_posts','.d_radom_posts','.d_recent_comments');
			$('.three_recent_comments').css({'color':'#757575'});
			$('.three_radom_posts').css({'color':'#757575'});
			$(this).css({'color':'violet'});
		});
	}
	function raise(elem1,elem2,elem3){
		$('.widget_d_three').find(elem1).css({'top':'68px','display':'block','opacity':'0'}).animate({top:38,opacity:1},{duration:400,queue:false});
		$('.widget_d_three').find(elem2).fadeOut('normal');
		$('.widget_d_three').find(elem3).fadeOut('normal');
	}
	
	//comment
	respond();
	function respond(){
		$('.reply').click(function(){
			var atid='"#'+$(this).parent().parent().attr("id")+'"';
			var atname=$(this).parent().find('.name').text();
			$('#comment').attr("value","<a href="+atid+">@"+atname+""+"</a>").focus();
		});
		$('.cancel_comment_reply a').click(function(){
			$('#comment').attr("value",'');
			
		});
	}
	
	//archives
	archives();
	function archives(){
		$('.archive_content').hide();
		$('.archive_content:first').show();
		$('.archive_date').click(function(){
			$(this).next().slideToggle('slow');
			return false;
		});
		$('#archives .archive_date').each(function(){
			var num=$(this).next().children('li').size();
			var text=$(this).text();
			$(this).html(text+' <span class="num">( '+num+' 篇文章 )</span>');
		});
		$('#archives').prepend('<span class="toggle_all">全部展开</span>');
		$('.toggle_all').toggle(function(){
			$('.archive_content').show();
			$('.toggle_all').text("全部折叠");
		},function(){
			$('.archive_content').hide();
			$('.toggle_all').text("全部展开");
		});
	}

	
	//ajax评论翻页
	comment_page_ajax();
	function comment_page_ajax(){
		$('.commentnavi a').click(function(){
			var wpurl=$(this).attr("href").split(/(\?|&)action=AjaxCommentsPage.*$/)[0];
			var commentPage = 1;
			if (/comment-page-/i.test(wpurl)) {
				commentPage = wpurl.split(/comment-page-/i)[1].split(/(\/|#|&).*$/)[0];
			} else if (/cpage=/i.test(wpurl)) {
				commentPage = wpurl.split(/cpage=/)[1].split(/(\/|#|&).*$/)[0];
			};
			//alert(commentPage);//获取页数
			var postId =$('#cp_post_id').text();
			//alert(postId);//获取postid
			var url = wpurl.split(/#.*$/)[0];
			url += /\?/i.test(wpurl) ? '&' : '?';
			url += 'action=AjaxCommentsPage&post=' + postId + '&page=' + commentPage;
			//alert(url);//看看传入参数是否正确
			var loading='<div class="commnav_loding">正在努力读取中......</div>';
			$.ajax({
				url:url,
				type: 'GET',
				beforeSend: function() {
					$('.commentlist').empty().html(loading);
					$body.animate( { scrollTop: $('#comments').offset().top - 200}, 1000);
				},
				error: function(request) {
						alert(request.responseText);
					},
				success:function(data){
					var responses=data.split('<!--winysky-AJAX-COMMENT-PAGE-->');
					$('.commentlist').empty().html(responses[0]).hide().fadeIn('slow');
					$('.commentnavi').empty().html(responses[1]);
					comment_page_ajax();//自身重载一次
					//single_js();//需要重载的js，注意
					respond();
					$('.commentlist img').lazyload({effect : "fadeIn"});
					//$body.animate( { scrollTop: $('#comments').offset().top - 200}, 1000);
				}//返回评论列表顶部
			});
			return false;
		});
	}
})