function MsgSync(){
	return this.init.apply(this,arguments);
};
MsgSync.prototype = (function(){
	var getCookie = function(key,def){
		try{
			var data = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)")) || "";
			if(data){
				return eval("(" + unescape(data[2]) + ")");
			}
		}catch(e){return def;}
		return def;
	};

	var setCookie = function(key,data){
		document.cookie = key + "=" + escape(Object.serialize2Str(data)) + ";path=/";
	};

	var clientsKey = "msg_sync_clients";	//存储client列表的cookie key
	var msgsKey = "msg_sync_msgs";			//存储msg列表的cookie key

	return {
		init:function(name){
			this.info = {	//当前client信息
				clientId	: "client-" + Math.random().toString(36).replace("0.",""),	//唯一ID，随便写了下，反正就是这个意思
				name		: name || "default",	//页面名称，不唯一
				isServer	: false				//是不是主机
			};
			this.onNewMsg = function(e){

			};
			BBEvent.observe(window,"unload",function(){
				this._remove(this.info.clientId);
				setCookie(clientsKey,this.clients);
			}.$bind(this));
			this._getClients();
			this._run();
		},
		sendMsg:function(opt){//发送消息，必须指定clientId或者name,以及data
			var cid = opt.clientId,
				name = opt.name||"default",
				data = opt.data,
				clients = [];
			if(cid){	//指定发送页面clientId
				var client = this._findClient(cid);
				clients = client?[client]:[];
			}else if(name){ //指定发送页面name
				clients = this._findClients(name);
			}else{
				return false;
			}

			if(clients.length > 0 && opt.data){
				this._sendMsg(clients,data);
			}
		},
		_run:function(){
			this.intId = setInterval(function(){
				this._clearMsg();
				this._readMsg();
				this._getClients();
			}.$bind(this),500);
		},
		_findClient:function(clientId){//找到指定clientId的client
			var index = this._indexOf(clientId);
			return index > -1 ? this.clients[index] : null;
		},
		_findClients:function(name){//找到指定name的client(s)
			var ret = [];
			this.clients.each(function(client,index){
				if(client.name == name){
					ret.push(client);
				}
			});
			return ret;
		},
		_indexOf:function(clientId){//找到指定clientId的client
			var index = -1;
			this.clients.each(function(client,idx){
				if(client.clientId == clientId){
					index = idx;
				}
			});
			return index;
		},
		_findSever:function(){//找到主机
			this.clients.each(function(client,index){
				if(client.isServer){
					return index;
				}
			});
			return -1;
		},
		_remove:function(clientId){//移除指定clientId的client
			var index = this._indexOf(clientId);
			this.clients.removeAt(index);
		},
		_sendMsg:function(clients,data){//发送消息
			var msgs = getCookie(msgsKey,[]);
			clients.each(function(client){
				if(this.info.clientId != client.clientId){//不能发给自己
					var d = {};
					d._clientId_ = client.clientId;
					d._from_ = this.info.clientId;
					d._status_ = "new";
					Object.extendJson(d,data);
					msgs.push(d);
				}
			}.$bind(this));
			setCookie(msgsKey,msgs);
		},
		_readMsg:function(){//读取跟自己有关的消息
			var msgs = getCookie(msgsKey,[]);
			msgs.each(function(msg){
				if(msg._clientId_ == this.info.clientId && msg._status_ == "new"){//只专注发给自己的新信息
					msg._status_ = "read";
					setCookie(msgsKey,msgs);
					this.onNewMsg(msg);
				}
			}.$bind(this));
		},
		_clearMsg:function(){//清除无用的消息
			var msgs = getCookie(msgsKey,[]),
				msgsLength = msgs.length,
				newMsgs = [];
			msgs.each(function(msg){
				if(msg._status_ != "read"){
					newMsgs.push(msg);
				}
			}.$bind(this));
			if(msgsLength!=newMsgs.length)setCookie(msgsKey,newMsgs);
		},
		_getClients:function(){//维护client列表
			this.clients = getCookie(clientsKey,[]);	//client列表
			if(this._indexOf(this.info.clientId) < 0){
				this.clients.push(this.info);
				setCookie(clientsKey,this.clients);
			}
		}
	};
})();