dojo.require("dojo.io.script");

var PG1=new PageGuide("PG1",dayoo_club_load);
PG1.pagesize=15;
ClubBoard='';


dojo.addOnLoad(
    function(){
		ClubBoard='lhzq_0';
        PG1.pagesize=12;
        setTimeout("dayoo_club_load()",1000);
    }
);

//远程论坛地址
var getClubListUrl="http://club.dayoo.com/service/JSON/clubList.dy";
//帖子地址
var readClubUrl="http://club.dayoo.com/read.dy";
/* 获取页面中有个getClubList 样式的元素，并取获取它的id
 * id格式 『 板块名_子板块id_显示条数 』 如 friend_0_10
*/
function dayoo_club_load() {	
	dojo.byId("part1").style.display='none';
	dojo.byId("loadgif").style.display='';
	dojoIoBind(PG1.pagesize+"_"+PG1.page+"_"+ClubBoard);
}
function doJsonpCallback(jsonS){
	eval('var jsonObj=('+jsonS+');');
	dayoo_club_parseClubData(jsonObj);
	try{
		clubReturnFun();
	}catch(e){}

}


//提交单个版块信息
function dojoIoBind(param){
	var td = dojo.io.script.get({
		url: getClubListUrl,
		callbackParamName: "doJsonpCallback",
		content:  { params: param },
		preventCache: true,
		handle: function(response, ioArgs){}
	});
}



//显示
function dayoo_club_parseClubData(data){
	
	var commenttable=document.getElementById("CommentTable");
	 while(commenttable.rows.length>1) {
	   commenttable.deleteRow(1);
     }
	
	var href='',title='',shorttitle='';
	var newTr=null,newTd1=null,newTd2=null,newTd3=null,newTd4=null,newTd5=null;
	for(var j=0;j < data.data.length;j++){

		//添加一行 
		newTr = commenttable.insertRow(-1); 
		//添加列 
		newTd1 = newTr.insertCell(-1); 
		newTd2 = newTr.insertCell(-1); 
		newTd3 = newTr.insertCell(-1); 
		newTd4 = newTr.insertCell(-1);
		newTd5 = newTr.insertCell(-1); 
		

		href=readClubUrl+"?b="+data.board+"&t="+data.data[j].id+"&i="+data.data[j].id;
		
		title=data.data[j].title;
		title=title.replace(/&nbsp;/g,' ');
		title=title.replace(/&#91;/g,'[');
		title=title.replace(/&#93;/g,']');
		title=title.replace(/&quot;/g,'"');
		title=title.replace(/&#039;/g,'\'');
		title=title.replace(/ /g,'');
		
		if(title.length>20){
			shorttitle=title.substr(0,20)+"...";
		}else{
			shorttitle=title;
		}
		
		//设置列内容和属性 
		newTd1.className='club_td1';
		newTd1.innerHTML = '<a target="_blank" title="'+title+'" href="'+href+'">'+shorttitle+'</a>'; 
		newTd2.innerHTML= data.data[j].hits; 
		newTd3.innerHTML= data.data[j].reply; 
		newTd4.innerHTML= data.data[j].nickname; 
		newTd5.innerHTML= strftime(data.data[j].last_postdate*1000, "%m-%d %H:%i"); 
		
	}
	dojo.byId('guide1').innerHTML=PG1.show(data.total[0],PG1.page);

	
	dojo.byId("part1").style.display='';
	dojo.byId("loadgif").style.display='none';
	//node.style.display='';
}


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 checkFormSubmit(object){
	
	if(object.username.value == '') {
		alert('请填写您的用户名!');
		object.username.focus();
		return false;
	}
	if(object.password.value == '') {
		alert('请填写您的密码!');
		object.password.focus();
		return false;
	}

	if(object.title.value == '') {
		alert('请填写标题!');
		object.title.focus();
		return false;
	}
	if(object.content.value == '') {
		alert('请填写内容!');
		object.content.focus();
		return false;
	}
	object.submit();
	object.reset();
	hiddenForm();
} // end func


function displayForm(){
	dojo.byId('part1').style.display='none';
	dojo.byId('part2').style.display='';
}

function hiddenForm(){
	dojo.byId('part1').style.display='';
	dojo.byId('part2').style.display='none';
}


//分页
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;
	}
}
