//评论
function commentPage(){
		    window.scroll(0,675);
}
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'));
	
	//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]";
}
//add by Irving over

//specify whether contents should be auto copied to clipboard (memory)
//Applies only to IE 4+
//0=no, 1=yes
var copytoclip=1;
function copyToClipboard(theField,isalert) {		
	var tempval=$(theField);		
	if (navigator.appVersion.match(/\bMSIE\b/)){
		tempval.select();		
		if (copytoclip==1){
			therange=tempval.createTextRange();
			therange.execCommand("Copy");
			if(isalert!=false)alert("复制成功。现在您可以粘贴（Ctrl+v）到Blog 或BBS中了。");
		}
		return;
	}else{
		alert("您使用的浏览器不支持此复制功能，请使用Ctrl+C或鼠标右键。");
		tempval.select();		
	}
}
//添加元素到收藏夹
var faved = false;
var fav_process = false;
function addFav(id){
	if(faved){
		//alert('dump');
	}
	if(fav_process){
                return;
        }
	Nova.QVideo.addFav({videoId:id}, addFavCallBack);
	fav_process = true;
}

function addFavCallBack(result,callid){
	fav_process = false;
	if(result==1){
		faved = true;
		//alert('添加收藏成功');
		document.getElementById("favo").innerHTML = '<div class="reData bold">收藏成功</div>';
		updateCount('text_fav_times',1);
	}else if(result==-1){
		//if(confirm('对不起，未登录不能进行此操作。您现在登录吗？')){
			openLoginDialog(videoLogin);
		//}
	}else if(result==-2){
		//alert('该视频您已收藏');
		document.getElementById("favo").innerHTML = '<div class="reData bold">已收藏。</div>';
	}else if(result==-3){
		alert('收藏失败');
	}else if(result==-4){
		alert('您不能够收藏自己的视频');
	}
}
function encode(sStr) {
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}
//stat.js
var stat = new Date().getTime()+'50';

//video_show_play.tpl
function onPlayerError(errorMsg){
    Nova.QVideo.playerr({msg:'PlayError',msg2:errorMsg}, errCallback);
}
function errCallback(){
//	alert('播放失败，请刷新网页重试一下');
}
//video_show.tpl
function videoLogin()
{
	var div = $("div_login");
	if(div){
		div.innerHTML="";
	}
}
function preload() {
  Nova.addScript("/QVideo/~ajax/");
//  PlayList.init(); PlayListIndexCallback();
}

var updownType = "";
function updown(type){
	if(Nova.Cookie.get("updown_"+videoId2)){
		alert('您已经顶/踩过');
		showUpDowned(0);
		return;
	}
	
	updownType = type;
	Nova.QVideo.updown({videoId:videoId2,type:type}, updownCallback);
}

function updownCallback(result, callid) {
	var typeString = "顶";
	if(updownType == "down"){
		typeString = "踩";
	}
	if(result==-1){
		alert('您已经顶/踩过');
		showUpDowned(0);
		return;
	}

	if(result==-3){
		alert(typeString+'操作失败。');
		return;
	}

	if(result==-4){
		alert('您不能'+typeString+'自己的视频。');
		showUpDowned(0);
		return;
	}
	if(updownType == "down"){
		showUpDowned(-1);
	}else{
		showUpDowned(1);
	}
}

function showUpDowned(times){
	if(times == 0 || times == -1 || times == 1){
		var object = $('upLower');

		var upTimes = parseInt($('upVideoTimes').innerHTML);
		var downTimes = parseInt($('downVideoTimes').innerHTML);
		if(updownType == "down" && times == -1){
			downTimes = downTimes + 1;
			updateCount("downTimesDetail",1);
		}else if(updownType == "up" && times == 1){
			upTimes = upTimes + 1;
			updateCount("upTimesDetail",1);
		}
		object.innerHTML = '<div class="reData"><span class="bold">已提交。</span><span class="ding">顶</span>:<span class="stat">'+upTimes+'</span> <span class="cai">踩</span>:<span class="stat">'+downTimes+'</span></div>'
		Nova.Cookie.set("updown_"+videoId2,1,1);
		tip('hidden');
	}
}

function initUpDowned(){
	if(Nova.Cookie.get("updown_"+videoId2)){
		showUpDowned(0);
	}else{
		$('updownLinkUp').style.display = "block";
		$('updownLinkDown').style.display = "block";
	}
}


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" name="commentForm" method="POST">'
	tmpHtml +=' <div style="position:absolute;top:-5px;right:218px;width:24px;height:24px;cursor:pointer;background:url(/index/img/smiley/Zoo1_2.gif) no-repeat 0 0;" onclick="grin(\'[Zoo1]\','+replyCommentOrder+');" alt="真印象" title="真印象"></div><ul class="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>';
	tmpHtml +='<textarea style="margin-top:25px" id="comment_content_'+replyCommentOrder+'" name="comment_content_'+replyCommentOrder+'" onKeyDown="ctlent(event,\'bt_comment_'+replyCommentOrder+'\');" onFocus="verify_code(false,'+replyCommentOrder+')"></textarea>'
	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 class="action">'
		tmpHtml+='<div id="verify_code'+replyCommentOrder+'" style="float:left;display:none" class="validate">'
		+'验证码: <input style="width:80px" type="text" onKeyDown="ctlent(event,'+"'bt_comment'"+');" name="verify_code_value'+replyCommentOrder+'" id="verify_code_value'+replyCommentOrder+'" value="" />'
		+'<span><img class="key" align=absmiddle id="verify_code_img'+replyCommentOrder+'"/></span> <a href="#" onclick="verify_code(true,'+replyCommentOrder+');return false;">看不清？</a></div>'
		+'<div class="submit">'
	tmpHtml+='<button align="absmiddle" id="bt_comment_'+replyCommentOrder+'" name="bt_comment_'+replyCommentOrder+'" onclick="javascript:return commentReply('+replyCommentOrder+');">回应</button>'
	+'<button align="absmiddle" onclick="cancleReply()">取消</button>'
	+'</div>'
	+'<div class="clear"></div>'
	+'</div>'
	+'</form>';
	replyNode.innerHTML = tmpHtml;
	replyNode.id = "replyCopy"
	replyNode.className = "commentAction commentReply"

	document.getElementById("comment" + replyCommentOrder).appendChild(replyNode);
	replyCommentOrder = replyCommentOrder;
	document.getElementById("reply" + replyCommentOrder).style.display = "none";
	hasReply = true;
	$('comment_content_'+replyCommentOrder).focus();
}
function cancleReply(){
	document.getElementById("comment" + replyCommentOrder).removeChild(document.getElementById("comment" + replyCommentOrder).lastChild);
	hasReply = false;
	document.getElementById("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){
		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;
	}
	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('请输入验证码');
		try{
		$('content').focus();
		}catch(e){}
		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="'+item.nickname+'" target="_blank" id="comment_name_'+item.pk_comment_item+'" name="comment_name_'+item.pk_comment_item+'">'+item.nickname+'</a> 发表于0分钟前</div>'+
'<div class="content" style="overflow-x:hidden;">'+
'<span class="content" id="content_'+item.pk_comment_item+'" name="content_'+item.pk_comment_item+'">'+item.content+'</span>'+
'<div class="t_r" id="reply'+item.pk_comment_item+'"><a href="javascript:replyComment('+item.pk_comment_item+')">回应此评论</a> '+managerHTML+'</div></div></div>';
	newCommentArea.innerHTML = newCommentHTML+newCommentArea.innerHTML;

	//更新页面记数
	updateCount('span_commenttotal1',1);
	updateCount('span_commenttotal2',1);
	if(parseInt($('span_commenttotal3').innerHTML) == 0){
		$('span_commentstring3').style.display="inline";
	}
	
	updateCount('span_commenttotal3',1);
	updateCount('span_currcommenttotal1',1);
	updateCount('span_currcommenttotal2',1);
	updateCount('span_detailcommenttotal',1);
	if(currReplyOrder > 0){
		currReplyOrder = 0;
	}
}

function updateCount(oId,num){
	var o = $(oId);
	if(o==undefined || o ==null)return;
	if(isNaN(parseInt(o.innerHTML.replace(/\,/,'')))){
		if(num > 0){
			o.innerHTML = num;
		}else{
			o.innerHTML = 0;
		}
	}else{
		//o.innerHTML = String(parseInt(o.innerHTML.replace(/\,/,''))+num).replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g,"$1,");
		o.innerHTML = parseInt(o.innerHTML.replace(/\,/,''))+num;
	}
}

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;
			}
		}
	}
}
var ikuagent;
function iku_protocol_impl(){
	if ((navigator.userAgent.indexOf('MSIE') != -1) && (window.ActiveXObject)){
		try{
			ikuagent = new ActiveXObject("iKuAgent.KuAgent2");
			if (ikuagent)
			{
				var protocolVersion = ikuagent.GetProtocolVersion();
				if (protocolVersion != "0" && protocolVersion != "1.0.0.0") {
				}else{
					$("download").href="http://c.youku.com/iku/";
					$("download").target="_blank";
				}
			}else{
				$("download").href="http://c.youku.com/iku/";
				$("download").target="_blank";
			}
		}catch(e){
			$("download").href="http://c.youku.com/iku/";
			$("download").target="_blank";
		}
	}else{
		//$("download").href="http://c.youku.com/iku/";
		//$("download").target="_blank";
	}

}
window.nova_init_hook_download=iku_protocol_impl;
//{{{player Control

var playerId="movie_player";
function playModeSet(playMode){
//	try{
		return thisPlayer(playerId).playModeSet(playMode);
//	}catch(e){
//		return false;
//	}
}
function thisPlayer(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}

//}}}
