dojo.require("dojo.io.script");

dojo.addOnLoad(function(){
	setTimeout('dayoo_club_load(1)',1000);
});


//远程论坛地址
var getClubListUrl="http://club.dayoo.com/service/JSON/clubList.dy";
//发帖地址
var postClubUrl="http://club.dayoo.com/outerPost/post.dy";
//帖子地址
var readClubUrl="http://club.dayoo.com/read.dy";
/* 获取页面中有个getClubList 样式的元素，并取获取它的id
 * id格式 『 板块名_子板块id_显示条数 』 如 friend_0_10
*/
function dayoo_club_load(multi) {
	var elements=new Array();
	elements=document.getElementsByName("clublist");

	if(multi==1){
		var params="";
		var items=new Array();
		for(var i=0;i<elements.length;i++){
			items=elements[i].value.split("@");
			if(document.getElementById(items[1]+'_template')==null){
				
				var ul=document.getElementById(items[1]);
				var tplNode = ul.cloneNode(true);
				tplNode.id=items[1]+'_template';
				tplNode.style.display='none';
				document.body.appendChild(tplNode);
				
			}
			params+="params[]="+items[0]+"_"+items[1]+"&";
		}
		dojoIoBindMulti(params);
	}else{
		for(var i=0;i<elements.length;i++){
			dojoIoBind(elements[i].id);
		}
	}

}
function doJsonpCallback(jsonS){
	eval('var jsonObj=('+jsonS+');');
	for(var j=0;j<jsonObj.length;j++){
		dayoo_club_parseClubData(jsonObj[j]);
	}
	try{
		clubReturnFun();
	}catch(e){}

}


//提交单个版块信息
function dojoIoBind(param){
	var td = dojo.io.script.get({
		url: getClubListUrl,
		callbackParamName: "doJsonpCallback",
		content:  { params: "10_1_"+param },
		preventCache: true,
		handle: function(response, ioArgs){}
	});
}


//提交多个版块信息
function dojoIoBindMulti(params){
	var td = dojo.io.script.get({
		url: getClubListUrl+"?"+params,
		callbackParamName: "doJsonpCallback",
		content:  '',
		preventCache: true,
		handle: function(response, ioArgs){}
	});
}

//显示
function dayoo_club_parseClubData(data){
	var node=dojo.byId(data.params);
	var oChild=node.getElementsByTagName('li')[0];
	node.removeChild(oChild);

	var template=dojo.byId(data.params+"_template")
	var li=template.getElementsByTagName('li')[0];
	var tplNode = li.cloneNode(true);
	
	var a=tplNode.getElementsByTagName('a')[0];
	var span=tplNode.getElementsByTagName('span')[0];
	

	var href='',title='',shorttitle='',time='',innerHTML='';
	for(var j=0;j < data.data.length;j++){
		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>18){
			shorttitle=title.substr(0,18)+"...";
		}else{
			shorttitle=title;
		}
		
		time=strftime(data.data[j].postdate*1000, "%H:%i");
		
		a.href=href;
		a.title=title;
		a.innerHTML=shorttitle;
		
		//if(span!=null)
		//span.innerHTML=time;
		
		node.appendChild(tplNode.cloneNode(true));

	}
	node.style.display='';
}

function showPostWin(b,cbid,ct){
	if(ct==null)ct='1';
	var scroll = dojo.html.getScroll();
	var viewport = dojo.html.getViewport();
	dojo.byId('postWin').style.top=scroll.top+viewport.height/5+"px";
	dojo.byId('postWin').style.left=scroll.left+viewport.width/5+"px";
	dojo.byId('outerPostIframe').src=postClubUrl+"?b="+b+"&cbid="+cbid+"&ct="+ct;
	dojo.widget.byId('postWin').show();
}

function hidePostWin(){
	dojo.widget.byId('postWin').hide();
}


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 showPostWin(b,cbid,ct){
	if(ct==null)ct='1';

	dojo.byId('postWin').style.top=(winCH()/3+winST())+"px";
	dojo.byId('postWin').style.left=(winCW()/3)+"px";
	dojo.byId('outerPostIframe').src=postClubUrl+"?b="+b+"&cbid="+cbid+"&ct="+ct;
	dojo.byId('postWin').style.display='';
}

function hidePostWin(){
	dojo.byId('postWin').style.display='none';
}

var browserName = navigator.appName;
var browse=0;
if (browserName == "Microsoft Internet Explorer") browse=1;
if (browserName == "Netscape") browse=2;
var winCH=function(){
	if(document.documentElement.clientHeight==0) return document.body.clientHeight;
	if(browse==2 && document.documentElement.clientHeight>document.body.clientHeight)return document.body.clientHeight
	else return document.documentElement.clientHeight;
}
var winCW=function(){
	if(document.documentElement.clientWidth==0) return document.body.clientWidth;
	if(browse==2 && document.documentElement.clientWidth>document.body.clientWidth)return document.body.clientWidth
	else return document.documentElement.clientWidth;
}
var winST=function(){
	if(document.documentElement.scrollTop==0) return document.body.scrollTop;
	else return document.documentElement.scrollTop;
}
var winSL=function(){
	if(document.documentElement.scrollLeft==0) return document.body.scrollLeft;
	else return document.documentElement.scrollLeft;
}
