dojo.require("dojo.io.script");

dojo.addOnLoad(
	function(){
		//C_nowURL='http://news.dayoo.com/guangzhou/zhuanti/node_40068/node_40181/2008-05/29/content_3411181.htm';
		//C_nowURL='http://2008.dayoo.com/200808/01/56799_3537871.htm';
        C_getSubjectId();
    }

);


var PG1=new PageGuide("PG1",C_getCommentList);
PG1.pagesize=15;


var C_nowURL='';
var C_subjectid=0;
var C_parentid=0;
var C_channel='';
var C_title='';
var C_order='postdate desc';
var C_mode=2;
var C_main=0;
var C_AutoRunTimeout=null;

var clubURL="http://comment1.dayoo.com/";
var clubURL_Post="http://club.dayoo.com/";

function C_getSubjectId(url){
	var postUrl=clubURL+"comment2/getSubjectId_json.dy";	
	
	if(url==null)url=C_nowURL;
	else C_nowURL=url;
	
	postUrl+="?url="+(url?('&url='+encodeURIComponent(dojo.trim(url))):'');
	dayoo_load_data(postUrl,C_getSubjectId_handle);
	
}
function C_getSubjectId_handle(data){
	C_subjectid=data.subjectid;
	C_parentid=data.subjectid;
	C_channel=data.channel;
	C_title=data.title;
	
	try{document.getElementById('title').value='回复：'+C_title;}catch(e){}
	
	C_getCommentList();
}

function C_getCommentList(pid){
	
	CommentAutoRun_onclick();
	
	try{document.getElementById('loadgif').style.display='';}catch(e){}
	try{document.getElementById('part1').style.display='none';}catch(e){}
	
	
	var postUrl=clubURL+"comment2/getCommentList_json.dy";
	
	if(C_subjectid=='' || C_subjectid==0){
		alert('执行错误！');
		return;
	}
	
	if(pid!=null){
		C_parentid=pid;
	}else if(C_parentid==''){
		C_parentid=C_subjectid;
	}
	
	postUrl+="?&subjectid="+C_subjectid+"&parentid="+C_parentid+"&channel="+C_channel+"&order="+C_order+"&page="+PG1.page+"&perpage="+PG1.pagesize+"&mode="+C_mode+"&main="+C_main;
	dayoo_load_data(postUrl,C_getCommentList_handle);
	
}
function C_getCommentList_handle(data){
	document.getElementById('guide1').innerHTML=PG1.show(data.total,PG1.page);
	var node=document.getElementById('CommentItem');
	showRecord(data.list,node);
}


//显示记录
function showRecord (data,node) {
	
	try{document.getElementById('loadgif').style.display='none';}catch(e){}
	try{document.getElementById('part1').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='';
	

	var repaceFun=function(html,data){
		var html2='';
		html2=html.replace(/#c_id#/g,data.id);
		
		html2=html2.replace(/#c_subjectid#/g,data.subjectid);
		html2=html2.replace(/#c_loginname#/g,data.loginname);
		html2=html2.replace(/#c_title#/g,data.title);
		html2=html2.replace(/#c_content#/g,data.content);
		
		html2=html2.replace(/#c_hits#/g,data.hits);
		html2=html2.replace(/#c_reply#/g,data.reply);
		
		html2=html2.replace(/#c_postdate#/g,strftime(data.postdate*1000,"%m-%d %H:%i"));
		html2=html2.replace(/#c_last_postdate#/g,strftime(data.last_postdate *1000,"%m-%d %H:%i"));
		
		if(data.attach_url!='')html2=html2.replace(/#c_img#/g,"<img align='middle' src="+data.attach_url+"></img>");
		else html2=html2.replace(/#c_img#/g,"");
		
		html2=html2.replace(/#c_nickname#/g,data.nickname);

		html2=html2.replace(/#c_channel#/g,C_channel);
		html2=html2.replace(/#c_floor#/g,data.c_floor);
		return html2;
	}
	
	var html=tplNode2.innerHTML;
	
	if(data!=null){
	
		for (var i=0; i<data.length; i++){
			tplNode2.innerHTML=repaceFun(html,data[i]);
			pNode.appendChild(tplNode2.cloneNode(true));
		}
	}
}

//日期转换
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()];
			}
		}));
	}
}



function C_post(object){
	var posturl=clubURL_Post+'comment2/post.dy';
	
	var name=object.username.value;
	var pw=object.password.value;
	var title=object.title.value;
	var content=object.content.value;
	var anonymous=object.anonymous;

	  if(anonymous.checked!=true){
		  if(name.replace(/ /g)==''){
			  alert('请输入用户名！');
			  object.username.focus();
			 return false;
		  }
		  if(pw.replace(/ /g)==''){
			  alert('请输入密码名！');
			  object.password.focus();
			  return false;
		  }
	  }
	  if(title.replace(/ /g)==''){
		  alert('发帖错误，请与技术人员联系!');
		  //object.title.focus();
		  return false;
	 }
	 
	  if(content.replace(/ /g)==''){
		  alert('请填写评论内容!');
		  object.content.focus();
		  return false;
	 }
	  
	 

	 object.action=posturl+"?subjectid="+C_subjectid+"&parentid="+C_parentid+'&channel='+C_channel;

	 object.submit();
	 object.reset();
     object.title.value=title;
	  
	 alert('发帖成功！');
	 try{hiddenForm();}catch(e){}
  	 PG1.page=1;
	 try{document.getElementById('loadgif').style.display='';}catch(e){}
	 try{document.getElementById('part1').style.display='none';}catch(e){}
	 setTimeout("C_getCommentList()",5000);

}


function getSubjectByUrl(){
	var param=getUrlParam('param');
	var piece=param.split("_");
	if(piece.length==3 && !isNaN(parseInt(piece[0])) && !isNaN(parseInt(piece[1])) && piece[2]!='' && isNaN(parseInt(piece[2])) ){
		C_subjectid=piece[0];
		C_parentid=piece[1];
		C_channel=piece[2].replace(/#/g,'');
		C_getCommentList();
	}else{
		
	}
}

//获取url参数
function getUrlParam(name){
	var url=window.location.toString();
	
	var pos = url.indexOf("?")
	var parastr = url.substring(pos+1);

	if (pos>0){
		//if (url.indexOf("&")>0){
			var para = parastr.split("&");
			var tempstr1='';
			for(i=0;i<para.length;i++){
				tempstr1 = para[i];
				pos = tempstr1.indexOf("=");
				if(tempstr1.substring(0,pos)==name)
				return tempstr1.substring(pos+1);
			}
		//}
	}
	
}


/*
 * 回复
 *
 */
function C_reply(obj){
	pnode=obj.parentNode.parentNode.parentNode;
	
	var div=pnode.getElementsByTagName('DIV');
	
	var title = C_title;
	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){}
	
}

/*
load 数据
*/
function dayoo_load_data(posturl,hf) {
	dojo.io.script.get({
		url: posturl,
		callbackParamName: "callback",
		handleAs: "json",
		preventCache: true,
		handle: function(data, ioArgs){
			hf(data);
		}
	});
}

function CommentAutoRun_onclick(){
	try{
		if(document.getElementById('CommentAutoRun').checked==true){
			window.clearTimeout(C_AutoRunTimeout);
			C_AutoRunTimeout=window.setTimeout('C_getCommentList()',120000);
		}else{
			window.clearTimeout(C_AutoRunTimeout);
		}
	}catch(e){}
}

//分页
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;
		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;
	}
}


//页面js
function displayForm(){
	document.getElementById('part1').style.display='none';
	document.getElementById('part2').style.display='';
}
function hiddenForm(){
	document.getElementById('part2').style.display='none';
	document.getElementById('part1').style.display='';
}
