﻿
var moodflag = "";        //控制该新闻是否显示表情列表   '0'-不显示  '1'-显示
var docid = "";   //文章ID
var docname = "";      //文章标题
var chnnid = "";							//频道ID
var docurl = FirstDocUrl;       //文章URL地址
var username;

var p;
//每页显示的大小
var pagesize;
var count;
var votetime=0;

function viewAllComment()
{
	var commentURL = 'http://comment.ifeng.com/view.php?docName=' + encodeURI(docname) + '&docUrl='+encodeURI(docurl);
	window.open(commentURL);
}

function CheckInfo(){
	var cookieEnabled=(navigator.cookieEnabled)?true:false;
	if(!cookieEnabled){
		alert("请开启浏览器COOKIE功能！");
		return false;
	}
	if (document.getElementById("username").value == "" || document.getElementById("username").value == null){
		alert("请输入用户名");
		document.getElementById("username").focus();
		return false;
	}
	if (document.getElementById("password").value == "" || document.getElementById("password").value == null){
		alert("请输入密码");
		document.getElementById("password").focus();
		return false;
	}
	document.getElementById("form1").submit();
	document.getElementById("form1").reset();
	return false;
}


//获取时间戳
function timestamp(){
    var timestamp = Date.parse(new Date())/1000;
    return timestamp;
}

function getElementsByClassName(parentEl,className,tagName)
{
	if(!parentEl||!className){
		return null;
	}
	
	var els=cds=[];
	cds = $(parentEl).getElementsByTagName(tagName);
	className=className.toUpperCase();// 将className全部大写
	for(var i=0;i<cds.length;i++){
		if(cds[i].className.toUpperCase() ==className)
		{
			els[els.length]=cds[i];
		}
	}
	return els;
}	
//支持反对操作
function Add_up_down(id)
{
	id = id.split('_')[0] + id.split('_')[1];;
	if($(id))
	{
		$(id).innerHTML = parseInt($(id).innerHTML) + 1;
	}
}

//注册支持反对操作事件
function BindUpAndDown(){
	var votesObj = getElementsByClassName("CommentContentId","vote_img_button","img");
	for(i=0;i<votesObj.length;i++)
	{
		var el = votesObj[i];
		var cmtId=el.id;
		BX.Event.addListener(el.id,'click',function(e,obj){
			if(votetime==0 || (timestamp()-votetime)>10){
				var id=obj.id.split('_')[1];
				var job = obj.id.split('_')[0];
				getScript_cds("http://comment.ifeng.com/vote.php?cmtId="+id+"&docUrl="+docurl+"&job="+job+ "&format=js&callback=Add_up_down&" + votetime);
				votetime=timestamp();
			}else{
				alert("很抱歉，你发表的频率太快，请稍后再试。")
			}
		});
	}
	//todo
	if(!$("floatBoxBg"))
	{
		var  temp_float="<div id=\"floatBoxBg\" style=\"z-index:998;height:"+(document.body.scrollHeight+100)+"px;filter:alpha(opacity=50);\"></div>";
		var maskdiv = document.createElement("div"); 
		maskdiv.innerHTML= temp_float;
		document.documentElement.lastChild.appendChild(maskdiv.childNodes[0]);
	}
	$("floatBoxBg").style.display="none";	
	
	BindReplayCmt();
}
//绑定回复
function BindReplayCmt(){
	var replyObjs = getElementsByClassName("CommentContentId","reply_a_button","a");
	for(i=0;i<replyObjs.length;i++)
	{
		var el = replyObjs[i];
		BX.Event.addListener(el.id,'click',function(e,obj){
			$("comment_form_float").quoteId.value = obj.id.split('_')[1];
			ShowMask();
		});
	}
}
//页面遮罩功能
var timer; 
var Opacity = 0 ;
var maxOpacity;
var stepOpacity;
if(IsIE)
{
	maxOpacity = 0.5;
	stepOpacity = 0.3;	
}
else
{
	maxOpacity = 50;
	stepOpacity = 30;	
}	
function ShowMask()
{
	el = $("floatBoxBg");
	el.style.height = (document.body.scrollHeight+100);
	el.style.display="block";
	Opacity = 0 ;
	timer = window.setInterval("IntervalShow()", 5) 
}
function IntervalShow() 
{ 
	el = $("floatBoxBg");
	if (Opacity == maxOpacity) 
	{ 
		window.clearInterval(timer) ;
		ShowReplyDiv();
		return ;
	} 
	Opacity = ( Opacity + stepOpacity ) > maxOpacity ?maxOpacity : Opacity + stepOpacity ;
	//el.style.opacity = 0.5;
	el.style.filter = "Alpha(Opacity="  + Opacity + ")"; 
}
function IntervalHide()
{
	el = $("floatBoxBg");
	if (Opacity == 0) 
	{ 
		window.clearInterval(timer); 
		el.style.display = "none"; 
		return ;
	} 
	Opacity = (Opacity - maxOpacity < 0) ? 0 : Opacity - maxOpacity ;
	el.style.filter = "Alpha(Opacity="  + Opacity + ")" ;
}
function HideMask()
{
	timer = window.setInterval('IntervalHide()', 5); 
}
function ShowReplyDiv()
{
	el = $("Revert");
	el.style.display="block";
	el.style.top = (document.documentElement.scrollTop + (document.documentElement.clientHeight-parseInt(el.offsetHeight))/2 ) + "px";
	el.style.left = (document.documentElement.scrollLeft + (document.documentElement.clientWidth - parseInt(el.offsetWidth))/2)  + "px";
}
function HideReplyDiv()
{
	HideMask();
	el = $("Revert");
	el.style.display='none'; 
}

//提交评论
function checkForm(id){
	var content="";
	var docUrl="";
	var quoteId="";
	var obj = $(id);
	content=obj.content.value;
	docUrl=FirstDocUrl;
	if(obj.quoteId) quoteId=obj.quoteId.value;
	
	if(content==""){
		alert("请输入评论内容");
		return false;
	}
	if(docUrl==""){
		alert("出错了，没有url参数");
		return false;
	}
	if(docUrl.indexOf("http")==-1){
		alert("出错了，url参数没有以http开头");
		return false;
	}
	//var posturl = "http://comment.ifeng.com/post.php?docUrl="+encodeURI(docurl)+"&docName="+encodeURI(docname)+ "&format=js&content="+encodeURI(content);
	var posturl = "http://comment.ifeng.com/post.php?format=js&callback=PostCmtComplete&docUrl="+docurl+"&docName="+encodeURI(docname)+ "&format=js&content="+encodeURI(content);
	if(quoteId!="")
		posturl+="&quoteId=" + quoteId;
	getScript_cds(posturl);
	obj.reset();
}
function PostCmtComplete(data__)
{
	if(typeof(data__)!='object')
	{
		alert(data__);
	}
	else
	{
		if(currentSmallImgIndex==0)
		{
			retriveComment();
			window.location.hash="ST";
		}
		else//弹出评论列表页
		{
			viewAllComment();
		}
	}
}

  function getCookie_comment(name){
	var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr!=null) return arr[2];
	return null;
}
function changeBox(){
	if(sid&&sid.length>32){
		document.getElementById("content").className="textLogin";
		if(document.getElementById("content").innerHTML=="文明上网，登录评论！"){
			document.getElementById("content").innerHTML="";
		}
	}else{
		document.getElementById("content").className="tipLogin";
		document.getElementById("content").innerHTML="请您先登录，再发表评论。";
		document.getElementById("username").focus();
	}
}
function cb_userdetail()
{
var msg = '<font color="#000000"><b>'+ sso_username +'</b> 凤凰网欢迎您</font>&nbsp;&nbsp;&nbsp;<a href="http://uc.ifeng.com/up/common!logout.sso?backurl='+escape(window.location.href)+'" id="btnlogout" class="ablue">退出</a>'; 
$("Login").innerHTML=msg;
}

function cb_login()
{
var msg2 = ' 用户名<input class="text" id="username" name="u.name" />'+
          '密码<input class="text" id="password" type="password" name="u.pwd" />'+
		  '<input class="button" type="button" value="登录" name="Submit" onclick="javascript:CheckInfo();" />'+
          '<a href="http://uc.ifeng.com/up/registerStep1.html" target="_blank">注册</a>';
$("Login").innerHTML=msg2;
}  
$('backurl').value = window.location.href;	
var sid = getCookie_comment("sid");
var sso_username = '';
if( sid && sid.length > 32)
{	
	sso_username = decodeURIComponent(sid.substr(32));
	cb_userdetail();
}	

//获取评论数据后的回调处理函数
function show(commentJsonVarStr___,divContainer){
	data=commentJsonVarStr___;
	count = data.count;

	var commentTpl = $("txt_comment_template").value;
	var builderTpl = $("txt_building_template").value;
	var loclcommentTpl = $("txt_local_comment_template").value; 

	var commentCode="";
	$(divContainer).innerHTML = "";
	//本地提交的评论在第一页才显示
	if(data.nopass && p==1)
	{
		for(var i=0;i<data.nopass.length;i++)
		{
			var code ="";
			var commentTpl_tmp;
			var builderTpl_tmp="";
			var item = data.nopass[i];
			commentTpl_tmp = RenderTemplate(loclcommentTpl,item);
			
			//显示盖楼的楼层
			var parent = item.parent;
			if(parent && parent.length>0)
			{
				var builderCode="";
				builderTpl_tmp = builderTpl;
				for(var ii=0;ii<parent.length;ii++)
				{
					builderCode = RenderTemplate(builderTpl,parent[ii]);
					if(ii==0)
						builderTpl_tmp = builderCode;
					else	
						builderTpl_tmp = builderTpl_tmp.replace(/{BuilderItem}/g,builderCode);
					builderTpl_tmp = builderTpl_tmp.replace(/{lou_num}/g,(parent.length - ii));	
				}
				builderTpl_tmp = builderTpl_tmp.replace(/{BuilderItem}/g,"");
			}
			code = commentTpl_tmp.replace(/{builder}/g,builderTpl_tmp);
			commentCode=code+"\n";	
			$(divContainer).innerHTML +=commentCode;
		}
	}
	
	if(data.comments)
	{
		for(var i=0;i<data.comments.length;i++)
		{
			var code ="";
			var commentTpl_tmp;
			var builderTpl_tmp="";
			var item = data.comments[i];
			commentTpl_tmp = RenderTemplate(commentTpl,item);
			
			//显示盖楼的楼层
			var parent = item.parent;
			if(parent && parent.length>0)
			{
				var builderCode="";
				builderTpl_tmp = builderTpl;
				for(var ii=0;ii<parent.length;ii++)
				{
					builderCode = RenderTemplate(builderTpl,parent[ii]);
					if(ii==0)
						builderTpl_tmp = builderCode;
					else	
						builderTpl_tmp = builderTpl_tmp.replace(/{BuilderItem}/g,builderCode);
						
					builderTpl_tmp = builderTpl_tmp.replace(/{lou_num}/g,(parent.length - ii));
				}
				builderTpl_tmp = builderTpl_tmp.replace(/{BuilderItem}/g,"");
			}
			code = commentTpl_tmp.replace(/{builder}/g,builderTpl_tmp);
			commentCode=code+"\n";	
			$(divContainer).innerHTML +=commentCode;
		}	
	}
	$("load").style.display = "none";
	if(count)
		$("ViewCount").innerHTML = count;
}


function RenderTemplate(tpl,item)
{
	var code=tpl;
	var id,comment_contents,uname,comment_date,client_ip,ip_from,uptimes,downtimes;
	id 				 = item.id;
	comment_contents = item.comment_contents;
	uname            = item.uname;
	comment_date     = item.comment_date;
	client_ip        = item.client_ip;
	ip_from          = item.ip_from;
	uptimes          = item.uptimes;
	downtimes        = item.downtimes;

	var content = comment_contents + " ";

	code = code.replace(/{id}/g,id);
	code = code.replace(/{comment_contents}/g,content);
	code = code.replace(/{uname}/g,uname);
	code = code.replace(/{comment_date}/g,comment_date);
	code = code.replace(/{client_ip}/g,client_ip);
	code = code.replace(/{ip_from}/g,ip_from);
	code = code.replace(/{uptimes}/g,uptimes);
	code = code.replace(/{downtimes}/g,downtimes);
	
	if(uname=="凤凰网友")
		usericon = '<img src="http://img.ifeng.com/tres/appres/newcomment/images/GreyMen.gif"   title="未登录用户"  width="28" height="28" />';
	else
		usericon = '<img src="http://img.ifeng.com/tres/appres/newcomment/images/GreyMenr.gif"   title="已登录用户"  width="28" height="28" />';
	code = code.replace(/{UserIcon}/g,usericon);
	
	return code;	
}
function ifengcmtcallback(allcount)
{
	$('commentAllCount').innerHTML = allcount;
	$('commentAllCount_1').innerHTML = allcount;
	
}
function retriveComment()
{
	//获得评论总数，包含未通过审核的
	getScript_cds("http://comment.ifeng.com/allcount_" + docurl);
	if(currentSmallImgIndex==0)
	{
		$("commentAbstractId").style.display = "block";
		$("newCommentContent").style.display = "block";
		$("load").style.display = "block";	
		divContaner = "newCommentContent";
		getScript_cds("http://comment.ifeng.com/get.php?pagesize="+pagesize+"&p="+p+"&job=1&callback=show&var=" +divContaner +"&format=js&docurl="+docurl+ "&timespan=" + timestamp(),BindUpAndDown);

	}
	else
	{
		$("commentAbstractId").style.display = "none";
		$("load").style.display = "none";
		$("newCommentContent").style.display = "none";
	}
}
window.onload = function()
{	
	pagesize = 5;
	p=1;	
	docname = $("DocTitleId").innerHTML;
	$("load").style.display = "none";
	$("postcomForm").docName.value = $("comment_form_float").docName.value = docname;
	$("postcomForm").docUrl.value = $("comment_form_float").docUrl.value = docurl;
	retriveComment();
}
