
function Reply(id, parentId, body, author, submitTime, mood) {
	this.id = id;
	this.parentId = parentId;
	this.body = body;
	this.author = author;
	this.submitTime = submitTime;
	this.mood = mood;
}
function ReplyList() {
	this.length = 0;
}
ReplyList.prototype.add = function (reply) {
	for (i = 0; i < this.length; i++) {
		if (this[i].id == reply.id) {
			this[i] = reply;//update
			return false;
		}
	}
	this[this.length] = reply;
	this.length++;
	return true;
};
ReplyList.prototype.remove = function (reply) {
	if (isNaN(reply.id)) {
		alert("NaN");
		return false;
	}
	for (i = 0, n = 0; i < this.length; i++) {
		if (this[i].id != reply.id) {
			this[n++] = this[i];
		}
	}
	if (n == i) {
		return false;//no such an item
	} else {
		this[this.length - 1] = null;
		this.length -= 1;
		return true;
	}
};
var commentArea;
var commentTemplate;
var contextPath;
var path;
var canDeleteComment = false;
function insertOrUpdateReplyList() {
	while (commentTemplate.nextSibling) {
		commentArea.removeChild(commentTemplate.nextSibling);
	}
	for (i = 0; i < rList.length; i++) {
		var liNode = commentTemplate.cloneNode(true);
		liNode.style.display = "block";
		liNode.getElementsByTagName("img")[0].src = contextPath + "/images/smiley/" + rList[i].mood;
		liNode.getElementsByTagName("span")[0].innerHTML = rList[i].author.name;
		liNode.getElementsByTagName("span")[1].innerHTML = rList[i].body;
		liNode.getElementsByTagName("span")[2].innerHTML = rList[i].submitTime;
		liNode.getElementsByTagName("a")[0].replyId = rList[i].id;
		if (!canDeleteComment) {
			liNode.getElementsByTagName("a")[0].style.display = "none";
		}
		commentArea.appendChild(liNode);
	}
}
var emotionDiv;
var emotionTemplate;
var emotionPreviewer;
var chosenEmotion;
var emotions = [["angry.gif", "\u751f\u6c14"], ["beicuimian.gif", "\u88ab\u50ac\u7720"], ["bieshuole.gif", "\u522b\u8bf4\u4e86"], ["cheater.gif", "\u9a97\u5b50"], ["cowboy.gif", "\u725b\u4ed4"], ["faint.gif", "\u6655"], ["guilian.gif", "\u9b3c\u8138"], ["haqian.gif", "\u54c8\u6b20"], ["hug.gif", "\u62e5\u62b1"], ["jiyan.gif", "\u6324\u773c"], ["kenzhijia.gif", "\u5543\u6307\u7532"], ["kuanghuan.gif", "\u72c2\u6b22"], ["liezuixiao.gif", "\u54a7\u5634\u7b11"], ["liukoushui.gif", "\u6d41\u53e3\u6c34"], ["looser.gif", "\u5931\u8d25\u8005"], ["love.gif", "\u7231\u6155"], ["paomeiyan.gif", "\u629b\u5a9a\u773c"], ["pei.gif", "\u5478"], ["plaud.gif", "\u9f13\u638c"], ["sad.gif", "\u4f24\u5fc3"], ["secret.gif", "\u4fdd\u5bc6"], ["sigh.gif", "\u53f9\u6c14"], ["sleepy.gif", "\u660f\u660f\u6b32\u7761"], ["smile.gif", "\u5fae\u7b11"], ["sweating.gif", "\u6d41\u6c57"], ["thinking.gif", "\u601d\u8003"], ["waiting.gif", "\u7b49\u5f85"], ["xiaochou.gif", "\u5c0f\u4e11"], ["zhuanyanzhu.gif", "\u8f6c\u773c\u73e0"], ["zuoou.gif", "\u4f5c\u5455"]];
function initializeEmotionDiv() {
	for (i = 0; i < emotions.length; i++) {
		var emotionItem = emotionTemplate.cloneNode(true);
		emotionItem.style.display = "block";
		emotionItem.emotion = emotions[i][0];
		var eImg = emotionItem.getElementsByTagName("img")[0];
		eImg.src = contextPath + "/images/smiley/" + emotions[i][0];
		eImg.title = emotions[i][1];
		emotionDiv.appendChild(emotionItem);
	}
}
function showHideEmotionPanel() {
	emotionDiv.style.display = emotionDiv.style.display == "block" ? "none" : "block";
}
function chooseEmotion(item) {
	emotionPreviewer.src = contextPath + "/images/smiley/" + item.emotion;
	chosenEmotion.value = item.emotion;
	emotionDiv.style.display = "none";
}
function commentArticle(id) {
	replyContent = $("comment-body").value;
	smiley = $("chosen-emotion").value;
	if (replyContent == null || replyContent.trim().length == 0) {
		alert("\u8bc4\u8bed\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a");
		return false;
	}
	var commentURL = contextPath + "/group/" + path + "/commentArticle2/?id=" + id;
	var ajax = new Request.JSON({url:commentURL, method:"post", data:"replyContent=" + replyContent + "&smiley=" + smiley, onRequest:function () {
		emotionPreviewer.src = contextPath + "/images/smiley/smile.gif";
		chosenEmotion.value = "smile.gif";
		$("comment-body").value = "";
	}, onSuccess:function () {
		var jsonObj = JSON.decode(this.response.text);
			//alert(jsonObj.commentList.length);
			//var jsonObj=this.response.json;
		if (jsonObj.rtype == "success") {
			rList = jsonObj.commentList;
			rList.length -= 1;
			insertOrUpdateReplyList();
		} else {
			alert(jsonObj.msg);
		}
	}});
	ajax.send();
}
function deleteComment(replyId, id) {
	if (!window.confirm("\u60a8\u786e\u5b9a\u8981\u5220\u9664\u8be5\u6761\u8bc4\u8bba\u5417?")) {
		return;
	}
	var commentURL = contextPath + "/group/" + path + "/deleteComment";
	var ajax = new Request.JSON({url:commentURL, method:"post", data:"replyId=" + replyId + "&id=" + id, onSuccess:function () {
		var jsonObj = JSON.decode(this.response.text);
		if (jsonObj.rtype == "success") {
			rList = jsonObj.commentList;
			rList.length -= 1;
			insertOrUpdateReplyList();
		} else {
			alert(jsonObj.msg);
		}
	}});
	ajax.send();
}
function recommendArticle(id) {
	var recommendURL = "/ArticleRecommend.tr";
	var ajax = new Request.JSON({url:recommendURL, method:"post", data:"id=" + id, onSuccess:function () {
		var jsonObj = JSON.decode(this.response.text);
		//alert(this.response.text);
		if(jsonObj.code==100){
			alert("您成功地推荐了该篇文章!");
			$("recommend-total").innerHTML = parseInt($("recommend-total").innerHTML)+1;
		}else if(jsonObj.code==101){
			alert("您已经推荐过该篇文章。");
		}else if(code==102){
			alert("您还没有登录，请登录后再尝试。");
		}else{
			alert("出现错误，请稍候再尝试。如果问题仍然存在，请联系管理员。");
		}
	}});
	ajax.send();
}
function moveThisArticle(id) {
	var menuId = $("select-menu").options[$("select-menu").selectedIndex].value;
	var moveURL = contextPath + "/group/" + path + "/moveThisArticle/?id=" + id + "&toMenuId=" + menuId;
	document.location = moveURL;
}

function scrapArticle(id,trigger){
	new MUI.Window({
        id: 'scrapArticleMui',
    	title:'抓走文章',
        loadMethod: 'iframe',
        contentURL: "/ArticleScrap.tr?sourceId="+id,
        width: 390,
        height: 400,
		resizable: true,
		evalResponse: true
	});
}

function closeScrapArticle(){
	$("scrapArticleMui").retrieve('instance').close();
}
