onready(function(){
	$('chatcontent').focus();
	document.onkeydown=function(e){
		var e=window.event?window.event:e;
		if((e.keyCode==13&&e.ctrlKey)||e.keyCode==13) {_ysyuan.send();$('chatcontent').innerHTML='';}
	}
		$('userto0').checked='checked';
	_ysyuan.first();
	_ysyuan.update();
});
_ysyuan={
	checksrocll:function(){
		if($("ifscroll").checked){
		_ysyuan.insrcoll=1;
		$("public_list").scrollTop = $("public_list").scrollHeight; 
		}else{
			_ysyuan.insrcoll=0;
		}
	},
	first:function(){
		_ysyuan.checksrocll();
		$.ajax.request({url:"http://"+location.host+"/chat/chat.php",method:"post",oncomplete:function(returns){								
				if(returns.responseText!=""){ 								
					$("public_list").html(returns.responseText);	
					$("public_list").scrollTop = $("public_list").scrollHeight;
											
				}
			}},"action=first&"+formToStr($('chatform')));
			setTimeout("_ysyuan.push()",1000);
	},
	send:function(){
		if(/(妈的|垃圾|差劲|我靠|我操|我日|kao|cao|ri|md|nnd|fuck)/gi.test($('chatcontent').value)){
		alert("一生缘提示：请文明用语啦");return false;
		}else if($('chatcontent').value.trim()==""){
			return false;
		}else{
			var lastTime = new Date(),lastmin=lastTime.getMinutes()>10?lastTime.getMinutes():lastTime.getMinutes()+10,lastsec=lastTime.getSeconds()>10?lastTime.getSeconds():lastTime.getSeconds()+10;
			var time=lastTime.getHours()+":"+lastmin+":"+lastsec;
			var s=document.createElement("li");
			if(userid!=0){
				if($('userto1').checked==true){
					s.innerHTML="<b class='self'>我对<u>"+$('userto1').value+"</u>说 "+time+"</b><p>"+_ysyuan.replace_str($('chatcontent').value)+"</p>";
				}else{
					s.innerHTML="<b class='self'>我对大家说 "+time+"</b><p>"+_ysyuan.replace_str($('chatcontent').value)+"</p>";
				};
			}else{
				s.innerHTML="<b class='self'>我对大家说 "+time+"</b><p>"+_ysyuan.replace_str($('chatcontent').value)+"</p>";
			}
			$("public_list").appendChild(s);
			if(_ysyuan.insrcoll) { $("public_list").scrollTop = $("public_list").scrollHeight; }											
			
		$.ajax.request({url:"http://"+location.host+"/chat/chat.php",method:"post",oncomplete:function(returns){
			$('chatcontent').value="";
			if(returns.responseText!=""&&returns.responseText!="|:-:|"){ 
				push_s=returns.responseText.substr(5).split('|:-:|');
				push_s_len=push_s.length;
					for (var key=0;key<push_s_len;){
						var schild=document.createElement("li");
						schild.innerHTML=push_s[key];
						$("public_list").appendChild(schild);
						key++;
						}								
				document.title='有新消息啦! 一生缘聊天室';		
				};	
		}},"action=send&"+formToStr($('chatform')));
		}
		return false;
	},
	
	push:function(){
		$.ajax.request({url:"http://"+location.host+"/chat/chat.php",method:"post",oncomplete:function(returns){
			if(returns.responseText!=""&&returns.responseText!="|:-:|"){ 
					push_s=returns.responseText.substr(5).split('|:-:|');
					if(push_s.length>=1){
					push_s_len=push_s.length;
					for (var key=0;key<push_s_len;){
							var schild=document.createElement("li");
							schild.innerHTML=push_s[key];
							$("public_list").appendChild(schild);
							key++;
					}								
					document.title='有新消息啦! 一生缘聊天室';	
					if(_ysyuan.insrcoll) { $("public_list").scrollTop = $("public_list").scrollHeight; }															
				};								
				}
				_ysyuan.st=setTimeout("_ysyuan.push()",1000);
		},timeout:60,ontimeout:function(){
				_ysyuan.push();
				var schild=document.createElement("li");
						schild.innerHTML="<p>你用猫上网呢？网速有点慢哦...</p>";
						$("public_list").appendChild(schild);
		
		}},"action=push&"+formToStr($('chatform')));
	},
	
	update:function(){
		$.ajax.request({url:"http://"+location.host+"/chat/chat.php",method:"post",oncomplete:function(returns){
			var update_s="var list={"+returns.responseText.substr(1),output='',ii=0;
			eval(update_s);
				for (var keys in list){
					var mclass=keys!=userid?" onclick='_ysyuan.c(this)' ":"";
					output+="<li><u"+mclass+" onmouseover='_ysyuan.showinfo("+keys+",this)' onmouseout=\"$('userinfo_showinfo').style.display='none'\">"+list[keys]+"</u><a target='+blank' href='"+siteurl+"?do=space&uid="+keys+"' title='查看资料'> 资料 </a> <a onclick=\"pm("+keys+",'"+list[keys]+"',this);return false\" title='发短消息'> 发短信 </a></li>";
					ii++;
				};
			$('onlinelist').html(output);
			$('onlinecount').html(ii);
			setTimeout("_ysyuan.update()",60*1000);
		}},"action=update");
	},
	
	c:function(obj){
		$('selectuser').innerHTML=$('userto1').value=obj.innerHTML;
		$('userto1').checked='checked';
		$('userto1').setStyle('visibility:visible');
	},
	
	insertimg:function(){
		var imagePath =$('insert_img').value ;
		if (imagePath==null||imagePath.trim()==""||imagePath.trim()=="http://")return;
		var html="[img:"+imagePath+":]";
		$("chatcontent").value+=html;
		$('insertimgid').wrap();
	},
	
	insertimgs:function(obj){
		dashzhao.extend(obj,dashzhao);
		$('insertimgid').wrap();
		$('insertimgid').setStyle("position:absolute;top:"+(obj.getPos()[1]-60)+"px;left:"+obj.getPos()[0]+"px;");
	},
	
	showinfo:function(skey,obj){
		dashzhao.extend(obj,dashzhao);
		$('userinfo_showinfo').wrap();
		$('userinfo_showinfo').setStyle("position:absolute;top:"+(obj.getPos()[1]-60)+"px;left:"+obj.getPos()[0]+"px;").html("<img src='"+siteurl+"avatar.php?uid="+skey+"&size=small'/>");
	},
	showquick:function(obj){
		dashzhao.extend(obj,dashzhao);
		$('quickset').wrap();
		$('quickset').setStyle("position:absolute;top:"+(obj.getPos()[1]-60)+"px;left:"+obj.getPos()[0]+"px;");
	},
	replace_str:function (output) {
		output=output.replace(/\[em:(.+?):\]/gi,"<img src=\"/image/face/$1.gif\" />");
		output=output.replace(/\[img:(.+?):\]/gi,"<img src=\"$1\" onload=\"if(this.offsetWidth>500){this.style.width='500px'}\"/>");
		return output;
	},
	insertquicks:function(obj){
		$('chatcontent').innerHTML+=obj.innerHTML;
		$('quickset').wrap();
	}	
};

