﻿if(typeof CMS_URL == "undefined"){
	var CMS_URL = "";
}else{
	if(CMS_URL.charAt(CMS_URL.length - 1) == "/"){
		CMS_URL = CMS_URL.substring(0, CMS_URL.length - 1);
	}
	if(CMS_URL.indexOf(":8080") < 0){
		CMS_URL += ":8080";
	}
}
//显示评论
var commentaries = {
	vos:null,
	type:"",
	xml:null,
	oid:0,
	element:null,
	loader:null,
	size:5,
	regexpLT:new RegExp("<", "g"),
	regexpBR:new RegExp("\n", "g"),
	form:null,
	page:0,
	replace:function(v){
		return v.replace(this.regexpLT, "&lt;").replace(this.regexpBR, "<br/>");
	},
	show:function(name, date, content){
		return "<div class='commentary'><div><span class='name'>" +
			(name.length > 0? this.replace(name) : "匿名网友") + "</span> 发表于 " +
			date + "：</div>\n" +
			"<div class='content'>" + this.replace(content) + "</div></div>\n"
	},
	load:function(page){
		var url = CMS_URL + "/commentary.do?type=" + this.type + "&oid=" + this.oid +
			"&size=" + this.size + "&page=" + page + "&view=json&onload=parent.commentaries.onload";
		document.write("<script language='javascript' src='" + url + "'></script>");
	},
	onload:function(o){
		var s = "";
		if(o != null){
			this.page = o.page;
			var i = 0
			for(;; i ++){
				if(typeof o[i] == "undefined"){
					break;
				}
				s += this.show(o[i].name,
					o[i].dateDeploy.substring(0, 16),
					o[i].content);
			}
			if(s.length > 0){
				s += "<div class='more'>";
				if(this.page > 0){
					s += "<a href='javascript:void(commentaries.load(commentaries.page-1))'>上页</a> ";
				}
				if(i + o.from < o.max){
					//s += "<a href='javascript:void(commentaries.load(commentaries.page+1))'>下页</a>";
					s += "<a href='" + CMS_URL + "/commentary.do?type=" + this.type + "&oid=" + this.oid + "' target='_blank'>更多留言...</a>";
				}
				s += "</div>";
				this.vos = o;
			}
		}
		DOM.setEHTML(this.element, s.length == 0? "暂无评论" : s);
	},
	init:function(ele, type, oid){
		this.element = DOM.E(ele);
		this.type = type;
		this.oid = oid;
		if(this.element != null){
			this.load(0);
		}
		this.form = document.forms["commentaryDeployForm"];
		if(this.form != null){
			var user = decodeURI(HTML.C("cdb_user"));
			var uid = parseInt(user);
			var username = !isNaN(uid) && uid > 0? user.substring(new String(uid).length + 1) : "";
			this.form["name"].value = username;
			this.form["ownerId"].value = oid;
			this.form["ownerType"].value = type;
		}
	},
	//检查评论表单
	checkForm:function(form){
		if(this.form == null){
			this.form = form;
		}
		if(form["content"].value.length == 0){
			alert("请输入评论内容");
		}else{
			form["submit"].disabled = true;
			var x = new XML();
			x.post = "ownerType=" + this.type + "&ownerId=" + this.oid +
				"&name=" + escape(form["name"].value) + "&content=" + escape(form["content"].value);
			x.data = this;
			x.onload = this.onload_deploy;
			x.load(CMS_URL + "/commentary.do?action=create&view=js", false, true);
		}
		return false;
	},
	onload_deploy:function(doc, c){
		if(doc == "ok"){
			alert("评论发表成功");
			var s = c.show(c.form["name"].value, DateFormater.format(new Date(), "Y-m-d H:i:s"),
				c.form["content"].value);
			if(c.element.innerHTML.length < 10){
				c.element.innerHTML = s;
			}else{
				c.element.innerHTML = s + c.element.innerHTML;
			}
			c.form["content"].value = "";
		}else{
			alert("评论发表失败");
		}
		c.form["submit"].disabled = false;
	}
}
