var type = 0;
var idInterval = 0;
var _updatesite;
document.observe("dom:loaded",function(){
	if(isLogued){
		_updatesite = new UpdateSite();
		idInterval = setInterval("_updatesite.updateCompte()",20000);
	}
});

function getList(type){
	_updatesite.setType(type);
	_updatesite.updateCompte();
}

var UpdateSite = Class.create({

	initialize: function(){
		this.newMess = 0;
		this.newVisite = 0;
		this.newFlash = 0;
		this.updated = false;
		this.invit = null;
		this.type = 0;
		this.bottom = false;
		this.list = new Object();
	},
	setType:function(type){
		this.type = type;
		this.bottom = true;
		$('listOnLine').update();
	},
	updateCompte:function(){
		var _self = this;
		new Ajax.Request("/actions,updateCompte.php?t="+this.type,{
			method:'post',
			parameters:'t='+this.type,
			onSuccess:function(xhr){
				_self.parseResult(xhr);
			}
		});
	},
	parseResult:function(xhr){
		var jsonObj = xhr.responseJSON;
		if(jsonObj.list)
			this.updateList(jsonObj.list,jsonObj.count,jsonObj.admin);
		if(jsonObj.newmess && this.newMess != jsonObj.newmess)
			this.updateMessage(jsonObj.newmess);
		if(jsonObj.newvisite && this.newVisite != jsonObj.newvisite)
			this.updateVisite(jsonObj.newvisite);
		if(jsonObj.newflash && this.newFlash != jsonObj.newflash)
			this.updateFlash(jsonObj.newflash);
		if(jsonObj.invit)
			this.updateInvit(jsonObj.invit);
		if(this.updated && $('tbord'))
			new Effect.Pulsate($('tbord'));
		this.updated = false;
	},
	updateList:function(list,count,admin){
	
		var listContener = $('listOnLine');
		if(!listContener)
			return;
		if($('countOnLine')){
				if(this.type == 0)
					$('countOnLine').update(count+" connect\351(s)");
				else if(this.type == 1)
					$('countOnLine').update(count+" homme(s) connect\351(s)");
				else if(this.type == 2)
					$('countOnLine').update(count+" femme(s) connect\351(s)");
				else if(this.type == 3)
					$('countOnLine').update(count+" flash(s) connect\351(s)");
		}
	 	this.listTmp = new Hash();	
		for(a=0;a<list.length;a++){
			this.listTmp.set("userId_"+list[a].id,true);
			if($('userId_'+list[a].id))
				continue;

			var p = new Element('p',{'class':'userList','id':'userId_'+list[a].id});
			var sp = new Element('a',{'class':'sexe_'+list[a].sexe,'href':'/profil.php?id='+list[a].id}).update(list[a].login);
			p.insert(sp);
			if(admin[list[a].id])
				sp.addClassName("sadmin");
			var img = new Element('img',{'idUser':list[a].id,'sexe':list[a].sexe,'login':list[a].login,'src':'/images/icones/chatW_'+list[a].sexe+'.gif','alt':'Chatter','title':'Chatter'});
			if(list[a].onchat == 1)
				img.observe("click",function(){
					openDial(this.getAttribute('idUser'),this.getAttribute('login'),this.getAttribute('sexe'));
				});
			else
				img.observe("click",function(){
					addInvitChat(this.getAttribute('idUser'));
				});
			p.insert(img);
			if(!this.bottom)
				Element.insert(listContener,{'top':p});
			else
				Element.insert(listContener,{'bottom':p})
		}
		this.bottom = false;
		var _self = this;
		$$('.userList').each(function(n){
			var id = n.getAttribute('id');
			if(!_self.listTmp.get(id))
				n.remove();

		});
	},
	updateMessage:function(nb){
		this.newMess = nb;
		var c = $('new_mess');
		if(c){
			c.update(nb);
			this.updated = true;
		}
	},
	updateVisite:function(nb){
		this.newVisite = nb;
		var c = $('new_visite');
		if(c){
			c.update(nb);
			this.updated = true;
		}
	},
	updateFlash:function(nb){
		this.newFlash = nb;
		var c = $('new_flash');
		if(c){
			c.update(nb);
			this.updated = true;
		}
	},
	updateInvit:function(invits){
		for(a=0;a<invits.length;a++){
			if($('invit_'+invits[a].id))
				continue;
			
			var div = new Element('div',{'id':'invit_'+invits[a].id,'class':'invitBox'});
			var p = new Element('p');
			var spLogin = new Element('span',{'class':'sx'+invits[a].sexe}).update(invits[a].login);
			p.insert(spLogin);
			p.insert(" vous invit sur le chat !");
			div.update(p);
			var span = new Element('span');
			span.setStyle("display:block");
			var acc = new Element("a",{'href':'javascript://','idfrom':invits[a].id}).update("Accepter");
			acc.observe("click",function(){
				var idFrom = this.getAttribute('idfrom');
				acceptInvitChat(idFrom);
				$('invit_'+idFrom).remove();
			});
			acc.setStyle("color:#46B046;font-weight:bold;");
			var ref = new Element("a",{'href':'javascript://','idfrom':invits[a].id}).update("Refuser");
			ref.observe("click",function(){
				var idFrom = this.getAttribute('idfrom');
				refInvitChat(idFrom);
				$('invit_'+idFrom).remove();
			});
			ref.setStyle("color:red;font-weight:bold;");
			span.insert(acc);
			span.insert(" / ");
			span.insert(ref);
			div.insert(span);

			div.hide();
			Element.insert($('tbord'),{"after":div});
			new Effect.Appear('invit_'+invits[a].id);
		}	
	}
});

function acceptInvitChat(id){
	if(!isAbo){
		refInvitChat(id);
		location.replace("/abonnement.php");
	}else
		parent.frames['chat'].openChat();
}

function refInvitChat(id){
	new Ajax.Request("/actions,delInvitation.php",{
		method:'post',
		parameters:'idfrom='+id
	});
}
