//-------------- OfflineConnectionHandler  class --------------//
/**
 * Offline connection handler
 * Offers same functionalities than JabberConnectionHandler class
 * but without any connection. Useful for offline tests.
 */
var OfflineConnectionHandler = new Class({
	initialize: function(jid,server,pwd,method,register,httpbase) {
		this.jid = jid ;
		this.pwd = pwd ;
		this.server = server ;
		
		this.contacts = [
			{cn:'mat@ajaxbber.test',dn:'Mat'},
			{cn:'pedro@ajaxbber.test'},
			{cn:'plop@gmail.test'},
			{cn:'ben@hotmail.test',dn:'Bro'},
			{cn:'pouet@icq.ajaxbber.test'},
			{cn:'brown@ajaxbber.test/gtalk'}
		];
		this.groups = ['Friends','Family','Work'];
		this.msgs = ['hello','how are you?','would you like some tea?','I am just a testing tool','Lucy in the sky with diamonds!','Cowabunga !','I don\'t believe in Peter Pan,Frankenstein or Superman.'] ;
		this.status = [SET_STATUS_ACTIVE,SET_STATUS_AWAY,SET_STATUS_DND,SET_STATUS_INVISIBLE,SET_STATUS_OFFLINE,SET_STATUS_XA,SET_STATUS_OFFLINE,'unavailable','unsubscribe','unsubscribed','subscribe'] ;
		
		this.backgroundOps = function() {
			//Connection
			this.handleConnected();
			
			//Eventually receive messages and presences before group list
			var rand = random(3) ;
			for ( var i = 0 ; i < rand ; i++) {
				this.handleMessage() ;
			}
			
			//Receive group list
			this.receiveGroupList();
			
			//Usual activities
			this.simulation = function() {
				var rand = random(10);
				if ( rand < 8 ) {
					//incoming message (80%)
					this.handleMessage();
				} else {
					//incoming presence
					this.handlePresence();
				}
			}.bindAsEventListener(this);
			this.simulation.periodical(10000);//an action every 10 seconds
			
		}.bindAsEventListener(this);
		
		//3secs before connecting, to be almost real
		this.backgroundOps.delay(3000);
		
		this.getContact = function(contactName) {
			var c = null;
			var cs = this.contacts.filter(function(c) {
				return c.cn.indexOf(contactName) > -1 ;
			}) ;
			if ( cs.length > 0 ) {
				c = cs[0];
			}
			return c;
		}
	},
	
	getRandomContact: function() {
		return this.contacts[random(this.contacts.length)] ;
	},
	
	getRandomMsg: function() {
		var msg ;
		var idx = random(this.msgs.length+1) ;
		if ( idx < this.msgs.length) {
			msg=this.msgs[ idx ] ;
		}
		return msg;
	},
	
	getRandomStatus: function() {
		return this.status[ random(this.status.length) ] ;
	},
	
	handleMessage: function() {
		var contactName = this.getRandomContact().cn;
		var msg = this.getRandomMsg();
		
		//if user was offline, set him active
		var c = this.getContact(contactName);
		if ( c!=null && (c.status == SET_STATUS_OFFLINE || typeof(c.status) == 'undefined' )) {
			c.status=SET_STATUS_ACTIVE;
			this.fireEvent(CONTACT_CHANGES_STATUS,{contactName:contactName,status:SET_STATUS_ACTIVE}) ;
		}
		
		this.fireEvent(CONTACT_SEND_MSG,{contactName:contactName,msg:msg}) ;
	},
	
	handlePresence: function() {
		var contactName = this.getRandomContact().cn;
		var msg = this.getRandomMsg();
		var status = this.getRandomStatus();
		
		var c = this.getContact(contactName);
		if ( c!=null) {
			c.status=status;
		}
		
		if (status == 'unavailable'|| status=='error') {
			this.fireEvent(CONTACT_LOGOFF,contactName) ;
		} else if (status=='unsubscribed') {
			this.fireEvent(CONTACT_REFUSED_AUTH,{contactName:contactName,status:status,msg:msg});
		} else if (status=='unsubscribe') {
			this.fireEvent(CONTACT_REMOVED_AUTH,{contactName:contactName,status:status,msg:msg});
		} else if (status == 'subscribe') {
			this.fireEvent(CONTACT_ASKED_AUTH,{contactName:contactName,status:status,msg:msg}) ;
		} else {
			if ( contactName && contactName != '' && status != 'subscribed') {
				this.fireEvent(CONTACT_CHANGES_STATUS,{contactName:contactName,status:status,msg:msg}) ;
			}
		}
	},
	
	handleConnected: function() {
		this.fireEvent(CONNECTED) ;
	},
	
	receiveGroupList: function() {
		for ( var i = 0 ; i < this.contacts.length ; i++) {
			var c = this.contacts[i];
			var randCount = random(this.groups.length) ;
			
			if ( randCount > 0 ) {
				var randStart = random(this.groups.length) ;
				for ( var j = 0; j < randCount ; j++) {
					var groupName = this.groups[(randStart+j)%this.groups.length ];
					this.fireEvent(ADD_CONTACT_GROUP,{jid:c.cn,groupName:groupName,displayableName:c.dn});
				}
			} else {
				this.fireEvent(ADD_CONTACT_GROUP,{jid:c.cn,displayableName:c.dn});
			}
		}
		this.fireEvent(GROUP_CONTACTS_RECEIVED) ;
	},
	
	handleDisconnect: function() {
		this.fireEvent(DISCONNECTED);
	},
	
	disconnect: function() {
	},
	
	setStatus: function() {
	},
	
	modifyContact: function() {
	},
	
	send: function(contactName,msg) {
		this.fireEvent(CONTACT_SEND_MSG,{contactName:contactName,msg:'Did you say : '+msg+' ?'}) ;
	},
	
	queryAddContact: function() {
	}
	

});

OfflineConnectionHandler.implement(new Events);

//-------------- End of OfflineConnectionHandler  class --------------//
