
if (!$) var $=function(_id) { return document.getElementById(_id); }
var _uimg = "http://home.pipi.cn/uc/images/noavatar_small.gif";

var _userName=null;
function getCookie (name){ //获取Cookie
     var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));  
     if(arr !=null) 
     	return decodeURIComponent(arr[2]); 
     return null;     
}

function setCookie(name,value){ 
	var date=new Date();
	date.setTime(date.getTime()+1000*60*60*24);
	document.cookie = name + "="+ value + ";expire=" +date.toGMTString();	
}

window.onload=function() {
}


function iniC(){
	setParentHeight("iframecomment");
}

function initUser(){
		var _userName=$("nickname");
		if (_userName){
			if (getCookie("username") == null){
				_userName.value="匿名游客";
				$("_iflogin").innerHTML='已注册会员，请 <a href="http://home.pipi.cn/do.php?ac=login&refer='+encodeURIComponent(top.location.href)+'">登录</a> 后发表影评。';
			}else{
				_userName.value=getCookie("username");
				_userName.readOnly=true;
				$("_iflogin").innerHTML="";
			}	
		}		
}


function sendComment(_button) {
	var flag = 2;
	var _userName=$("userName_"+flag);
	if (_userName.value.trim()=="") {
		alert("昵称不能为空！");
		return;
	}
	if ($("comment_title").value.trim() == "") {
		alert("标题不能为空！");
		return;
	}
	if ($("comment_title").value.trim().length > 35) {
		alert("发布标题不能超过35字！");
		return;
	}
	if ($("comment_txt_"+flag).value.trim()=="") {
		alert("不能发布空内容！");
		return;
	}
	if ($("comment_txt_"+flag).value.trim().length > 20000) {
		alert("发布内容不能超过2000字！");
		return;
	}
	if ($("vCode_"+flag).value.length!=4) {
		alert("验证码位数有误！");
		return;
	}
	document.getElementById("frmComment").action="http://user.pipi.cn/action/revCommit.jsp?flag=1";
	document.getElementById("frmComment").target="hid";
	$("frmComment").submit();
}

function revComment(){
	var _userName=$("nickname");
	if (_userName.value.trim()=="") {
		alert("用户名不能为空！");
		return;
	}
	
	if ($("msgcontent").value.trim()=="") {
		alert("不能发布空内容！");
		return;
	}
	if ($("vCode_2").value.length!=4) {
		alert("验证码位数有误！");
		return;
	}
	//_button.onclick=function(){};
	document.getElementById("frmcomment").action="http://user.pipi.cn/action/commentCommit.jsp?flag=2";
	document.getElementById("frmcomment").target="hid";
	$("frmcomment").submit();
}


function showCImg(fg) {
	var flag="2";
	if (fg){flag=fg}
	var _html='<img src="http://user.pipi.cn/action/verify.jsp" id="vi_'+flag+'" style="display:inline;margin:0 0 -4px 5px" align="bottom"/>';
	_html+='&nbsp;&nbsp;<a style="cursor:pointer" onclick="refreshCImg('+fg+')">看不清，换一个</a>';
	$("vimg_"+flag).innerHTML=_html;
	$("vCode_"+flag).onfocus=function(){};
}

function refreshCImg(fg) {
	var flag="2";
	if (fg){flag=fg}
	var _img=$("vi_"+flag);
	_img.src="http://user.pipi.cn/action/verify.jsp?t="+new Date().getTime();
}


function showHelloDiv(){
	var usernickname = getCookie("usernickname");
	if (usernickname != null){
		$("_helloUser").innerHTML="您好，<a href='#'>"+usernickname+"</a>！";
	}else{
		$("_helloUser").innerHTML="您好，游客！";
	}
}



function delcom(_revid){
	if (confirm("确认删除？")){
		if ($("_commentdiv_"+_revid))$("_commentdiv_"+_revid).style.display="none";
		$("_comactionfrm").action="/action/commentCommit.jsp?flag=6&revId="+_revid;
		$("_comactionfrm").submit();
	}
	return false;
	
}



function upcom(_obj,_revid){
	_obj.onclick=function(){
		if(getCookie("uid"))alert("对不起，您已经评价过该影评!");
		else alert("请您先登录");
		return false;
	};
	//3,顶影评；
	var flag = "3";
	var userid = getCookie("uid");
	if (userid){
		if (getCookie("commOrder_"+userid+"_"+_revid+"_"+flag) == "1" || getCookie("commOrder_"+userid+"_"+_revid+"_4") == "1"){
			alert("对不起，您已经评价过该影评!");
			return false;
		}else{
			setCookie("commOrder_"+userid+"_"+_revid+"_"+flag,"1");
			var _ocount = _obj.innerHTML.substr(_obj.innerHTML.indexOf('(')+1,_obj.innerHTML.indexOf(')')-2);
			_obj.innerHTML = "顶("+ (parseInt(_ocount)+1)+")";
			if ($("_comactionfrm")){
				$("_comactionfrm").action='/action/commentCommit.jsp?flag=3&revId='+_revid;
				$("_comactionfrm").submit();
			}else{
				var _temp=document.createElement("form");			
				document.body.appendChild(_temp);
				with (_temp) {
					method="post";
					target="xx";	
					action="http://user.pipi.cn/action/commentCommit.jsp?flag=3&revId="	+_revid;	
				}
				_temp.submit();
				//_obj.target="xx";
				//_obj.href="http://user.pipi.cn/action/commentCommit.jsp?flag=3&revId="+_revid;
			}
		}
	}else{
		alert("请您先登录");
	}
	
}

function downcom(_obj,_revid){
	_obj.onclick=function(){
			if(getCookie("uid"))alert("对不起，您已经评价过该影评!");
			else alert("请您先登录");
			return false;
		};
	//4,踩影评
	var flag = "4";
	var userid = getCookie("uid");
	if (userid){
		if (getCookie("commOrder_"+userid+"_"+_revid+"_"+flag)  == "1" || getCookie("commOrder_"+userid+"_"+_revid+"_3")  == "1"){
			alert("对不起，您已经评价过该影评!");
			return false;
		}else{
			setCookie("commOrder_"+userid+"_"+_revid+"_"+flag,"1");
			var _ocount = _obj.innerHTML.substr(_obj.innerHTML.indexOf('(')+1,_obj.innerHTML.indexOf(')')-1);
			_obj.innerHTML = "踩("+ (parseInt(_ocount)+1)+")";
			if ($("_comactionfrm")){
				$("_comactionfrm").action='/action/commentCommit.jsp?flag=4&revId='+_revid;
				$("_comactionfrm").submit();
			}else{
				var _temp=document.createElement("form");			
				document.body.appendChild(_temp);
				with (_temp) {
					method="post";
					target="xx";	
					action="http://user.pipi.cn/action/commentCommit.jsp?flag=3&revId="	+_revid;	
				}
				_temp.submit();
				//_obj.target="xx";
				//_obj.href="http://user.pipi.cn/action/commentCommit.jsp?flag=4&revId="+_revid;
			}
		}
	}else{
		alert("请您先登录");
	}
}

function collectcom(_obj,_revid){
	_obj = $("_colcount");
	var flag = "7";
	var userid = getCookie("uid");
	if (userid){
		if (getCookie("commCollect_"+userid+"_"+_revid+"_"+flag) == "1"){
			alert("您已经收藏!");
			return false;
		}else{
			var _ocount = _obj.innerHTML.substr(_obj.innerHTML.indexOf('(')+1,_obj.innerHTML.indexOf('人')-1);
			_obj.innerHTML = "<span>("+(parseInt(_ocount)+1)+"人已收藏)</span>";
			$("_comactionfrm").action='/action/commentCommit.jsp?flag=7&revId='+_revid;
			$("_comactionfrm").submit();
		}
	}
}

function showEdit(_uid,_revId,_colcount,_movId){
	var cokuid = getCookie("uid");
	if ( cokuid != null){
		if (cokuid ==_uid){
			if ($("_opercomDiv"))$("_opercomDiv").innerHTML="<a href='http://www.pipi.cn/review/"+Math.floor(_movId/1000)+"/"+_movId+".html?did=6&reviewid="+_revId+"' target='_parent'>编辑</a>&nbsp;|&nbsp;<a href='javascript:void(0)' onclick='delcom("+_revId+")'>删除</a> | <span>("+_colcount+"人已收藏)</span>";
		}else{
			if ($("_opercomDiv"))$("_opercomDiv").innerHTML="<a href='javascript:void(0)' onclick='collectcom(this,"+_revId+")' class='addfav'>加入收藏</a><span id='_colcount'>("+_colcount+"人已收藏)</span>";
		}
	}else{
		if ($("_opercomDiv"))$("_opercomDiv").innerHTML="<span>("+_colcount+"人已收藏)</span>";
	}
}

function showUpLink(_uid,_revId,_commInfo){
	var cokuid = getCookie("uid");
	var _upct = 0;
	var _downct = 0;
	if (_commInfo && _commInfo.length>0){
		_upct = _commInfo[0].up_count;
		_downct = _commInfo[0].down_count;
	}
	if ( cokuid != null){
		if (cokuid ==_uid){
			if ($("_uphold"))$("_uphold").innerHTML="顶(" + _upct +")  踩(" + _downct +")";
		}else{
			if ($("_uphold"))$("_uphold").innerHTML='<a href="javascript:void(0)" onclick="upcom(this,'+_revId+')" class="up">顶('+_upct+')</a> <a href="javascript:void(0)" onclick="downcom(this,'+_revId+')" class="down">  踩('+_downct+')</a>';
		}
	}else{
		//if ($("_uphold"))$("_uphold").innerHTML="顶("+_upct+")  踩("+_downct+")";
		if ($("_uphold"))$("_uphold").innerHTML='<a href="javascript:void(0)" onclick="upcom(this,'+_revId+')" class="up">顶('+_upct+')</a> <a href="javascript:void(0)" onclick="downcom(this,'+_revId+')" class="down">  踩('+_downct+')</a>';
	}
}


function changeTab(_tab){
	$(_tab+"_li").className="active";
	$(_tab+"_content").style.display="";
	_tab = _tab=="hot"?"new":"hot";
	
	$(_tab+"_li").className="";
	$(_tab+"_content").style.display="none";
	
}


function showDate(_date) {
	document.write(new Date(_date).format("yyyy年MM月dd日 hh:mm:ss"));
}

/////////////////////////////////////////////////////////////////////

function getXMLHttpRequest(){
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
				request = false;
				} 
			}
	}
	return request;
}

String.prototype.trim = function() { 
	return this.replace(/(\s*$)/g, ""); 
} 

Date.prototype.format = function(format)
{
	var o = {
	"M+" : this.getMonth()+1, //month
	"d+" : this.getDate(),    //day
	"h+" : this.getHours(),   //hour
	"m+" : this.getMinutes(), //minute
	"s+" : this.getSeconds(), //second
	"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
	"S" : this.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
	(this.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)if(new RegExp("("+ k +")").test(format))
	format = format.replace(RegExp.$1,
	RegExp.$1.length==1 ? o[k] :
	("00"+ o[k]).substr((""+ o[k]).length));
	return format;
}

document.write("<iframe name='hid' id='hid' width='1' height='1'></iframe>");


var comment={};
comment.login={	
	_revUrl:"",
	showmsg:function(){
		/*var _msghtml="";
		_msghtml +='<li class="close"><a href="#" onclick="review.login.msgclose(1)"><strong>×</strong></a></li>';
		_msghtml +='<li class="frm-content">评论成功！您的评论将在审核通过后显示。</li>';
		_msghtml +='<li class="btn-ok"><input type="button" value=" 确定 " class="btn" onclick="comment.login.msgclose()"/></li>';
				
		if (document.getElementById("_msgid")==null){
			var _ul=document.createElement("ul");
			document.body.appendChild(_ul);		
			_ul.style.cssText="display:none";
			_ul.className="cmtok frm-box";
			_ul.id="_msgid";
			_ul.innerHTML= _msghtml;
		    var _bodyWidth=document.body.clientWidth;
			_ul.style.cssText="display:none;position:absolute;z-Index:9999;top:150px;left:"+(_bodyWidth-300)/2+"px;width:300px;border:1px solid #CCC;";
			
		}
		document.getElementById("_msgid").style.display="block";	
		*/
		alert("评论成功！您的评论将在审核通过后显示。");
		comment.login.msgclose();
	},
	msgclose:function(f){
		var _msgid = document.getElementById("_msgid");
		if (_msgid != null){
			_msgid.style.display="none";
		}
		if (f !=null) return;
		if (comment.login._revUrl){
			if (comment.login._revUrl == top.window.location.href)
			window.location.reload();
			else
			top.window.location.href=comment.login._revUrl;
		}
	}
};

comment.edit={	
	_revId:"",
	_movId:"",
	showInfo:function(){
		//document.write("<script type='text/javascript' src='http://user.pipi.cn/common/comments/js/"+Math.floor(comment.edit._revId/1000)+"/"+comment.edit._revId+".js'></script>");
		var _title = "";
		var _content = "";
		var _uid = "";
		var _logid = "";
		var _unickname="";
		_logid=getCookie("uid")==null?"":getCookie("uid");
		if (_commInfo && _commInfo.length>0){
			_title = _commInfo[0].title;
			_content = _commInfo[0].rev_content;
			_uid = _commInfo[0].u_id;
			_unickname = _commInfo[0].u_nickname;
		}
		if (!_logid || _logid != _uid){
			alert("请先登录");
			return;
		}
		
		var reg = new RegExp("<br/>","g");
		_content = _content.replace(reg,"\r\n");
		var _msghtml=""+
			"  <form id='frmEditCom' action='' method='post'>"+
			"  	 <input type='hidden' name='movId' value='"+comment.edit._movId+"'/>"+
			"  	 <input type='hidden' name='revId' id='revId' value='"+comment.edit._revId+"'/>"+
			"    <h2 class='title'>更新影评：</h2>"+
			"    <ul>"+
			"		<li id='_helloUser'><script type='text/javascript'>showHelloDiv();</script></li>"+
			"	    <li><label for='nickname'>昵称：</label>      "+
			"	      <input type='text' name='userName' id='_userName' readOnly=true class='ipt_text' value='"+_unickname+"'/>&nbsp;&nbsp;&nbsp;&nbsp;"+
			"	      <span></span>"+
			"	    </li>"+
			"	    <li><label for='comment_title'>标题：</label>"+
			"	      <input name='comment_title' type='text' id='comment_title' class='ipt_text msgtitle' value='"+_title+"'/>"+
			"	    </li>"+
			"	    <li>"+
			"	      <textarea name='comment_txt' id='comment_txt' rows='20' cols='50' class='ipt_text msgcontent' >"+_content+"</textarea>"+
			"	    </li>"+
			"	    <li><label for='vcode'>验证码</label>&nbsp;"+
			"			      <input type='text'  id='vCode_3' name='vCode' onfocus='showCImg(3)' class='ipt_text' size='6' />&nbsp;&nbsp;<span id='vimg_3'></span>&nbsp;&nbsp;&nbsp;&nbsp;"+
			"			      "+
			"			      <label>"+
			"			      <input type='button' name='button' id='button' onclick='comment.edit.sendInfo()' value='修改影评' />"+
			"			      </label>"+
			"			    &nbsp;</li>"+
			"      <li></li>"+
			"    </ul>"+
			"    </form>";			
			parent.document.getElementById("_editrevDiv").innerHTML = _msghtml;
			
	},
	sendInfo:function(_button){
		var _frm = $("frmEditCom");
		var _userName=_frm._userName;
		var _title=_frm.comment_title;
		var _content=_frm.comment_txt;
		var _revId = _frm.revId;
		if (_userName.value.trim()=="") {
			alert("昵称不能为空！");
			return;
		}
		if (_title.value.trim() == "") {
			alert("标题不能为空！");
			return;
		}
		if (_title.value.trim().length > 35) {
			alert("发布标题不能超过35字！");
			return;
		}
		if (_content.value.trim()=="") {
			alert("不能发布空内容！");
			return;
		}
		if (_content.value.trim().length > 20000) {
			alert("发布内容不能超过2000字！");
			return;
		}
		if ($("vCode_3").value.length!=4) {
			alert("验证码位数有误！");
			return;
		}
		//_button.onclick=function(){};
		if (_revId && _revId.value !=0){
			_frm.action="http://user.pipi.cn/action/revCommit.jsp?flag=5";		
			_frm.target="hid";
			_frm.submit();
		}
	}
};

comment.first={	
	_comIn:null,
	showActionDiv:function(_revid,_uid,_upcount,_downcount,_flag){
		if (comment.first._comIn){
			_upcount=comment.first._comIn[0].up_count;
			_downcount=comment.first._comIn[0].down_count;
		}
		
		var cokuid = getCookie("uid");
		if ( cokuid != null){
			if (cokuid ==_uid){
				if($("_"+_flag+"revaction_"+_revid))$("_"+_flag+"revaction_"+_revid).innerHTML="顶("+_upcount+")&nbsp;&nbsp;踩("+_downcount+")";
			}else{
				if($("_"+_flag+"revaction_"+_revid))$("_"+_flag+"revaction_"+_revid).innerHTML="<a href='javascript:void(0)' onclick='comment.first.upcom(this,"+_revid+")'  class='up'>顶("+_upcount+")</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='comment.first.downcom(this,"+_revid+")' class='down'>踩("+_downcount+")</a>";
			}
		}else{
			//if($("_"+_flag+"revaction_"+_revid))$("_"+_flag+"revaction_"+_revid).innerHTML="顶("+_upcount+")&nbsp;&nbsp;踩("+_downcount+")";
			if($("_"+_flag+"revaction_"+_revid))$("_"+_flag+"revaction_"+_revid).innerHTML="<a href='javascript:void(0)' onclick='comment.first.upcom(this,"+_revid+")'  class='up'>顶("+_upcount+")</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='comment.first.downcom(this,"+_revid+")' class='down'>踩("+_downcount+")</a>";
		}
	},
	showEditDiv:function(_uid,_revId,_flag,_eurl){
		if (getCookie("uid") != null && getCookie("uid")==_uid)
			if ($("_"+_flag+"editDiv_"+_revId))$("_"+_flag+"editDiv_"+_revId).innerHTML="<a href='"+_eurl+"' target='_blank'>编辑</a>&nbsp;|&nbsp;<a href='javascript:void(0)' onclick='comment.first.delcomments("+_revId+")'>删除</a>";		
		else{
			if ($("_"+_flag+"editDiv_"+_revId))$("_"+_flag+"editDiv_"+_revId).innerHTML="";
		}	
	},
	delcomments:function(_revid){
		if (confirm("确认删除？")){
			$("_commentdiv_"+_revid).style.display="none";			
			$("_comactionfrm").action="/action/commentCommit.jsp?flag=6&revId="+_revid;
			$("_comactionfrm").submit();
		}
		return false;
	},
	upcom:function(_obj,_revid){
		var userid = getCookie("uid");
		_obj.onclick=function(){
			if (userid)alert("对不起，您已经评价过该影评!");
			else alert("请您先登录");
			return false;
		};
		//3,顶影评；
		var flag = "3";		
		if (userid){
			if (getCookie("commOrder_"+userid+"_"+_revid+"_"+flag) == "1" ||getCookie("commOrder_"+userid+"_"+_revid+"_4") == "1"){
				alert("对不起，您已经评价过该影评!");
				return false;
			}else{
				var _ocount = _obj.innerHTML.substr(_obj.innerHTML.indexOf('(')+1,_obj.innerHTML.indexOf(')')-2);
				_obj.innerHTML = "顶("+ (parseInt(_ocount)+1)+")";
				
				$("_comactionfrm").action='/action/commentCommit.jsp?flag=3&revId='+_revid;
				$("_comactionfrm").submit();
			}
		}else{
			alert("请您先登录");
		}		
	},
	downcom:function(_obj,_revid){
		var userid = getCookie("uid");
		_obj.onclick=function(){
			if (userid)alert("对不起，您已经评价过该影评!");
			else alert("请您先登录");
			return false;
		};
		//4,踩影评
		var flag = "4";		
		if (userid){
			if (getCookie("commOrder_"+userid+"_"+_revid+"_"+flag) == "1" || getCookie("commOrder_"+userid+"_"+_revid+"_3") == "1"){
				alert("对不起，您已经评价过该影评!");
				return false;
			}else{
				var _ocount = _obj.innerHTML.substr(_obj.innerHTML.indexOf('(')+1,_obj.innerHTML.indexOf(')')-1);
				_obj.innerHTML = "踩("+ (parseInt(_ocount)+1)+")";
				$("_comactionfrm").action='/action/commentCommit.jsp?flag=4&revId='+_revid;
				$("_comactionfrm").submit();
			}
		}else{
			alert("请您先登录");
		}
	}
}

function doothers(){
}



