//TOPTHINK社区评论插件
(function($){
	$.fn.ThinkComment = function(options){		
		//默认配置
		var defaults = {
			"init_url"   : U("Comment/index"), //载入评论URL
			"more_url"   : U("Comment/getMore"), //载入更多列表URL
			"add_url"    : U("Comment/add"), //载入更多列表URL
			"verify_url" : U("Common/verify"), //验证码URL
			"more"       : "#ThinkCommentMore", //默认更多按钮选择器
			"publish"    : "#ThinkCommentPublish", //发布按钮选择器
			"content"    : "#ThinkCommentBody", //内容框选择器
			"list"       : "#ThinkCommentList", //评论列表选择器
			"verifyimg"  : "#ThinkCommentVerifyImg", //验证码图片选择器
			"verify"     : "#ThinkCommentVerify", //验证码表单
			"count"      : "#ThinkCommentCount",

			"onload"     : function(data){
								if(data.status){
									$(options.list).append(data.content);
									if(data.next && $(options.list).find("tr").length != $(options.count).html()){
										this.attr("next", data.next);
										this.html('<a href="javascript:void(0);" title="更多">更多</a>');
									} else {
										this.hide();
									}	
								} else {
									if(data.error) $.ThinkBox.error(data.error); //错误提示
									this.hide();
								}
							 }, //载入完成后执行的方法
			"added"      : function(data){
								if(data.status){ //评论发布成功
									self.find(options.list).prepend(data.body);
									self.find(options.count).html(parseInt(self.find(options.count).html())+1);
									self.find(options.content).val("");
									self.find(options.verify).val("");
									$.ThinkBox.success("发表评论成功！");
								} else {
									$.ThinkBox.error(data.error);
								}
							 }
		};
		
		var options = $.extend({}, defaults, options);
		var self    = $(this);
		var data    = {
				"model"  : self.attr("model"),
				"record" : self.attr("record"),
				"row"    : self.attr("row"),
				"list"   : self.attr("list") || 1,
				"edit"   : self.attr("edit") || 1
			};
		
		//加载评论
		self.load(options.init_url, data, function(){
			//加载完成后的操作 
			//给更多按钮绑定时间
			self.find(options.more).click(function(){
				self = $(this);
				self.html("评论加载中...");
				delete data.list; delete data.edit;
				data.row = self.attr("row"); //获取条数
				data.next = self.attr("next"); //最大ID
				$.post(options.more_url, data, function(data){options.onload.call(self, data);}, "json");
			});
			
			//发布评论
			self.find(options.publish).click(function(){
				var save = {
						"model"     : data.model,
						"record_id" : data.record,
						"quote_id"  : $(options.quote_id).val(),
						"content"   : $(options.content).val(),
						"verify"    : $(options.verify).val()
					};
				$.post(options.add_url, save, function(data){options.added.call(self, data);}, "json");
			});
			
			//设置验证码
			self.find(options.verifyimg).click(function(){
				this.src = options.verify_url + "?" + Math.random();
			});
			
			if(self.find(options.list).find("tr").length == self.find(options.count).html()){
				self.find(options.more).hide();
			}
		});
		
	};
})(jQuery);

//初始化评论
$(function(){$("#ThinkComment").ThinkComment();});
