/*****************************************
			   Auteur :	
	  Gael du Plessix - Neweb Design
			Copyright 2008
	Tous droits réservés à Neweb Design
*****************************************/

var User = new Class.create({
	
	initialize: function()
		{
		this.id = false;
		this.login = '';
		this.mail = '';
		this.avatar = '';
		this.groupe = '';
		this.groupeId = 0;
		this.lastConnect = 0;			//Timestamp lors de la connexion
		this.sessionLastUpdate = 0;		//Dernier chargement de page (vaut time() )
		this.regles = 0;
		this.infos = [];
		this.config = [];
		this.invite = true;
		},
		
	connect: function(login,mdp,cookie,callBack)
		{
		var registerCookies = (cookie) ? '1' : '0';
		
		new Ajax.Request('ajax/user.php',{
				method: 'post',
				requestHeaders: {Accept: 'application/json'},
				parameters: {action:'connect', login:login, mdp:mdp, cookie:registerCookies},
				onSuccess: function(e)
					{
					var json = e.responseText.evalJSON(true);
					this.invite = (json.stat == '1') ? false : true;
					
					this.id = parseInt(json.id);
					this.login = json.login;
					this.mail = json.mail;
					this.avatar = json.avatar;
					this.groupe = json.groupe;
					this.groupeId = parseInt(json.groupeId);
					this.lastConnect = parseInt(json.lastConnect);
					this.sessionLastUpdate = parseInt(json.sessionLastUpdate);
					this.regles = parseInt(json.regles);
					this.infos = json.infos;
					this.config = json.config;
					
					var succes = !this.invite;
					if(callBack)
						callBack(succes);
					}.bind(this),
				onFailure: function(e)
					{
					var succes = !this.invite;
					if(callBack)
						callBack(succes);
					}.bind(this)
			});
		},
		
	deconnect: function(callBack)
		{
		new Ajax.Request('ajax/user.php',{
				method: 'post',
				requestHeaders: {Accept: 'application/json'},
				parameters: {action:'deconnect'},
				onSuccess: function(e)
					{
					var json = e.responseText.evalJSON(true);
					var succes = false;
					if(json.succes == "1")
						succes = true;
						
					if(callBack)
						callBack(succes);
					}.bind(this),
				onFailure: function(e)
					{
					if(callBack)
						callBack(false);
					}.bind(this)
			});
		},
		
	droit: function(droit)
		{
		return (droit & this.regles);
		}
});

var user = new User();
