jQuery(document).ready(function(){
	_initTopbar();
	for(var i=0,len=blogInfo.$moduleFuns.length;i<len;i++){
		_initModuleContent(blogInfo.$moduleFuns[i]);
	}
});
/**
 * 初始化topbar顶部导航
 */
function _initTopbar(){
	jQuery.get(BASEURL + "/blog/topbar","",function(data){
		jQuery("#topbar_menu").html(data);
	});
}

/**
 * 各模块数据的初始化
 */
function _initModuleContent(module){
	jQuery.get(BASEURL + "/blog/"+module.initFun+"/blogid/"+blogInfo.$blogid,(new Date).getTime(),function(htmlData){
		jQuery("#"+module.id+"_body").html(htmlData);
	});
}
/**
 * 初始化文章列表
 */
function _initArticleList(){
	showArticleList(0);
}
/**
 * 博文详细页面的初始化
 */
function _initArticleDetail(){
	showArticleState();
	showRelationLink();
	showCommentList(0);
}
/**
 * 文章列表
 */
function showArticleList(currentpage,params){
	jQuery.get(BASEURL + "/blog/articlelist/blogid/"+blogInfo.$blogid+"/currentpage/"+currentpage,(new Date).getTime(),function(htmlData){
		articleIdList = [];
		jQuery("#m_article_body").html(htmlData);
		showArticleState();
	});
}
/**
 * 列表文章统计信息
 */
function showArticleState(){
	jQuery.getJSON(BASEURL + "/blog/articlestate/articlelist/"+articleIdList.join("_"),(new Date).getTime(),function(json){
		for(var i=0,len=json.length;i<len;i++){
			jQuery("#num_reads_article_"+json[i].id).html(json[i].reads);
			jQuery("#num_nonspam_comments_article_"+json[i].id).html(json[i].comments);
			jQuery("#num_recommends_article_"+json[i].id).html(json[i].recommends);
		}
	});
}
/**
 * 文章评论信息
 */
function showCommentList(currentpage,params){
	jQuery.post(BASEURL + "/comment/list/blogid/"+blogInfo.$blogid+"/article/"+articleIdList.join("_")+"/currentpage/"+currentpage,(new Date).getTime(),function(htmlData){
		jQuery("#commentListContainer").html(htmlData);
		if(currentpage > 0){
			window.location.hash = "comment";
		}
	});
}
/**
 * 博文上下篇文章信息
 */
function showRelationLink(){
	jQuery.get(BASEURL + "/article/relationlink/article/" + articleIdList.join("_") + "/blogid/" + blogInfo.$blogid,(new Date).getTime(),function(htmlData){
		jQuery("#relationArticleInfo").html(htmlData);
	});
}
function recommendArticle(id,vtoken){
	jQuery.getJSON(BASEURL + "/article/recommend/article/" + id + "/blogid/" + blogInfo.$blogid+"/vtoken/"+vtoken,(new Date).getTime(),function(json){
		if(json.type == "ok"){
			jQuery("#num_recommends_article_"+id).html(parseInt(jQuery("#num_recommends_article_"+id).html()) + 1);
			jQuery("#recommendLink").removeAttr("href");
			jQuery("#recommendLink").removeAttr("onclick");
		}else if(json.type == "error"){
			if(json.value=="ever"){
				jsDialog.showMessage("_list","您已经推荐过该博文~~");
			}
		}
	});
}

function showFaceContainer(){
	alert ("功能待完善");
}
/**
 * 刷新验证码图片
 * @todo 30秒最小评论间隔验证，按钮的连击禁止~ ie下图片的js重置，对应信息的提示
 */
function changeCheckcode(codeImg){
	var $Obj = jQuery("#"+codeImg);
	$Obj.attr("src",$Obj.attr("source") + "/" + (new Date()).getTime())
}
function submitComment(){
	if(jQuery.trim(jQuery("#commentText").val()) == ""){
		alert ("请输入评论内容");jQuery("#commentText").focus();return;
	}
	if(jQuery("#checkcode").val() == ""){
		alert ("请输入验证码");jQuery("#checkcode").focus();return;
	}
	if(jQuery("#commentText").val().length > 1000){
		var params = {
			"type"		: "notice",
			"ok"		: true,
			"ok_fun"	: function(){
			},
			"cancel"	: false,
			"title"		: "提示",
			"text"		: "您的评论内容不能超过1000个字符，请修改后重新提交~~",
			"focus"		: "ok"
		};	
		jsDialog.init("_list",320,220).show("_list",params);	//alert,confirm,propt
	}
	jQuery("#btCommentAdd").attr("disabled",true);
	jQuery.post(BASEURL + "/comment/add","article="+ articleIdList.join("_") +"&anonyousNick="+encodeURIComponent(jQuery("#anonyousNick").val()) + "&checkcode=" + jQuery("#checkcode").val() + "&commentText=" + encodeURIComponent(jQuery("#commentText").val()) + "&vtoken=" + jQuery("#vtoken").val(),function(json){
		jQuery("#btCommentAdd").attr("disabled",false);
		if(json.type == "ok"){
			var params = json.params;
			if(params.value == "first"){
				if(params.total <= 1){
					jQuery("#commentListContainer").html(params.html);
				}else{
					jQuery("#commentListContainer").append(params.html)
				}
			}else if(params.value == "end"){
				showCommentList(params.page);
			}
			jQuery("#commentForm")[0].reset();
			changeCheckcode("checkcodeImg");
		}else if(json.type == "error"){
			if(json.value == "audit"){
				var params = {
					"type"		: "error",
					"ok"		: true,
					"ok_fun"	: function(){
					},
					"cancel"	: false,
					"title"		: "提示",
					"text"		: "评论中含有非法字符，请等待管理员审核。",
					"focus"		: "ok"
				};	
				jsDialog.init("_list",320,220).show("_list",params);	//alert,confirm,propt
				jQuery("#commentForm")[0].reset();
				changeCheckcode("checkcodeImg");
			}else if(json.value == "checkcode"){
				var params = {
					"type"		: "error",
					"ok"		: true,
					"ok_fun"	: function(){
						changeCheckcode("checkcodeImg");
						jQuery("#checkcode").val("");
						jQuery("#checkcode").focus();
					},
					"cancel"	: false,
					"title"		: "提示",
					"text"		: "验证码错误，请重新输入。",
					"focus"		: "ok"
				};	
				jsDialog.init("_list",320,220).show("_list",params);	//alert,confirm,propt
			}else if(json.value == "much"){
				var params = {
					"type"		: "notice",
					"ok"		: true,
					"ok_fun"	: function(){
					},
					"cancel"	: false,
					"title"		: "提示",
					"text"		: "对不起，您评论太频繁了，请稍候评论~~",
					"focus"		: "ok"
				};	
				jsDialog.init("_list",320,220).show("_list",params);	//alert,confirm,propt
			}else{
				var params = {
					"type"		: "notice",
					"ok"		: true,
					"ok_fun"	: function(){
					},
					"cancel"	: false,
					"title"		: "提示",
					"text"		: "内部错误，请刷新页面后重试~",
					"focus"		: "ok"
				};	
				jsDialog.init("_list",320,220).show("_list",params);	//alert,confirm,propt
			}
		}
		
	},"json");
}
/**
 * 加友情泡客
 */
function addFriendBlog(vtoken){
	jQuery.getJSON(BASEURL + "/blog/addfriend/blogid/"+blogInfo.$blogid+"/vtoken/"+vtoken,"",function(json){
		if(json.type == "ok"){
			var params = {
				"type"		: "ok",
				"ok"		: true,
				"cancel"	: false,
				"title"		: "提示",
				"text"		: "恭喜您，操作成功~~",
				"focus"		: "ok"
			};	
			jsDialog.init("_list",320,220).show("_list",params);	//alert,confirm,propt
		}else if(json.type == "error"){
			switch(json.value){
				case "noLogin":{
					blogLigin('friend');
					break;	
				}
				case "vtoken":{
					jsDialog.showMessage("_list","校验码错误~~");
					break;
				}
				case "me":{
					jsDialog.showMessage("_list","对不起，您不能加自己为友情泡客~~");
					break;
				}
				case "exists":{
					jsDialog.showMessage("_list","TA 已经是您的友情泡客了~~");
					break;
				}
				default:{
					jsDialog.showMessage("_list","对不起，参数错误，操作失败~~");
				}
			}
		}
	});
}
