dojo.require("dojo.io.script");


function CommentList(objectName){
	this.objectName=objectName;

	this.PG1=new PageGuide(this.objectName+".PG1",this.objectName+'.C_getCommentList');
	this.PG1.pagesize=15;

	this.C_form='';
	this.C_nowURL='';
	this.C_subjectid=0;
	this.C_parentid=0;
	this.C_board='';
	this.C_title='';
	this.C_order='postdate desc';
	this.C_mode=2;
	this.C_main=0;
	this.C_AutoRunTimeout=null;
	
	this.clubURL="http://club.dayoo.com/comment2/getCommentList2_json.dy";
	this.clubURL_Post="http://club.dayoo.com/outerPost/post2.dy";
	this.LoginURL="http://club.dayoo.com/service/JSON/checkIsLogin.dy";
	
	this.C_IsLogin=0;
	this.LoginName='';
	this.NickName='';
	
	//检查是否登录
	this.C_checkLogon=function(){
		this.dayoo_load_data(this.LoginURL,'C_loginHandle');
	}
	
	//登录回调处理
	this.C_loginHandle=function(data){
		try{
			if(data.login=='1'){
				dojo.byId(this.C_form+'_notLogin').style.display='none';
				dojo.byId(this.C_form+'_isLogin').style.display='';
				dojo.byId(this.C_form+'_spanUser').innerHTML=data.loginName;
				this.LoginName=data.loginName;
				if(data.nickName!=''){
					this.NickName=data.nickName;
					dojo.byId(this.C_form+'_spanUser').innerHTML+="["+data.loginName+"]";
				}
				this.C_IsLogin=1;
			}
		}catch(e){}
	}
	
	this.C_getSubjectId=function(){
		dojo.byId(this.C_form+'_commentURL').href=this.C_nowURL;
		
		var url=this.C_nowURL;
		var regx='';
		var rs=null;
		
		if(url.indexOf(".htm")!=-1){
			regx=/read-([^-]*)-(\d*)/;
			rs=regx.exec(url);
			this.C_board=rs[1];
			this.C_subjectid=rs[2];
			this.C_parentid=rs[2];
		}else{
			regx=/b=([^\&&^\=]*)\&/;//var regx=/.*\/[^\/&^\_]*\_(\d*)\./;
			rs=regx.exec(url);
			this.C_board=rs[1];
		
			regx=/t=(\d*)/;
			rs=regx.exec(url);
			this.C_subjectid=rs[1];
			this.C_parentid=rs[1];
		}
		
		
		this.C_getCommentList();
			
	}
	
	this.C_getCommentList=function(){
		
	
		try{dojo.byId(this.C_form+'_loadgif').style.display='';}catch(e){}
		try{dojo.byId(this.C_form+'_part1').style.display='none';}catch(e){}
		
			
		if(this.C_subjectid=='' || this.C_subjectid==0){
			alert('参数错误！');
			return;
		}
				
		var postUrl=this.clubURL;
		postUrl+="?&subjectid="+this.C_subjectid+"&parentid="+this.C_parentid+"&board="+this.C_board+"&order=id desc&page="+this.PG1.page+"&perpage="+this.PG1.pagesize+"&mode="+this.C_mode+"&main="+this.C_main;
		this.dayoo_load_data(postUrl,"C_getCommentList_handle");		
	}
	
	this.C_getCommentList_handle=function(data){
		data.total=parseInt(data.total);
		dojo.byId(this.C_form+'_commentCount').innerHTML=data.total;
		
		if(data.total==0)dojo.byId(this.C_form+'_guide1').innerHTML="暂无评论";
		else dojo.byId(this.C_form+'_guide1').innerHTML=this.PG1.show(data.total,this.PG1.page);
		
		var node=dojo.byId(this.C_form+'_CommentItem');
		this.showRecord(data.list,node);
	}
	
	
	//显示记录
	this.showRecord=function(data,node) {
		
		try{dojo.byId(this.C_form+'_loadgif').style.display='none';}catch(e){}
		try{dojo.byId(this.C_form+'_commentContent').style.display='';}catch(e){}
	
		var pNode	= node.parentNode;
		var tplNode = node.cloneNode(true);
		var tplNode2 = node.cloneNode(true);
	
		pNode.innerHTML='';
		pNode.appendChild(tplNode);
	
		tplNode2.style.display='';
		tplNode2.id='';
		
		if(data!=null){
			for (var i=0; i<data.length; i++){

				if(data[i].nickname=="")data[i].nickname=data[i].loginname;
				else data[i].nickname=data[i].loginname+"["+data[i].nickname+"]";
				
				dojo.query(".inforUser",tplNode2)[0].getElementsByTagName("em")[0].innerHTML=data[i].nickname;
				dojo.query(".inforUser",tplNode2)[0].getElementsByTagName("em")[1].innerHTML=strftime(data[i].postdate*1000,"%m-%d %H:%i");
				dojo.query(".c",tplNode2)[0].innerHTML=data[i].content;
				
				if(data[i].attach_url!="")
					dojo.query(".imgBox",tplNode2)[0].innerHTML="<a href='"+data[i].attach_url+"' target='_blank'><img border=0 onload='"+this.objectName+".fixSize(this)' align='middle' src="+data[i].attach_url+"></img></a>";
				else
					dojo.query(".imgBox",tplNode2)[0].innerHTML="";

				pNode.appendChild(tplNode2.cloneNode(true));
			}
		}

	}
	
	this.fixSize=function(img){
		var imgWidth=600;
		var imgHeight=600;
		
		var w=(img.width);
		var h=(img.height);
		if(w>imgWidth && h>imgHeight){
			if(w>h){
				img.style.width=imgWidth+"px";
				img.style.height=(h/w*imgWidth)+"px"
			}else{
				img.style.height=imgHeight+"px";
				img.style.width=(w/h*imgHeight)+"px"
			}
		}else if(w>imgWidth){
			img.style.width=imgWidth+"px";
			img.style.height=(h/w*imgWidth)+"px"
		}else if(h>imgHeight){
			img.style.height=imgHeight+"px";
			img.style.width=(w/h*imgHeight)+"px"
		}else{
			img.style.height=h+"px";
		}
	}
	
	//日期转换
	function strftime(time, format) {
		var monthArray = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
		
		var aTime = new Date(time);
		
		var month = (aTime.getMonth()+1).toString().length==1?'0'+(aTime.getMonth()+1):(aTime.getMonth()+1);
		var day = aTime.getDate().toString().length==1?'0'+aTime.getDate():aTime.getDate();
		var hour = aTime.getHours().toString().length==1?'0'+aTime.getHours():aTime.getHours();
		var min = aTime.getMinutes().toString().length==1?'0'+aTime.getMinutes():aTime.getMinutes();
		var sec = aTime.getSeconds().toString().length==1?'0'+aTime.getSeconds():aTime.getSeconds();
		
		if (!format){
			return (monthArray[aTime.getMonth()]+' '+day+' '+aTime.getFullYear()+' '+hour+':'+min+':'+sec);
		} else {
			return (format.replace(/%[a-zA-Z%]/g, function (word){
				switch (word) {
					case '%%': return '%';
					case '%Y': return aTime.getFullYear();
					case '%m': return month;
					case '%d': return day;
					case '%H': return hour;
					case '%i': return min;
					case '%s': return sec;
					case '%M': return monthArray[aTime.getMonth()];
				}
			}));
		}
	}
	
	
	
	this.C_post=function(obj){
		
		if(this.C_IsLogin==0){
			if(typeof(IsLogin)=='undefined' || IsLogin!=1){
				if(obj.username.value.replace(/ /g)==''){
					alert('请输入用户名！');
					obj.username.focus();
					return false;
				}
				
				if(obj.password.value.replace(/ /g)==''){
					alert('请输入用户密码！');
					obj.password.focus();
					return false;
				}
				
				 if(obj.username.value != '' && obj.password.value != ''){
					obj.anonymous.value=0;
					obj.nickname.value='';
				 }else{
					obj.anonymous.value=1;
					obj.nickname.value=obj.username.value;
				 }
			}else{
				obj.anonymous.value=0;
				obj.nickname.value='';
			}
		}else{
			obj.anonymous.value=0;
			obj.nickname.value='';
		}

		
		if(obj.content.value.replace(/ /g)=='' || obj.content.value=='文明上网　理性发言'){
			alert('请填写评论内容！');
			obj.content.focus();
			return false;
		}
		
		if(this.C_board=='' || this.C_subjectid==0 || this.C_parentid==0){
			alert('参数错误，请刷新页面！');
			return false;
		}

		obj.action=this.clubURL_Post+"?t="+this.C_subjectid+"&r="+this.C_parentid+'&b='+this.C_board+"&reply=1&showReturn=2";
		
		obj.submit();
		var nickname=obj.nickname.value;
		var username=obj.username.value;
		alert('感谢您发表评论，您发表的评论将在管理员审核后发布！');
		this.PG1.page=1;
		try{dojo.byId(this.C_form+'_loadgif').style.display='';}catch(e){}
		try{dojo.byId(this.C_form+'_commentContent').style.display='none';}catch(e){}
		setTimeout(this.objectName+".C_getCommentList()",3000);
		setTimeout(this.objectName+".C_checkLogon()",1000);
		setTimeout(function(){obj.reset();obj.nickname.value=nickname;obj.username.value=username;},2000);
		return false;
	}
	
		
	/*
	 * 回复
	 *
	 */
	this.C_reply=function(obj){
		pnode=obj.parentNode.parentNode.parentNode;
		
		var div=pnode.getElementsByTagName('DIV');
		
		var name  = div[0].getElementsByTagName('span')[0].getElementsByTagName('em')[0].innerHTML;
		var replyContent=div[1].innerHTML;
		
		replyContent=replyContent.replace("<div class=\"quote\">",'');
		replyContent=replyContent.replace("</div>","");
		replyContent=replyContent.replace("<DIV class=quote>",'');
		replyContent=replyContent.replace("</DIV>","");
		replyContent=replyContent.replace(/\r/g,'');
		replyContent=replyContent.replace(/\n/g,'');
	
		replyContent=replyContent.replace(/<br>/g,"\r:");
		replyContent=replyContent.replace(/<BR>/g,"\r:");
		replyContent=replyContent.replace(/&nbsp;/g," ");
	
		
		replyContent="\r\r:【"+name+" 提到】：\r:"+replyContent;
		
		document.getElementById('content').value=replyContent;
		document.getElementById('content').focus();
		
		try{
			var rtextRange =document.getElementById('content').createTextRange();
			rtextRange.moveStart('character',0);
			rtextRange.collapse(true);
			rtextRange.select();
		}catch(e){}
		
	}
	
	//匿名与登录切换
	this.pp1Onclick=function(){
		dojo.byId(this.C_form+'_pp1').style.display='none';
		dojo.byId(this.C_form+'_pp2').style.display='block';
	}
	
	//登录与匿名切换
	this.pp2Onclick=function(){
		dojo.byId(this.C_form+'_pp1').style.display='block';
		dojo.byId(this.C_form+'_pp2').style.display='none';
	}
	
	/*
	load 数据
	*/
	this.dayoo_load_data=function(posturl,hf) {
		var objectName=this.objectName;
		dojo.io.script.get({
			url: posturl,
			callbackParamName: "callback",
			handleAs: "json",
			preventCache: true,
			handle: function(data, ioArgs){
				eval(objectName+"."+hf+"(data)");
			}
		});
	}
	
	//分页
	function PageGuide(objname,fun)
	{

		this.objName=objname;//定义当前对象的变量名称
		this.pagesize=10;//每页记录数
		this.totalrecord=0;//总记录个数
		this.page=1;//当前页号
		this.realpagecount=0;//实际的总页数
		this.colsize=6;//导航条一行显示的页数
		this.mode='0111111';//显示模式
		this.pageFun=fun;//外部调用的函数
		this.params;//外部调用的函数的参数
		this.list;//显示的导航列表（html代码）
		
		//回调函数
		this.handle=function(p){
			this.page=p;
			eval(this.pageFun+"(this.params)");
			
			
		}
		//显示分页导航
		this.show=function(total,p,params){
			this.totalrecord=total;
			this.page=p;
			if(params!=null){
				this.params=params;
			}
			
			//计算实际的页数
			if(this.totalrecord<=this.pagesize){
				this.realpagecount=1;
			}else{
				var c1=this.totalrecord/this.pagesize;
				var c2=this.totalrecord%this.pagesize;
				if(c2!=0){
					this.realpagecount=Math.ceil(c1);
				}else{
					this.realpagecount=c1;
				}
			}
			if(this.page>this.realpagecount || this.page<=0 ){	
				//var pp=this.page-1;
				if(this.page>1)
					this.page=this.realpagecount;
				else
					this.page=1;
			}
			
			if(this.page!=1){
				//第一页
				var First="1";
				First="href=\"###\" onclick=\""+this.objName+".handle("+First+")\"";
				//前一页
				var ColFirst=parseInt(this.page)-1;
				ColFirst="href=\"###\" onclick=\""+this.objName+".handle("+ColFirst+")\"";
			}else{
				var ColFirst="";
				var First="";
			}
			
			if(this.page!=this.realpagecount){
				//最后一页
				var Last=this.realpagecount;
				Last="href=\"###\" onclick=\""+this.objName+".handle("+Last+")\"";
				//后一页
				var ColLast=parseInt(this.page)+1;
				ColLast="href=\"###\" onclick=\""+this.objName+".handle("+ColLast+")\"";
			}else{
				var ColLast="";
				var Last="";
			}
			
			//开始生成分页框架
			this.list="<div class='PageGuide'>";
			
			if(this.mode.substr(0,1)=='1')
			this.list+="共"+this.totalrecord+"条记录";
		
			if(this.mode.substr(1,1)=='1')
			this.list+="&nbsp;共"+this.realpagecount+"页";
			
			if(this.mode.substr(2,1)=='1')
			this.list+="&nbsp;<a  "+First+">第一页</a>";
		
			if(this.mode.substr(3,1)=='1')
			this.list+="&nbsp;<a  "+ColFirst+">上一页</a>";
		 
			if(this.realpagecount<=this.colsize){
				this.count=this.realpagecount;
				this.startpage=1;
			}else{
				if(this.page<=Math.ceil(this.colsize/2))	{
					this.startpage=1;
					if(this.realpagecount<=this.colsize)
						this.count=this.realpagecount;
					else
						this.count=this.colsize;
				}else{
					this.startpage=this.page-Math.ceil(this.colsize/2)+1;
					if(this.startpage+this.colsize-1<=this.realpagecount){
						this.count=this.colsize;				
					}else{
						this.count=this.realpagecount-this.startpage+1;
					}
		
					if(this.startpage-(this.colsize-this.count)>1){
						this.startpage=this.startpage-(this.colsize-this.count);
						this.count=this.colsize;
					}
				}
				
			}
			
			if(this.mode.substr(4,1)=='1'){
				for(var i=this.startpage;this.count+this.startpage>i;i++){
					if(this.page!=i)
						this.list+="&nbsp;<a href=\"###\" onclick=\""+this.objName+".handle("+i+")\">"+i+"</a>";
					else
						this.list+="&nbsp;<span>"+i+"</span>";
				}
			}
		
			if(this.mode.substr(5,1)=='1')
			this.list+="&nbsp;<a "+ColLast+">下一页</a>";
			
			if(this.mode.substr(6,1)=='1')
			this.list+="&nbsp;<a "+Last+">最后页</a>";
		
			this.list+="</div>";
			return this.list;
		}
	}

}
