dojo.require("dojo.io.ScriptSrcIO");

var zhuboURL='http://v.dayoo.com/tc/node_30782/2007-11/01/content_3126178.htm';
var nowURL='http://v.dayoo.com/tc/node_30782/2007-11/01/content_3126178.htm';
/*
load 数据
*/
function dayoo_club_load (/*?*/url) {
	if(url==null || url==''){
		url=nowURL;
	}else{
		nowURL=url;
	}
	var kw = {
		url: "http://club.dayoo.com/comment/redirect.dy?type=json"+(url?('&url='+url):''),
		//url: "http://127.0.0.1/comment/redirect.dy?type=json"+(url?('&url='+url):''),
		transport: "ScriptSrcTransport",
		jsonParamName: "callback",
		preventCache: true,
		load: function (type, data, evt) {dayoo_club_parseClubData(data, dojo.byId('dayoo_club_load'));},
		timeout: function (){},
		timeoutSeconds: 10,
		error:function(type, error){alert('读取数据失败，请联系管理员！'+type+' ERROR:'+error.message);},
		mimetype: "text/javascript",
		preventCache:true,
		apiId: "tingcao"
	};

	dojo.addOnLoad(
	
		function () {
			dojo.io.queueBind(kw);
		}
	);
}

/*
 *	load 主播在线,固定的链接
 *
 */
function dayoo_zhubo_load(){
	var url=zhuboURL;
	var kw = {
		url: "http://club.dayoo.com/comment/redirect.dy?type=json"+(url?('&url='+url):''),
		//url: "http://127.0.0.1/comment/redirect.dy?type=json"+(url?('&url='+url):''),
		transport: "ScriptSrcTransport",
		jsonParamName: "callback",
		preventCache: true,
		load: function (type, data, evt) {dayoo_club_parseClubData(data, dojo.byId('dayoo_zhubo_load'));},
		timeout: function (){},
		timeoutSeconds: 10,
		error:function(type, error){alert('读取数据失败，请联系管理员！'+type+' ERROR:'+error.message);},
		mimetype: "text/javascript",
		preventCache:true,
		apiId: "tingcao"
	};

	dojo.addOnLoad(
	
		function () {
			dojo.io.queueBind(kw);
		}
	);
}


/*
 * 回复
 *
 */
function reply(obj){
	pnode=obj.parentNode.parentNode.parentNode;
	
	
	var name = dojo.dom.firstElement(pnode,'span');
	var doc = dojo.dom.nextElement(name,"div");
	
	var replyContent=doc.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.innerHTML+" 提到】：\r:"+replyContent;
	
	var f=dojo.dom.nextElement(pnode.parentNode.parentNode,"form");
	var c=dojo.dom.firstElement(f,'textarea');
	
	dojo.dom.textContent(c,'');
	dojo.dom.textContent(c, replyContent);
	c.focus();
	
}
 
//过滤标记,两个相邻的相同的标记，现在只保留一个
function filtrate(content,mark){
	while(content.indexOf(mark+mark)!=-1){
		content=content.replace(mark+mark,mark);
	}
	return content;
}
 /**
   * 评论提交检查
  */
  function checkFormComment(object,mark)
  {
      if (object.anonymous.checked == false) {
          if (object.username.value == '') {
              alert('请填写你的用户名!');
			  object.username.focus();
			  return false;
          }

		  if (object.password.value == '') {
              alert('请填写你的密码!');
			  object.password.focus();
			  return false;
          }
      }

	  if (object.content.value == '') {
	      alert('请填写评论内容!');
		  object.content.focus();
		  return false;
	  }
	  
	  var url='';
	  if(mark==1){
	  	url=nowURL;
	  }else{
	  	url=zhuboURL;
	  }
	  
	  object.action="http://club.dayoo.com/comment/redirect.dy?url="+url;
	  object.submit();
	  object.reset();
	  object.username.disabled = false;
	  object.password.disabled = false;
	  
	  alert('发帖成功！');
	  if(mark==1){
		setTimeout('dayoo_club_load()',5000);
	  }else{
		 setTimeout('dayoo_zhubo_load()',5000);
	  }
	  
	 // dayoo_club_load (window.location);
  } // end func

/*
dayoo_club_loginname
dayoo_club_content
dayoo_club_postdate
*/
function dayoo_club_parseClubData (data, node, parent) {
	
	var tplNode = node.cloneNode(true);
	
	tplNode.style.display='';
	tplNode.id='';
	var pNode	= !parent?node.parentNode:parent;

	dojo.dom.removeChildren(pNode);
	pNode.appendChild(node);
		
	for (var i=0; i < data.length; i++)
	{
		data[i].content=data[i].content.replace(/\r/g,'');
		data[i].content=data[i].content.replace(/\n/g,'');
		_dayoo_club_setVariable(tplNode, 'name', data[i].loginname);
		_dayoo_club_setVariable(tplNode, 'doc', data[i].content, true);
		_dayoo_club_setVariable(tplNode, 'date', strftime(data[i].postdate*1000, "%Y-%m-%d %H:%i")+" 发表");

		pNode.appendChild(tplNode.cloneNode(true));

	}
	
	//alert(pNode.innerHTML);
}

function _dayoo_club_setVariable(node, placeHolder, text, isHtml) {
	var elements = dojo.html.getElementsByClass(placeHolder, node);
	

	for (var i=0; i < elements.length; i++)
	{
		if (isHtml) {
			elements[i].innerHTML = text;
		} else {
			dojo.dom.textContent(elements[i], text);
		}
	}
}

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()];
			}
		}));
	}
}