function $(id) { return document.getElementById(id); }
function $t(p, child) { return p.getElementsByTagName(child)[0].text; }
function $o(p, child) { return p.getElementsByTagName(child); }
var smpPost = function(url,proc,data) {
	XMLHttp.requestEx(url,"POST",proc,null,data,true,XMLHttp.ENC_FORM,null);
};

var Comments = function(fId, /*fTitle, */tItems, cPage, ppItems, dUrl) {
	this.ns = "_"+Math.round(Math.random()*1000);	// 命名空间
	this.flashId = fId;
	// this.flashTitle = fTitle;	// 暂时未用到
	this.cPage = cPage;	// 当前页码
	this.dataUrl = dUrl; // 数据地址
	this.proc = new proc1_hshq();
	this.proc.init(this.ns, cPage, tItems, ppItems, this.$, this.turnPage);
	this.init();
};
Comments.prototype = {
	$: function(id) { return document.getElementById(id+this.ns); },
	smpGet: function(url, proc) { XMLHttp.request(url,"GET",proc,null,null); },
	init: function() {
		this.turnPage.pageObj = this;
		this.seekData();
	},
	turnPage: function() {
		// this.onclick.pageObj.cPage = parseInt(this.getAttribute(this.onclick.pageObj.ns+"pg"));
		this.onclick.pageObj.cPage = parseInt(this.getAttribute("title"));
		this.onclick.pageObj.proc.cPage = this.onclick.pageObj.cPage;
		this.onclick.pageObj.seekData();
	},
	seekData: function() {
		var url = this.dataUrl;
		url += (url.indexOf('?')==-1 ? '?' : '&');
		url += ("FlashInfoID="+this.flashId+"&cpage="+this.cPage);
		//document.write(url);
		this.smpGet(url, this.proc);
		window.$("turnpage_hshq").style.display = "none";
		$("commentlist").innerHTML =
			"<div id='readstatus_hshq' style='color:red;text-align:center;margin-bottom: 5px;'>评论数据数据读取中。。。</div>";
	},
	setTItems: function(tItems) { this.proc.setTItems(tItems); }
};

var proc1_hshq = function() {
	this.ns = "";
	this.cPage = -1;
	this.tPage = -1;
	this.tItems = 0;	// 总项目数
	this.ppItems = 5;	// 每页项目数
};
proc1_hshq.prototype = {
	turnPage: null,
	init: function(ns, cPage, tItems, ppItems, $, turnPage) {
		this.ns = ns; this.cPage = cPage;
		this.ppItems = (ppItems<1? 5: ppItems);
		this.setTItems(tItems);
		this.$ = $; this.turnPage = turnPage;
		var ops = $o(window.$("turnpage_hshq"), "span");
		ops[0].onclick = ops[1].onclick = ops[2].onclick = ops[3].onclick = this.turnPage;
		ops[0].title = 1;
		window.$('tpage_hshq').innerHTML = this.tPage;
	},
	proc: function(xhr, obj) {
		var cs = xhr.responseXML.getElementsByTagName("comment");
		if(!cs) { alert("数据读取失败！"); return; }
		var len = cs.length;
		if(!len) { window.$("readstatus_hshq").innerHTML="暂无留言"; return; }
		var str = "";
		for(i=0; i<len; i++) {
			var c = cs[i];
			str += ("<div class='comment_cell" + (i%2?"2":"") + "' id='" + $t(c, "FlashCommentID") + "' style='padding-left:10px;'>");
			var tmp = $t(c, "user"); tmp = (tmp==null||tmp.length<1?"匿名":tmp);
			str += ("<div class='comment_info'><span>" + tmp + "</span> 来自：[" + $t(c, "from"));
			str += ("] 发表于：" + $t(c, "time") + "</div>");
			str += ("<div class='comment_c'>" + $t(c, "content").replace(/\[em:(...)\]/g, "<img src='/images/biaoqing/$1.gif'/>")
				+ "</div><div class='space'></div></div>");
		}
		$("commentlist").innerHTML = str;
		var o = window.$("turnpage_hshq");
		o.style.display = "inline";
		var ops = $o(o, "span");
		var ops = $o(window.$("turnpage_hshq"), "span");
		ops[0].style.display = (obj.cPage != 1?"":"none");
		ops[1].style.display = (obj.cPage != 1?"":"none"); ops[1].title = obj.cPage-1;
		ops[2].style.display = (obj.cPage != obj.tPage?"":"none"); ops[2].title = obj.cPage+1;
		ops[3].style.display = (obj.cPage != obj.tPage?"":"none"); ops[3].title = obj.tPage; // 也可以提交时候刷新 ****
		window.$('cpage_hshq').innerHTML = obj.cPage;
	},
	setTItems: function(tItems) {
		this.tItems = (tItems<1? 0: tItems);
		this.newTPage();
	},
	newTPage: function() {
		this.tPage = Math.floor(this.tItems/this.ppItems) + (this.tItems%this.ppItems? 1 : 0);	// 总页数
	}
};

var proc2_hshq = function() {
	this.comments = null;
};
proc2_hshq.prototype = {
	proc: function(xhr, obj) {
		if(xhr.responseText.toLowerCase()=="success") {
			var p1 = obj.comments.proc;
			p1.tItems += 1;
			p1.newTPage();
			p1.cPage = 1;
			// p1.turnPage();
			$o(window.$("turnpage_hshq"), "span")[0].onclick();
		} else {
			alert("留言失败，请稍后重试！");
		}
	}
};

var initCW = function(cont, name, submit, cs) {
	$(cont).cursor =$(cont).value.length;
	var insertBQ = function(e) {
		var r = $(cont);
		var v = r.value;
		r.value = v.substr(0, r.cursor) + this.value + v.substring(r.cursor, v.length);
		r.cursor += this.value.length;
	}
	var tmp = $o($('bq_hshq'), "img");
	for(i in tmp) tmp[i].onclick = insertBQ;
	$(cont).onclick = $(cont).onkeyup = function() {
		var rng = event.srcElement.createTextRange();
		rng.moveToPoint(event.x,event.y);
		rng.moveStart("character",-event.srcElement.value.length);
		this.cursor = rng.text.length;
	};
	$(submit).onclick = function(e) {
		var d = "";
		d += (name + '=' + $(name).value);
		d += "|/*";
		var v = $(cont).value;
		if(v==null||v.length<1) return alert("请输入留言内容");
		if(v!=null&&v.length > 250) return alert("内容太多");
		//if(v.length%2) 
		v += "|/*";
		d += ('&' + cont + '=' + v);
		var p2h = new proc2_hshq();
		p2h.comments = cs;
		$(name).value='';
		$(cont).value='';
		smpPost(url_newc+"?FlashInfoID=" + cs.flashId, p2h, d);
	};
}



