// JavaScript Document
/*
*
*调用方法: Comment.loadComment("div id",{选项})
* "选项" 参照 下面的：_defaults
**/
var Comment = {
	//_debug : true,
	_debug : false,
	_target : "",//显示到目标DIV的id
	_pageno : 1,
	_pageCount: 1,
	//默认配置
	_defaults : {
				para:{type:"video",targetid:0,pageSize:6,pageno:1},
				contextPath : "/",	
				contentMaxLen:200,				//截取长度
				showPageBar:true,				//显示分页
				showTitle:false,				//是否显示标题 
				linkStyle:"{type}/{id}/" 		//标题连接的格式，自动前面加上contextPath 
				},
	//当前配置
	_currOption: { },
	//读取评论
	loadComment : function(target,option){
		option = jQuery.extend( this._defaults ,option );
		this._currOption =  option;
		this._target = target;
		jQuery.ajax({
				type:"GET",
				url: option.contextPath + "showComment.do",
				dataType:"xml",
				data:option.para,
				success:this.parseComment,
				error:function(){
						alert("读取评论失败！ ");
					}
				});
	},
	showFace : function (option){
		option = jQuery.extend( this._defaults ,option );
		var html = "<div>";
		for(i = 201 ;i <= 210 ; i++){
			html += "<img src='"+option.contextPath+"images/face/face" + i +".gif' width=24 height=24 onclick='insertFace("+i+")'/>"
		}
		html += "</div>";
	
		document.write(html);
	},
	/* 分页操作开始 */
	first :function(){
		var o = this._currOption;
		o.para.pageno = 1;
		this.loadComment(this._target,o);
	},
	pre :function (){
		var o = this._currOption;
		o.para.pageno = o.para.pageno - 1;
		this.loadComment(this._target,o);
	},
	next :function (){
		if (Comment._debug) alert(this._debug);
		var o = Comment._currOption;
		o.para.pageno = o.para.pageno + 1;
		
		this.loadComment(this._target,o);
	},
	last :function(){
		var o = this._currOption;
		o.para.pageno = this._pageCount;
		this.loadComment(this._target,o);
	},
	/* 分页操作开始 == end */
	//处理结果
	parseComment: function(xml){
		if (Comment._debug) alert(" parseComment xml:" + xml);
		$("#"+Comment._target+"").html("");
		
		var rowCount = parseInt($(xml).find("rowCount").text());
		var pageCount = parseInt($(xml).find("pageCount").text());
		var pageno = parseInt($(xml).find("pageno").text());
		var pageSize = parseInt($(xml).find("pageSize").text());
		var resultHtml ;
		if (rowCount == 0){
			return "还没有人发表评论呢！";
		}
		var o = Comment._currOption;
		Comment._pageno = pageno;
		Comment._pageCount = pageCount ;
		
		resultHtml = "<ol>";
		$(xml).find("comment").each(function(){
										var content = jQuery(this).find('content').text();
										var title = jQuery(this).find('targetName').text();
										title = title.length<6?title:title.substring(0,6)+"..";
										if (content.length > o.contentMaxLen){
											content = content.substring(0,o.contentMaxLen-3) + "...";
										}
										var regx=/\{(2\d\d)\}/;
										while (regx.test(content)){
											content = content.replace(regx,"<img src='"+o.contextPath+"images/face/face$1.gif' width=24 height=24/>");
										}
										var str = "<li>";
										str += "<div class=\"img\">";
										str += "<img title=\"\" src=\"" + o.contextPath +jQuery(this).find('face').text()+"\" width=40 heght=40 />";
										str += "</div><div class=\"text\">";
										str += "<span style=\"color:#900; font-weight:bold\">"+jQuery(this).find('userName').text()+"</span>";
										if (o.showTitle){
											var tmpUrl = o.linkStyle;
											tmpUrl = tmpUrl.replace("\{type\}",jQuery(this).find('type').text());
											tmpUrl = tmpUrl.replace("\{id\}",jQuery(this).find('targetid').text());
											str += " 评 [<a href='"+ o.contextPath + tmpUrl+"'>"+title+"</a>]：";
										}else{
											str += "["+jQuery(this).find('addTime').text()+"]评论说：";
										}
										str += "<br />"+content;
										str += "</div>";
										str += "</li>";
										resultHtml += str;
									});
		//分页
		if (o.showPageBar){
			var phtml = "<div>共有<b>"+rowCount+"</b>条评论  第"+pageno+"/"+pageCount+"页 ";
			if (pageno != 1){
				phtml += "<a href=\"javascript:\" onclick=\"Comment.first()\">首页</a> ";
				phtml += "<a href=\"javascript:\" onclick=\"Comment.pre()\">上一页</a> ";
			}
			if (pageno< pageCount){
				phtml += "<a href=\"javascript:\" onclick=\"Comment.next()\">下一页</a> ";
				phtml += "<a href=\"javascript:\" onclick=\"Comment.last()\">尾页</a> ";
			}
			phtml += "</div>";
			resultHtml += phtml;
		}
		resultHtml += "<ol>";
		
		$(resultHtml).appendTo($("#"+Comment._target+"")).show();

	}
	
};


//发表评论
function writeComment(path){
	if ($("#type").val() == "" || $("#tid").val() == ""){
		alert('参数错误！');	
		return ;
	}
	if ($("#commentstr").val() == "请输入评论内容"){
		alert("请输入评论内容！");
		$("#commentstr").focus();
		return ;
	}
	if ($("#commentstr").val() == "" || $("#commentstr").val().length > 50){
		alert("对不起，评论内容不能大于50个字符！");
		return ;
	}
	if (!path) path = "../../";
	var url = path + "writeComment.do";
	
	$.post(url,{type:$("#type").val(),targetid:$("#tid").val(),targetName:$("#targetName").val(),commentstr:$("#commentstr").val()},
		   function(xml){
			   var resultCode = parseInt($(xml).find("resultCode").text());
			   alert($(xml).find("mesg").text());
			   if (resultCode == 0){
				   $("#commentstr").val('');
				   //showCommentContent($("#type").val(),$("#tid").val(),1);
					Comment.loadComment(Comment._target,Comment._currOption);
			   }
			   
		   },"xml");
}

function insertFace(id){
	if ($("#commentstr").val() == "请输入评论内容") $("#commentstr").val("");
	$("#commentstr").val($("#commentstr").val() + "{"+id+"}");
}