﻿/*--
标题：留言控件
设计：王集鹄
博客：http://blog.csdn.net/zswang
日期：2009年6月7日
--*/

if (!Common.scripts["Comment"]) {
	//loadCss("/Scripts/Comment.css");
	Common.scripts["Comment"] = true;
}

function Comment(parent, url, key, contentLength, validateLength, caption) {
	var self = this;
	this.url = url;
	this.key = key;
	this.parent = parent ? parent : document.body;
	this.div_comment = document.createElement("div");
	
	this.button_comment = document.createElement("input");
	this.button_comment.type = "button";
	this.button_comment.value = caption ? caption : "我要评论";
	this.button_comment.onclick = function() {
		self.editor.textarea_editor.focus();
	}
	this.div_comment.appendChild(this.button_comment);
	
	this.div_comments = document.createElement("div");
	this.div_comment.appendChild(this.div_comments);

	this.button_next = document.createElement("input");
	this.button_next.type = "button";
	this.button_next.value = "显示下一组";
	this.button_next.onclick = function() {
		publics.counter('click_next', location);
		self.loadBook(loadBookIndex, 10);
	}
	this.div_comment.appendChild(this.button_next);
	
	this.editor = new UbbEditor(this.div_comment, "100%", 270, parseInt(contentLength));
	validateLength = +validateLength;
	if (validateLength) this.validate = new Validate(this.div_comment, validateLength);
	this.button_submit = document.createElement("input");
	this.button_submit.type = "button";
	this.button_submit.value = "提交";
	this.button_submit.onclick = function() {
		publics.counter("click_submit", location);
		var error = "";
		error += self.editor.getError("评论");
		if (self.validate) error += self.validate.getError();
		if (error != '') {
			alert(error);
			return;
		}
		
		var data = {};
		data.action = "submit";
		data.text = self.editor.getText();
		data.key = self.key;
		data.disable = self.editor.getDisable();
		if (self.validate) data.validate = self.validate.getValue();
		
		self.button_submit.disabled = true;
		requestHttp(self.url, "POST", json2Text(data), function(xmlhttp) {
			self.button_submit.disabled = false;
			try {
				var result = str2Object(xmlhttp.responseText);
				if (result.error) {
					alert(result.error);
					if (result.revalidate && self.validate) self.validate.update();
					return;
				}
				self.appendBook({
					username: publics.passinfo.username
					, userid: publics.passinfo.ident
					, money: publics.passinfo.money
					, index: "未知"
					, flag: publics.passinfo.flag
					, time: dateFormat(new Date(), "$1/$2/$3 $4:$5:$6")
					, content: data.text
					, format: data.disable ? "text" : "ubb"
				}, true);
				if (self.validate) self.validate.reset();
				alert("提交成功。");
			} catch(e) {
				alert(e.message);
			}
		});
	}
	this.div_comment.appendChild(this.button_submit);
	
	this.button_preview = document.createElement("input");
	this.button_preview.type = "button";
	this.button_preview.value = "预览";
	this.button_preview.onclick = function() {
		self.div_preview.innerHTML = format2Html(self.editor.getText(), self.editor.getDisable() ? "text" : "ubb");
	};
	this.div_comment.appendChild(this.button_preview);
	this.div_preview = document.createElement("div");
	this.div_comment.appendChild(this.div_preview);
	this.parent.appendChild(this.div_comment);
	this.loadBook(-1, 10);
}

Comment.prototype.appendBook = function(books, insert) {
	var self = this;
	var panel = document.createElement("div");
	var time = document.createElement("div");
	time.books = books;
	time.innerHTML = ubb2Html("[b]时间：[/b][time" + books.time + "]");
	var quoted = document.createElement("a");
	quoted.href = "javascript:;";
	quoted.onclick = function() {
		publics.counter('click_quote', location);
		self.editor.setSelectText("[quote=" + (books.username || books.address) +
			" 发表于：[time" + books.time + "]]" + books.content + "[/quote]\n");
	};
	quoted.innerHTML = "引用";
	quoted.style.marginLeft = "20px";
	time.appendChild(quoted);

	var top = document.createElement("a");
	top.href = "javascript:;";
	top.onclick = function() {
		publics.counter('click_top', location);
		scroll(0, 0);
	};
	top.innerHTML = "TOP";
	top.style.marginLeft = "20px";
	time.appendChild(top);

	time.style.backgroundColor = insert ? "#c0dcc0" : "#eeeeee";
	panel.appendChild(time);
	var user = document.createElement("div");
	user.style.backgroundColor = insert ? "#dcc0dc" : "#dddddd";
	user.innerHTML = ubb2Html(jsonFormat("[b]ID：[/b]${index} [b]用户：[/b] ${flag}${username}", books) +
		("money" in books ?
			jsonFormat(" [small][b]财富：[/b]${money} [b][url=/UserHome.aspx?id=${userid}]到访[/url][/b][/small]", books) : ""));
	panel.appendChild(user);
	var book = document.createElement("div");
	book.innerHTML = format2Html(books.content, books.format);
	book.style.paddingBottom = "50px";
	panel.appendChild(book);
	if (insert) {
		this.div_comments.insertBefore(panel, this.div_comments.firstChild);
		panel.scrollIntoView(true);
	} else this.div_comments.appendChild(panel);
}

Comment.prototype.loadBook = function(index, count) {
	var self = this;
	if (count <= 0) {
		self.button_next.disabled = false;
		return;
	}
	var data = {};			   
	data.action = "get";
	data.key = self.key;
	data.index = index;
	requestHttp(self.url, 'POST', json2Text(data), function(xmlhttp) {
		try {
			var result = str2Object(xmlhttp.responseText);
			if (result.error) {
				self.button_next.disabled = true;
				return;
			}
			self.appendBook(result);
			loadBookIndex = parseInt(result.index) - 1;
			if (loadBookIndex > 0)
				self.loadBook(loadBookIndex, count - 1);
			else self.button_next.disabled = true;
		} catch (e) {
			alert(e.message);
		}
	});
}