function changeImg(obj){
	obj.src = obj.src + "?a=" + Math.random();
}
function copy(obj){
	obj.select();
	try{
		var copy = obj.value;
		if (window.clipboardData){
		window.clipboardData.setData("Text", copy);}
		else if (window.netscape){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=copy;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);}
		window.alert("链接复制成功，通过QQ/MSN发给您的朋友，邀请他们也来看看！");
		return false;
		}catch(e){
		window.alert("复制失败！可能原因不支持的浏览器!");
		return false;
	}
}
window.page = {};
window.page.currentUserNewnaiNum = 0;
window.page.slideAlbumId = 0;
window.page.setCurrentUser = function(newnaiNum){
	window.page.currentNewnaiNum = newnaiNum;
};
window.page.commentPageSize = 5;
window.page.commentCurrentPage = 0;
window.page.totalRootComment = 0;
window.page.commentTotalPage = 1;
window.page.comments = new Array();
window.page.rootComments = new Array();
window.page.AJAX_TOP_URL = "doSlideComment.do?method=top";
window.page._addComment = function(commentId, content, slideAlbumId, parentCommentId, 
	ipAddress, address, createTime, newnaiNum, nickName, headImg,
	replyCommentId, replyNewnainNum, replyUserNickName, topNum){
	var comment = {};
	comment.commentId = commentId;
	comment.content = decodeURI(content);
	comment.slideAlbumId = slideAlbumId;
	comment.parentCommentId = parentCommentId;
	comment.ipAddress = ipAddress;
	comment.address = decodeURI(address);
	comment.createTime = createTime;
	comment.newnaiNum = newnaiNum;
	comment.nickName = decodeURI(nickName);
	if(headImg == ''){
		headImg = "http://assert.newnai.com/youzhaopian.newnai.com/images/system/defaultHeadImg.jpg";
	}
	comment.headImg = headImg;
	comment.replyCommentId = replyCommentId;
	comment.replyNewnainNum = replyNewnainNum;
	comment.replyUserNickName = decodeURI(replyUserNickName);
	comment.topNum = topNum;
	if(parentCommentId == 0){
		window.page.rootComments.push(comment);
		window.page.totalRootComment = window.page.totalRootComment + 1;
	}else{
		window.page.comments.push(comment);
	}
};
window.page._generateCommentPager = function(commentPageSize){
	
	if(window.page.totalRootComment > 0){
		window.page.commentPageSize = commentPageSize;
		window.page.commentTotalPage = parseInt(((window.page.totalRootComment + commentPageSize - 1)/ commentPageSize), 10);
		var html = "<div id=\"comment_pager\" class=\"pgs_pager\"><a href=\"javascript:window.page._showCommentNextPage();\">查看更多评论"+
					"<span id=\"_commentpageNo\">(1/"+window.page.totalRootComment+")</span></a></div>";
		document.write(html);
		YAHOO.util.Event.on(window, "load", function(){window.page._showCommentNextPage(0)});
	}
};
window.page._showCommentNextPage = function(page){
	if(page){
		window.page.commentCurrentPage = page;
	}
	var length = window.page.commentPageSize;
	var end = false;
	if(window.page.rootComments.length < window.page.commentPageSize){
		length = window.page.rootComments.length;
		end = true;
	}
	var comment_list = document.getElementById("comment_list");
	for(var i = 0; i < length; i++){
		var comment = window.page.rootComments.shift();
		var node = window.page._generateCommentNode(comment);
		for(var m = window.page.comments.length - 1; m > -1; m--){
			var c = window.page.comments[m];
			if(c.parentCommentId == comment.commentId){
				var n = window.page._generateCommentNode(c);
				window.page.appendReplayComment(node, n);
			}
		}
		comment_list.appendChild(node);
	}
	if(end){
		YAHOO.util.Dom.setStyle("comment_pager", "display", "none");
	}else{
		window.page.commentCurrentPage = window.page.commentCurrentPage + 1;
		if(window.page.commentCurrentPage == window.page.commentTotalPage){
			YAHOO.util.Dom.setStyle("comment_pager", "display", "none");
		}else{
			document.getElementById("_commentpageNo").innerHTML = "("+window.page.commentCurrentPage+"/"+window.page.commentTotalPage+")";
		}
	}
	
};
window.page._removeCommentNode = function(commentId, parentCommentId){
	if (!window.confirm("\u60a8\u786e\u8ba4\u8981\u5220\u9664\u8be5\u6761\u8bc4\u8bba\u5417\uff1f")) {
		return;
	}
	var data = "commentid=" + commentId + "&parentCommentId=" + parentCommentId;
	var opt = {success:function (o) {
		var result = o.responseText;
		if (result == "ajax_failure") {
			alert("\u5220\u9664\u5931\u8d25");
		} else {
			var totalComment = 1;
			var id = "coment_" + commentId;
			var comment = document.getElementById(id);
			if(parentCommentId == 0){
				var ol = YAHOO.util.Dom.getElementsByClassName("comment-reply", "OL", comment)[0];
				var childrens = YAHOO.util.Dom.getElementsByClassName("pull-left", "DIV", ol).length;
				totalComment = totalComment + childrens;
			}
			var p = comment.parentNode;
			p.removeChild(comment);
			flashNum(0-totalComment);
		}
	}, failure:function (o) {
		window.alert("\u5bf9\u4e0d\u8d77\uff0c\u670d\u52a1\u5668\u6b63\u5fd9\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5\uff01");
	}};
	YAHOO.util.Connect.asyncRequest("POST", "doSlideComment.do?method=delete", opt, data);
};
window.page._generateCommentNode = function(comment){
	var li = document.createElement("LI");
	li.id = "coment_" + comment.commentId;
	
	var pull_left = document.createElement("DIV");
	YAHOO.util.Dom.addClass(pull_left, "pull-left");
	
	var head = document.createElement("IMG");
	head.src = comment.headImg;
	head.title = comment.nickName;
	YAHOO.util.Dom.setStyle(head, "width", "48px");
	YAHOO.util.Dom.setStyle(head, "height", "48px");
	if(comment.newnaiNum != 0){
		var hLink = document.createElement("A");
		hLink.href = "showUserHomePage.do?newnaiNum=" + comment.newnaiNum;
		hLink.title = comment.nickName;
		YAHOO.util.Event.on(head, "mouseover", function () {
			PersonElement.delayTimer(this, comment.newnaiNum);
		});
		hLink.appendChild(head);
		pull_left.appendChild(hLink);
	}else{
		pull_left.appendChild(head);
	}
	var action = document.createElement("P");
	YAHOO.util.Dom.addClass(action, "action-ding");
	var ding = document.createElement("A");
	ding.href = "javascript:void(0)";
	ding.onclick = new Function("window.page._topComment('"+comment.commentId+"', this)");
	ding.innerHTML = "顶";
	var count = document.createElement("SPAN");
	YAHOO.util.Dom.addClass(count, "count");
	count.innerHTML = comment.topNum;
	action.appendChild(ding);
	action.appendChild(document.createTextNode(" "));
	action.appendChild(count);
	pull_left.appendChild(action);
	li.appendChild(pull_left);
	
	var comment_content = document.createElement("DIV");
	YAHOO.util.Dom.addClass(comment_content, "comment-content");
	
	var h3 = document.createElement("H3");
	var who = document.createElement("SPAN");
	YAHOO.util.Dom.addClass(who, "who");
	if(comment.newnaiNum == 0){
		who.appendChild(document.createTextNode(comment.address));
		h3.appendChild(who);
		h3.appendChild(document.createTextNode(" "));
		
		var from_ip = document.createElement("SPAN");
		YAHOO.util.Dom.addClass(from_ip, "from-ip");
		from_ip.appendChild(document.createTextNode(comment.ipAddress));
		h3.appendChild(from_ip);
	}else{
		var a = document.createElement("A");
		a.href = "showUserHomePage.do?newnaiNum=" + comment.newnaiNum;
		a.appendChild(document.createTextNode(comment.nickName));
		who.appendChild(a);
		h3.appendChild(who);
	}
	var date = document.createElement("SPAN");
	YAHOO.util.Dom.addClass(date, "date");
	date.appendChild(document.createTextNode(comment.createTime));
	h3.appendChild(date);
	
	var doing = document.createElement("SPAN");
	if(comment.parentCommentId == 0 || (comment.replyNewnainNum == 0 && comment.replyUserNickName == '')){
		doing.appendChild(document.createTextNode(" 发表"));
		h3.appendChild(document.createTextNode(" "));
		h3.appendChild(doing);
	}else{
		doing.appendChild(document.createTextNode(" 回复"));
		h3.appendChild(document.createTextNode(" "));
		h3.appendChild(doing);
		h3.appendChild(document.createTextNode(" "));
		
		var rWho = document.createElement("SPAN");
		YAHOO.util.Dom.addClass(rWho, "who");
		if(comment.replyNewnainNum == 0){
			rWho.appendChild(document.createTextNode(comment.replyUserNickName));// + " ("+comment.ipAddress+")"
			h3.appendChild(rWho);
		}else{
			var a = document.createElement("A");
			a.href = "showUserHomePage.do?newnaiNum=" + comment.replyNewnainNum;
			a.appendChild(document.createTextNode(comment.replyUserNickName));
			rWho.appendChild(a);
			h3.appendChild(rWho);
		}
	}
	h3.appendChild(document.createTextNode(" 说："));
	comment_content.appendChild(h3);
	
	var content = document.createElement("P");
	var part = comment.content.split("\n");
	var context = "";
	for (var i = 0; i < part.length; i++) {
		context += part[i] + "<br/>";
	}
	context = context.substr(0, context.length - 5);
	content.innerHTML = context;
	comment_content.appendChild(content);
	
	var meta = document.createElement("DIV");
	YAHOO.util.Dom.addClass(meta, "meta");
	YAHOO.util.Dom.setStyle(meta, "visibility", "hidden");
	var actionLink = "（"
	//if(window.page.currentUserNewnaiNum == '' 
	//	|| window.page.currentUserNewnaiNum == '0' 
	//	|| window.page.currentUserNewnaiNum != comment.newnaiNum){
		commentId = comment.commentId;
		if(comment.parentCommentId != 0){
			commentId = comment.parentCommentId;
		}
		var nickName = comment.nickName;
		if(comment.newnaiNum == 0){
			nickName = comment.address;// + " ("+comment.ipAddress+")"
		}
		actionLink = actionLink + "<a href=\"javascript:window.page._replay('"+commentId+"','"+comment.newnaiNum+"','"+nickName+"');\">对Ta说</a>";
	//}
	if(window.page.currentUserNewnaiNum != '' 
		&& window.page.currentUserNewnaiNum != '0' 
		&& window.page.currentUserNewnaiNum == window.page.slideAlbumNewnaiNum){
		if(actionLink.length > 3){
			actionLink = actionLink + " | ";
		}
		actionLink = actionLink + "<a href=\"javascript:window.page._removeCommentNode('"+comment.commentId+"','"+comment.parentCommentId+"');\">删除</a>";
	}
	actionLink = actionLink + "）";
	meta.innerHTML = actionLink;
	comment_content.appendChild(meta);
	
	if(comment.parentCommentId == 0){
		var comment_reply = document.createElement("OL");
		YAHOO.util.Dom.addClass(comment_reply, "comment-reply");
		YAHOO.util.Dom.setStyle(comment_reply, "visibility", "visible");
		comment_content.appendChild(comment_reply);
	}
	
	li.appendChild(pull_left);
	li.appendChild(comment_content);
	YAHOO.util.Event.on(li, "mouseover", function(e){
		stopPopEvent(e);
		YAHOO.util.Dom.setStyle(meta, "visibility", "visible");
	});
	YAHOO.util.Event.on(li, "mouseout", function(e){
		stopPopEvent(e);
		YAHOO.util.Dom.setStyle(meta, "visibility", "hidden");
	});
	
	return li;
};
window.page.appendReplayComment = function(parentComment, comment){
	var ol = YAHOO.util.Dom.getElementsByClassName("comment-reply", "OL", parentComment)[0];
	YAHOO.util.Dom.setStyle(ol, "display", "block");
	ol.appendChild(comment);
};
window.page._topComment = function(commentId, link){
	var data = "commentid=" + commentId;
	var opt = {success:function (o) {
		var result = o.responseText;
		if (result == "ajax_failure") {
			alert("顶帖失败，注意您是否短时间内连续顶贴啦！");
		} else {
			var parent = link.parentNode;
			var count = YAHOO.util.Dom.getElementsByClassName("count", "SPAN", parent)[0];
			count.innerHTML = (parseInt(count.innerHTML, 10) + 1);
			var span = document.createElement("SPAN");
			span.appendChild(document.createTextNode("顶"));
			parent.insertBefore(span, link);
			parent.removeChild(link);
		}
	}, failure:function (o) {
		window.alert("对不起，服务器正忙，请稍后重试！");
	}};
	YAHOO.util.Connect.asyncRequest("POST", window.page.AJAX_TOP_URL, opt, data);
};
window.page._replay = function(parentCommentId, newnaiNum, photoNickName) {
	YAHOO.util.Dom.get("newnaiNum").value = newnaiNum;
	YAHOO.util.Dom.get("photoNickName").value = photoNickName;
	YAHOO.util.Dom.get("parentCommentId").value = parentCommentId;
	var span = YAHOO.util.Dom.get("replayUserName");
	span.innerHTML = "对 " + photoNickName + " 说：";
	YAHOO.util.Dom.setStyle(span.parentNode, "display", "block");
	window.location.hash="comment_focus";
	YAHOO.util.Dom.get("message").focus();
	
};
window.page._cancelReplay = function() {
	YAHOO.util.Dom.get("parentCommentId").value = "0";
	YAHOO.util.Dom.get("newnaiNum").value = "0";
	YAHOO.util.Dom.get("photoNickName").value = "";
	var span = YAHOO.util.Dom.get("replayUserName");
	span.innerHTML = "";
	YAHOO.util.Dom.setStyle(span.parentNode, "display", "none");
};
window.page._postComment = function (nickName, headImg) {
	var validCode = YAHOO.util.Dom.get("validCode").value;
	if(validCode.length == 0){
		alert("验证码不能为空！");
		window.location.hash="comment_focus";
		YAHOO.util.Dom.get("validCode").focus();
		return false;
	}
	var message = YAHOO.util.Dom.get("message").value;
	if (message.length < 1) {
		alert("\u8bf7\u8f93\u5165\u4e00\u4e9b\u8bc4\u8bba\u518d\u63d0\u4ea4\uff01");
		return false;
	}
	var p = message.split("\n");
	var context = "";
	for (var i = 0; i < p.length; i++) {
		context += p[i] + "<br/>";
	}
	context = context.substr(0, context.length - 5);
	if(context.length > NEWNAI_PARAMS.commentLength) {
		context = context.substr(0, NEWNAI_PARAMS.commentLength);
		alert("评论的长度应小于"+NEWNAI_PARAMS.commentLength+"个字，超出的部分系统将自动截取！");
	}
	var address = YAHOO.util.Dom.get("address").value;
	var ipAddress = YAHOO.util.Dom.get("ipAddress").value;
	var url = "doSlideComment.do?method=add";
	if(window.page.currentUserNewnaiNum == ''){
		window.page.currentUserNewnaiNum = '0';
	}
	var data = "newnaiNum=" + window.page.currentUserNewnaiNum + "&movieId=" + window.page.slideAlbumId
		+ "&content=" + message + "&address=" + address + "&validCode=" + validCode;
	var parentCommentId = YAHOO.util.Dom.get("parentCommentId").value;
	var replyNewnainNum = YAHOO.util.Dom.get("newnaiNum").value;
	var replyUserNickName = YAHOO.util.Dom.get("photoNickName").value;
	parentCommentId = YAHOO.util.Dom.get("parentCommentId").value;
	if(parentCommentId == ''){
		parentCommentId = 0;
	}
	if(replyNewnainNum==''){
		replyNewnainNum = 0;
	}
	data = data + "&replyNewnainNum=" + replyNewnainNum
		 + "&replyUserNickName=" + replyUserNickName + "&parentCommentId=" + parentCommentId;
	data = encodeURI(data);
	var opt = {success:function (o) {
		var result = o.responseText;
		if (result == "ajax_failure") {
			alert("\u6dfb\u52a0\u5931\u8d25 \u8bf7\u7a0d\u540e\u91cd\u8bd5!");
		} else if(result == "validCode_failure"){
			alert("验证码输入错误，请重新输入!");
			changeImg(document.getElementById('validateImg'));
		} else {
			alert("\u8bc4\u8bba\u53d1\u8868\u6210\u529f\uff01");
			window.location.reload();
		}
	}, failure:function (o) {
		window.alert("\u5bf9\u4e0d\u8d77\uff0c\u670d\u52a1\u5668\u6b63\u5fd9\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5\uff01");
	}};
	YAHOO.util.Connect.asyncRequest("POST", url, opt, data);
	return false;
};
window.page._commentid = 0;
window.page._modifyComment = function(commentId){
	window.page._commentid = commentId;
	var d = YAHOO.util.Dom;
	gPopinWins["selectPhotoPopin"]={
		win:null,
		props:{
			fixedcenter:true,
			constraintoviewport:true,
			underlay:"shadow",
			zindex:10000,
			close:true,
			visible:true,
			draggable:true,
			iframe:true,
			modal:true
		}
	};
	showModalPopin("selectPhotoPopin",520,250,null,"修改回复","ifr/comment-modify.jsp");
};
function loadCommentContent(){
	var content = YAHOO.util.Dom.get("content_" + window.page._commentid).innerHTML;
	var reg = new RegExp("<br>", "\g");
	content = content.replace(reg, "\r\n");
	reg = new RegExp("<BR>", "\g");
	content = content.replace(reg, "\r\n");
	return content;
}
function submitModifyComment(content){
	hidePopin("selectPhotoPopin");
	var data = "content=" + content + "&commentid=" + window.page._commentid;
	data = encodeURI(data);
	var opt = {success:function (o) {
		var result = o.responseText;
		if (result == "ajax_success") {
			alert("回复修改成功！");
			window.location.reload();
		} else {
			alert("修改失败!");
		}
	}, failure:function (o) {
		window.alert("\u5bf9\u4e0d\u8d77\uff0c\u670d\u52a1\u5668\u6b63\u5fd9\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5\uff01");
	}};
	YAHOO.util.Connect.asyncRequest("POST", "doSlideComment.do?method=modify", opt, data);
};
function stopPopEvent(e){
	e = e?e:window.event;
	if(document.all){
		e.cancelBubble = true;
	}else{
		e.stopPropagation();
		e.preventDefault();
	}
}
function showMeta(meta, e){
	stopPopEvent(e);
	YAHOO.util.Dom.setStyle(meta, "visibility", "visible");
}
function hiddenMeta(meta, e){
	stopPopEvent(e);
	YAHOO.util.Dom.setStyle(meta, "visibility", "hidden");
}

