var opuslistid;
var authorid;  //作者ID
var loginuser;  //观看者ID
$(function(){
	authorid = $("#authorid").val();
	opuslistid = $("#opusid").val();
	loginuser = $("#loginuser").val();
});
function turnpage(p) {
	t = new Date();
	$.getJSON("/ajaxaction.do?method=turnolcpage&t=" + t.getTime(), {"indexc":p, "opuslistid":opuslistid}, function(json) {
		var jlen = json.length-1;
		//更新页面评论
		var result = new Array();
		for (var i=0; i<jlen; i++) {
			var opuslistComment = json[i];
			result.push("<div class=\"text_box tb_type_video\">");
			if( opuslistComment.userid>0 )
			{
				result.push("<p class=\"room\"><span class=\"ttl\"><a title=\"\" href=\""+opuslistComment.commenterSpace+"\">"+opuslistComment.name+"</a>");
			}
			else
			{
				result.push("<p class=\"room\"><span class=\"ttl\">未知流星");
			}
			result.push("<span class=\"runtime\">时间：<b>"+opuslistComment.createTime+"</b></span></span>");
			result.push("<span class=\"runtime\">"+opuslistComment.content+"</span>");
			result.push("</p>");
			result.push("<div class=\"MoreBottom\" style=\" margin-bottom:0px;\"><a href=\"#cc2\" id=\"cc2\" onClick='javascript:showbox(\"replycom\", " + opuslistComment.id + ");'>[回复]</a></div>");
			result.push("<div id=\""+opuslistComment.id+"\" style=\"display:none;\">");
			result.push("<div class='message_box'>");
			result.push("<textarea id='replaytext" + opuslistComment.id + "' onblur='if(this.value==\"\"){this.value=this.defaultValue;}' onfocus='if(this.value==this.defaultValue){this.value=\"\";}' class='leave_message_txt'>发表评论：</textarea>");
			result.push("</div>");
			result.push("<div class='message_btn_box'>");
			result.push("<input id='replaytext" + opuslistComment.id + "btn' type='button' value='提 交' onclick='postcomment(\"replaytext" + opuslistComment.id + "\", \"replay\", \"" + opuslistComment.id + "\");' class='submit_btn' style='border:1px solid #eee; background: #EC9400; color:#fff; height:20px; overflow:hidden; padding:2px 2px 0px; margin-top:0px;'/>");
			result.push("<input type='button' value='取 消' onclick='$(\"#replaytext\" + " + opuslistComment.id + ").val(\"发表评论：\");cancelbox(" + opuslistComment.id + ");' class='submit_btn' style='border:1px solid #eee; background: #EC9400; color:#fff; height:20px; overflow:hidden; padding:2px 2px 0px; margin-top:0px;'/>");
			result.push("</div>");
			result.push("<div class='vspace'></div>");
			result.push("</div>");
			result.push("<div class='clear_float'></div>");
			
			var childList = opuslistComment.children;
			var sLen = childList.length-1;
			for( var j=0;j<sLen;j++ )
			{
				var chComment =childList[j]; 
				result.push("<div class=\"text_box tb_type_video sj_24\">");
				if( chComment.userid>0 )
				{
					result.push("<p class=\"room\"><span class=\"ttl\"><a title=\"\" href=\""+chComment.commenterSpace+"\">"+chComment.name+"</a>");
				}
				else
				{
					result.push("<p class=\"room\"><span class=\"ttl\">未知流星");
				}
				result.push("<span class=\"runtime\">时间：<b>"+chComment.createTime+"</b></span></span>");
				result.push("<span class=\"runtime\">"+chComment.content+"</span>");
				result.push("</p>");
				result.push("<div class=\"clear_float\"></div>");
				result.push("</div>");
				
			}
			result.push("</div>");
			/*if (i == 0) {
				result.push("<div class='text_box tb_type_video'>");
			} else if (i!=0 && !comform.isreply) {
				result.push("</div>");
				result.push("<div class='text_box tb_type_video'>");
			}
			if (comform.isreply) {
				result.push("<div class='text_box tb_type_video sj_24'>");
			}
			if (comform.commenterid > 0) {
				result.push("<p class='room'><span class='ttl'><a title='" + comform.commentername + "' href='"+ comform.commenterspace + "'>" + comform.commentername + "</a>");
			} else {
				result.push("<p class='room'><span class='ttl'>未知流星" + comform.commentername);
			}
			result.push("&nbsp;<span class='runtime'>时间：<b>" + comform.createtime + "</b></span>&nbsp;<span style=\"float:right;margin-top:-18px;\"><span id=\"importantcomment_"+comform.id+"\"><a href=\"javascript:importantc(1,'"+comform.id+"');\"><img src=\"/image/tj.jpg\"  /></a></span><span style=\"margin-left:12px;\"id=\"unimportantcomment_"+comform.id+"\"><a href=\"javascript:importantc(-1,'"+comform.id+"');\"><img src=\"/image/fd.jpg\"  /></a></span></span>");
			result.push("</span>");
			result.push("<span class='runtime'>" + comform.content + "</span>");
			result.push("</p>");
			result.push("<div class='MoreBottom' style=' margin-bottom:0px;'><a href='javascript:showbox(\"replycom\", " + comform.id + ");'>[回复]</a></div>");
			result.push("<div id='" + comform.id + "' style='display:none;width:500px'>");
			result.push("<div class='message_box'>");
			result.push("<textarea id='replaytext" + comform.id + "' onblur='if(this.value==\"\"){this.value=this.defaultValue;}' onfocus='if(this.value==this.defaultValue){this.value=\"\";}' class='leave_message_txt'>发表评论：</textarea>");
			result.push("</div>");
			result.push("<div class='message_btn_box'>");
			result.push("<input id='replaytext" + comform.id + "btn' type='button' value='提 交' onclick='postcomment(\"replaytext" + comform.id + "\", \"replay\", \"" + comform.parentid + "\");' class='submit_btn' style='border:1px solid #eee; background: #EC9400; color:#fff; height:20px; overflow:hidden; padding:2px 2px 0px; margin-top:0px;'/>");
			result.push("<input type='button' value='取 消' onclick='$(\"#replaytext\" + " + comform.id + ").val(\"发表评论：\");cancelbox(" + comform.id + ");' class='submit_btn' style='border:1px solid #eee; background: #EC9400; color:#fff; height:20px; overflow:hidden; padding:2px 2px 0px; margin-top:0px;'/>");
			result.push("</div>");
			result.push("<div class='vspace'></div>");
			result.push("</div>");
			result.push("<div class='clear_float'></div>");
			if (comform.isreply) { //回复
				result.push("</div>");
			}*/
		}
		$("#commentdiv").html(result.join(''));
		
		//更新翻页
		$("b.current_page").html(p);
		$("span.tpb_right").html(json[jlen].page);
		//$("#compage").find("b.current_page").html(p);
		//$("#compage").html(json[jlen].page);

	});
}


function postcomment(id, type, pid) {
	var content = $("#" + id).val();
	var devalue = $("#" + id).attr("defaultValue");
	if (content == "" || content == devalue) {
		return ;
	}
	$("#" + id + "btn").attr("disabled","true");
	var commenterid = loginuser;
	var parentid;
	if (type == "post") {
		parentid = 0;
	} else {
		parentid = pid;
	}
	var param = {"opuslistid": opuslistid, "commenterid": commenterid, 
				 "parentid": parentid, "content": content};
	t = new Date();
	$.post("/ajaxaction.do?method=postolcomment&t=" + t.getTime(), param, function(xml) {
	     if ($(xml).text() == -1) {
	     	alert("您的评论被拒绝。");
	     } else if ($(xml).text() == 0) {
	     	alert("您的评论提交失败，请重试。");
	     } else {
	     	alert("您的评论已提交，请等待管理员审核。");
	     }
   	});
   	
   	location.reload();
  // window.location.href="/opus/"+opusid+".html";
}


function postykcomment(id, type, pid) {
	var content = $("#" + id).val();
	var devalue = $("#" + id).attr("defaultValue");
	if (content == "" || content == devalue) {
		return ;
	}
	$("#" + id + "btn").attr("disabled","true");
	var commenterid = loginuser;
	var parentid;
	if (type == "post") {
		parentid = 0;
	} else {
		parentid = pid;
	}
	var param = {"opuslistid": opuslistid, "commenterid": commenterid, 
				 "parentid": parentid, "content": content};
	t = new Date();
	$.post("/ajaxaction.do?method=postykolcomment&t=" + t.getTime(), param, function(xml) {
	     if ($(xml).text() == -1) {
	     	alert("您的评论被拒绝。");
	     } else if ($(xml).text() == 0) {
	     	alert("您的评论提交失败，请重试。");
	     } else {
	     	alert("您的评论已提交，请等待管理员审核。");
	     }
   	});
   	
   	location.reload();
}


function showbox(type, id) {
	if (type == "postcom") {
			//$("#posttext2")[0].focus();
			if (id != null && (typeof id != 'undefined') && id != "") {
				$("#"+ id).css("display", "block");				
			}
	} else if (type == "replycom") {
			if (id != null && (typeof id != 'undefined') && id != "") {
				$("#"+ id).css("display", "block");				
			}
	} 
}


function cancelbox(elmid) {
	$("#" + elmid).hide();
}
