//评论
function commentPage(){
		    window.scroll(0,675);
}
/*
function updateCount(oId,num){
	var o = $(oId);
	if(o==undefined || o ==null)return;
	if(isNaN(o.innerHTML)){
		if(num > 0){
			o.innerHTML = num;
		}else{
			o.innerHTML = 0;
		}
	}else{
		o.innerHTML = parseInt(o.innerHTML)+num;
	}
}

*/
function comment(){
	var t_username = '';
	var t_password = '';
	if(!islogin()){
		if($('username').value=='' || $('username').value=='Email或昵称'){
			alert('请填写用户名.')
			$('username').focus();
			return false;
		}else{
			t_username = $('username').value;
		}
		if($('password').value==''){
			alert('请填写密码.')
			$('password').focus();
			return false;
		}else{
			t_password = $('password').value;
		}
	}
	var t_pkid = '';
	if($('pkId')){
		t_pkid = $('pkId').value;
	}
	var t_content = $('content').value;
	if(checkCommentContent(t_content) == false){
		return false;
	}
	
	var t_videoId = $('videoId').value;
	var verify_code="";
	if($('verify_code_value') && $('verify_code_value').value){
		verify_code= $('verify_code_value').value;
	}
	Nova.QVideo.comment({videoId:t_videoId,content:t_content,username:t_username,password:t_password,pkId:t_pkid,verify_code:verify_code},commentCallBack);
	return false;
}
//发表成功返回新插入的QComment_Item对象 -1 发表失败 -2 内容不能为空 -3 用户名/密码错误 -4 内容过多
function commentCallBack(result,callid){
	var item = result;
	if(checkCommentCallBackResult(result,$('content')) == false){
		return ;
	}
	verify_code_src = '';
	showNewComment(item,$('content'));
	
	if(!empty(item.feedpublish) && (item.feedpublish == 2)){
	    RRFeedPublishTip('视频评论');
	}
	//update comment list
	var bt_cmtlist = $('bt_allcmt');
	if(bt_cmtlist){
		var cmtlist_url = $('cmtlist_url').value;
		bt_cmtlist.onclick = function cmtlist(){
			nova_update_fade('videobodycommentlist',cmtlist_url+"time_"+new Date().getTime());
		}
	}
	try{
		Element.hide("div_login"+update_code_tmp);
		Element.hide("verify_code"+update_code_tmp);
		$("verify_code_value"+update_code_tmp).value="";
	}catch(e){}
	window.scrollTo(0,$('videobodycommentlist').offsetTop);
	update_login_status();
}

function genReplyCommentContent(replyOrder){
	var name = $('comment_name_'+replyOrder).innerHTML;
	var content = $('content_'+replyOrder).innerHTML;
	content = content.replace(/<br>/gi,"");
	var newContent = content.split("\n");
	content = newContent.join("<newline>");
	content = content.replace(/<DIV.*\/DIV>/mgi,"");
	content = content.replace(/<newline>/gi,"\n");
	content = content.replace(/<IMG(\salt=(["\u4e00-\u9fa5]*))*\ssrc="([\:a-z0-9\.\/]*)smiley\/(.+?)\.gif"(\salt=(["\u4e00-\u9fa5]*))*>/gi,"[$4]");
	return "[quote]"+name + "写道：\n"+content+"[/quote]";
}


var replyNode = null;
var replyLink  = null;
var hasReply = false;
var replyCommentOrder = "";

function replyComment(order){
	if(hasReply){cancleReply();}
	replyCommentOrder = order;
	showCommentReplay();
}

var update_code_tmp="";

function verify_code(renew,replyCommentOrder){
	if(renew){
			verify_code_src ="";
	}
	if(replyCommentOrder==undefined){
		replyCommentOrder="";
	}
	update_code_tmp=replyCommentOrder;
	Nova.QVideo.isNeedVerify({}, verify_code_callback);
}
var verify_code_src = "";
function verify_code_callback(result,callid){
		if(result){
			   if(verify_code_src==""){
					var timer = new Date();
					var rand = timer.getTime()+"-"+Math.random();
					verify_code_src = '/verify/'+"?"+rand;
					$('verify_code_value'+update_code_tmp).value='';
			   }
			   if($('verify_code_img'+update_code_tmp).src.indexOf(verify_code_src)<0 && verify_code_src !=""){
				$('verify_code_img'+update_code_tmp).src=verify_code_src;
			   }
				Element.show('verify_code'+update_code_tmp);
		}else{
				Element.hide('verify_code'+update_code_tmp);
		}
}
function showCommentReplay(){
	if(replyCommentOrder == ""){
		return ;
	}

	replyNode = document.createElement("DIV");
	tmpHtml = '<form id="commentForm'+replyCommentOrder+'" name="commentForm'+replyCommentOrder+'" method="POST" onsubmit="return commentReply('+replyCommentOrder+');">'
	tmpHtml +=' <div class="faces"> <ul class="ico__faces"> <li title="赞" onclick="grin(\'[Yo17]\','+replyCommentOrder+');"></li> <li title="稀饭" onclick="grin(\'[Qoo7]\','+replyCommentOrder+');"></li> <li title="怒" onclick="grin(\'[Qoo4]\','+replyCommentOrder+');"></li> <li title="吐" onclick="grin(\'[Qoo14]\','+replyCommentOrder+');"></li> <li title="无语" onclick="grin(\'[Qoo5]\','+replyCommentOrder+');"></li> <li title="难过" onclick="grin(\'[Yo2]\','+replyCommentOrder+');"></li> <li title="汗" onclick="grin(\'[Qoo11]\','+replyCommentOrder+');"></li> <li title="搞笑" onclick="grin(\'[Qoo12]\','+replyCommentOrder+');"></li> </ul></div>';
	tmpHtml +='<textarea id="comment_content_'+replyCommentOrder+'" name="comment_content_'+replyCommentOrder+'" onKeyDown="ctlent(event,\'bt_comment_'+replyCommentOrder+'\');" onFocus="verify_code(false,'+replyCommentOrder+')"></textarea>'
	tmpHtml +='<div class="action">'
	+'<div class="submit">'
	+'<button style="width:56px;text-align:center;" type="submit" align="absmiddle" id="bt_comment_'+replyCommentOrder+'" name="bt_comment_'+replyCommentOrder+'">回应</button>'
	+'<button type="button" align="absmiddle" onclick="cancleReply();">取消</button>'
	+'</div>'
	+'</div>'

	if(!islogin()){
		tmpHtml+='<div class="login" id="div_login'+replyCommentOrder+'" style="display:block">'
		+'用户名: <input name="username" id="username'+replyCommentOrder+'" type="text" value=""/>'
		+'密码: <input type="password" name="password" id="password'+replyCommentOrder+'" value="" />'
		+'<a href="http://www.youku.com/user/signup" target="_blank">新用户注册</a>'
		+'</div>'
	}

	tmpHtml+='<div id="verify_code'+replyCommentOrder+'" style="float:left;display:none" class="validate">'
	+'验证码: <input type="text" class="defaultext" onfocus="if(this.value == this.defaultValue){this.value = \'\';this.className = \'\';}" onblur="if(this.value == \'\'){this.value = this.defaultValue;this.className = \'defaultext\';}" onKeyDown="ctlent(event,'+"'bt_comment'"+');" name="verify_code_value'+replyCommentOrder+'" id="verify_code_value'+replyCommentOrder+'" value="不区分大小写" />'
	+'<span><img align="absmiddle" class="key"  id="verify_code_img'+replyCommentOrder+'"/></span> <a href="#" onclick="verify_code(true,'+replyCommentOrder+');return false;">看不清换一张</a></div>'
	+'<div class="clear"></div>'
	+'</div>'
	+'</form>';
	replyNode.innerHTML = tmpHtml;
	replyNode.id = "replyCopy"
	replyNode.className = "commentAction commentReply"

	$("comment" + replyCommentOrder).appendChild(replyNode);
	replyCommentOrder = replyCommentOrder;
	$("reply" + replyCommentOrder).style.display = "none";
	hasReply = true;
	$('comment_content_'+replyCommentOrder).focus();
}
function cancleReply(){
	$("comment" + replyCommentOrder).removeChild($("comment" + replyCommentOrder).lastChild);
	hasReply = false;
	$("reply" + replyCommentOrder).style.display = "block";
}

var currReplyOrder = 0;
//评论
function commentReply(replyOrder){
	currReplyOrder = replyOrder;
	var t_username = '';
	var t_password = '';
	if(!islogin()){
		if($('username'+update_code_tmp).value=='' || $('username'+update_code_tmp).value=='Email或昵称'){
			alert('请填写用户名.')
			$('username'+update_code_tmp).focus();
			return false;
		}else{
			t_username = $('username'+update_code_tmp).value;
		}
		if($('password'+update_code_tmp).value==''){
			alert('请填写密码.')
			$('password'+update_code_tmp).focus();
			return false;
		}else{
			t_password = $('password'+update_code_tmp).value;
		}
	}
	var t_content = $('comment_content_'+replyOrder).value;
	var t_pkid = '';
	if($('pkId')){
		t_pkid = $('pkId').value;
	}
	verify_code_src = "";
	if(checkCommentContent(t_content) == false){
		$("comment_content_"+replyCommentOrder).focus();
		return false;
	}
	t_content = t_content+"\n"+genReplyCommentContent(replyOrder);
	var t_videoId = $('videoId').value;
	var verify_code="";
	if($('verify_code_value'+update_code_tmp) && $('verify_code_value'+update_code_tmp).value){
		verify_code= $('verify_code_value'+update_code_tmp).value;
	}
	Nova.QVideo.comment({videoId:t_videoId,content:t_content,username:t_username,password:t_password,pkId:t_pkid,verify_code:verify_code},commentReplyCallBack);
	return false;
}

function checkCommentContent(content){
	var reg = /\[Yo1\]|\[Yo2\]|\[Yo3\]|\[Qoo4\]|\[Qoo5\]|\[Qoo6\]|\[Qoo7\]|\[Qoo8\]|\[Yo9\]|\[Qoo10\]|\[Qoo11\]|\[Qoo12\]|\[Qoo13\]|\[Qoo14\]|\[Qoo15\]|\[Yo16\]|\[Yo17\]|\[Qoo18\]|\[Zoo1\]/g;    // 创建正则表达式模式。
	var r=new Array();
	r = content.match(reg);   // 尝试匹配搜索字符串。
	if(r!=null){
		if(r.length>5){
			alert('表情个数不能多于5个');
			return false;
		}
	}
	s = content.replace(reg,'');
	if(s.length>1000){
		alert('评论内容不能超过1000字');
		return false;
	}
	if(content.replace(/(^\s*)|(\s*$)/g,"").length<1){
		alert('内容不能为空。');
		return false;
	}
	return true;
}

//发表成功返回新插入的QComment_Item对象 -1 发表失败 -2 内容不能为空 -3 用户名/密码错误 -4 内容过多
function commentReplyCallBack(result,callid){
	var item = result;
	var txtContent = $('comment_content_'+currReplyOrder);

	if(checkCommentCallBackResult(result,txtContent) == false){
		return ;
	}
	showNewComment(item,txtContent);
	cancleReply();
	window.scrollTo(0,$('videobodycommentlist').offsetTop);
	update_login_status();
}

function checkCommentCallBackResult(result,txtContent){
	if(result==null){
		return false;
	}
	if(result==-1){
		alert('评论发表失败。');
		return false;
	}
	if(result==-8){
		alert('内容不能为空。');
		return false;
	}
	if(result==-4){
		alert('您不能输入太多评论内容，请减少内容。');
		return false;
	}
	if(result==-3){
		alert('您的用户名/密码输入有误。');
		return false;
	}
	if(result==-2){
		alert('您已经被限制发表评论，若有疑问，请通过网站客服反馈意见，谢谢。');
		return false;
	}
	if(result==-6){
		alert('您已经发表了该评论，谢谢！');
		return false;
	}
	if(result==-7){
		alert('您的评论含有网站禁止内容，请修改，谢谢！');
		txtContent.value='';
		return false;
	}
	if(result==-9){
		alert('请输入验证码');
		return false;
	}
	if(result==-10){
		alert('验证码输入错误，请重新输入');
		return false;
	}
	return true;
}

function showNewComment(item,txtContent){
	var newCommentArea = $('div_new_comment');
	var managerHTML ='';
	if($('manage')&&$('manage').value=='true'){
		managerHTML = '<a href="javascript:deleteComment('+item.pk_comment_item+','+item.object_id+');">删除</a>';
	}

	if($('comment_none')){
		$('comment_none').style.display = "none";
		$('comment_none_for_new').style.display = "block";
	}
	
	//限制连续提交
	if(currReplyOrder == 0){
		//txtContent.value='发帖累了么，休息3秒钟吧。';
		txtContent.value='';
		//timer = setTimeout("enableCommentForm()", 3000);
	}else{
		$('reply'+currReplyOrder).style.display = "none";
	}


	var newCommentHTML = '<div class="comment" id="comment'+item.pk_comment_item+'">'+
'<div class="bar"><a href="/user/show/id/'+item.user_id+'.html"'+
	    '" title="'+partnerRealName(item.nickname, 'renren')+'" target="_blank" id="comment_name_'+item.pk_comment_item+'" name="comment_name_'+item.pk_comment_item+'">'+partnerRealName(item.nickname, 'renren')+'</a> '+(!empty(item.email) ? item.email : '')+' 发表于0分钟前</div>'+
'<div class="content" style="overflow-x:hidden;">'+
'<span id="content_'+item.pk_comment_item+'" name="content_'+item.pk_comment_item+'">'+item.content+'</span>'+
'<div class="reply" id="reply'+item.pk_comment_item+'"><a href="javascript:replyComment('+item.pk_comment_item+')">回应此评论</a> '+managerHTML+'</div></div></div>';
	newCommentArea.innerHTML = newCommentHTML+newCommentArea.innerHTML;

	updateTotalComment(1,true);
	if(currReplyOrder > 0){
		currReplyOrder = 0;
	}
}
function setCommentFocus(){
	if(hasReply){
		cancleReply();
	}
	var form = $('commentForm');
	if(form){
		for(var i =0;i<form.elements.length;i++){
			if(form.elements[i].type == "textarea" && form.elements[i].name=="content"){
				try{
					form.elements[i].focus();
				}catch(e){}
				return;
			}
		}
	}
}
function ctlent(event,btn_name) {
	if((event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83)) {
		var btn=$(btn_name);
		if(btn){
			btn.click();
		}
	}
}
function updateTotalComment(tc,isAdd){
	if(isAdd) $("span_currcommenttotal1").innerHTML=parseInt($("span_currcommenttotal1").innerHTML)+tc;
	if(isAdd) tc+=parseInt($("totalComment").innerHTML);
	if($("totalComment2")) $("totalComment2").innerHTML=tc;
	$("totalComment").innerHTML=tc;
	$("span_commenttotal1").innerHTML=tc;
	if($("pageCommentBar2") && $("pageCommentBar")) $("pageCommentBar2").innerHTML = $("pageCommentBar").innerHTML;
}
