var track = {

	users: '',
					
	idle_time: 0,
	
	init: function(access)
	{
	
		this.access = access;
		if ($defined( $('online_friends')))
		{
		this.users = $('online_friends').value;
		} 
		
		this.update_idle_time.periodical(5000);
		
		

		$$('body')[0].addEvents({ 
		
			
			'click' : function(){
			//alert('yes');
				this.idle_time = 0;
			}.bind(this),
			'mousedown' : function(){
				this.idle_time = 0;
			}.bind(this),
			'mousemove': function()
			{
				
			
				this.idle_time = 0;
			}.bind(this),
			'beforeunload':function()
			{
				this.cancel();
			}.bind(this)
		
		});
		
		
		
		this.update_time();
	},
	update_idle_time:function()
	{
		this.idle_time = this.idle_time + 5;
	},
	
	go: function(access)
	{
	
		this.update_time();
	
	},
	update_time: function()
	{
		this.req = new Request.HTML({  
		method: 'get',  
		async: true,
		url: 'php/user.online.php',  
		data: { 'location': window.location.pathname + window.location.search, 'idle_time' : this.idle_time, 'access' : this.access, 'online_friends' : this.users},  
		onRequest: function() { 
			//this.idle_time = 0;
		}.bind(this),  
		onComplete: function(a,b,c,d) 
		{ 
			//log(c);
			var response = JSON.decode(c);
			//log(response);
			if ($defined(response))
			{
				if (response.go === "1")
				{
					this.update_time();
				}
				
			}
		}.bind(this),
		onFailure: function(){ },
		onCancel: function() {  }
		}).send();
	},
	
	cancel: function ()
	{
		this.req.cancel();
	}
}
	

Array.prototype.inArray = function (value)
{
	var i;
	for (i=0; i < this.length; i++) {
	// Matches identical (===), not just similar (==).
	if (this[i] === value) {
		return true;
		}
	}
	return false;
};

var trackChat = {


	init : function (qid, time, size, cid) {
	
		window.addEvents({ 'beforeunload': function() {  
		
		var r = new Request({method: 'get', url: 'leave.php'});
		r.send('qid='+qid);
		//alert('yes');

		
		this.cancel();


		}.bind(this) });
	
		this.of = $('online_friends').value.split(',');
		this.cid  = cid;
		this.time = time;
		this.size = size;
		this.qid  = qid;
		
		this.update();
	
	},
	
	go : function () {
	
		this.update()
	
	},
	
	update: function() {
	
		this.req = new Request.HTML({ method: 'get', async: true, url: 'php/track.discussion.php',  
		
			data: { 'qid' : this.qid, 'time' : this.time, 'size' : this.size, 'cid' : this.cid },   
			onComplete: function(a,b,c,d) 
			{ 
				var response = JSON.decode(c);
				//console.log(response);
				
				if ($defined(response)) 
				{				
					
					if ($defined(response.u))
					{
					//	$$('.v0-on-user').set({'class' : 'v0-off-user'});
						
						var new_of = [];
						
						var i=0;
						for(id in response.u)
						{
							$('ustatus'+id).set('html', response.u[id][1]);
							
							var new_location = response.u[id][2];
							var old_location = $('ulocation'+id).get('value');
							var js_location = window.location.pathname + window.location.search;
							//log(old_location+' > '+ new_location+' > '+js_location);
							
							if (old_location != new_location)
							{
								var status = new_location == js_location ? 'Viewing this discussion' : 'Browsing other study groups';
			
								$('ulocation-status'+id).set('html', status);
								
								this.highlight(id);
								
								
							}
							$('ulocation'+id).set('value', new_location);
							
							if ((!this.of.inArray(id) && response.u[id][0] == 1)){
							
								var div = new Element('div', { 'class' : 'v0-info-update' }).set('html', 
								'<a href="'+$('uname'+id).get('href') +'">' + $('uname'+id).get('html') + '</a> has joined the discussion');
								
								div.inject($('dcontent'), 'top'); 
								
							} 
							
							
							if (response.u[id][0] == 1)
							{
								new_of[i] = id;
								$('u'+id).set({'class': 'v0-on-user'});
								i++;
							} else {
								$('ulocation-status'+id).set('html','');
							}
						}
						
						this.of = new_of;
						
					}
					if ($defined(response.time)) { this.time = response.time; }
					if ($defined(response.size)) { this.size = response.size; }
					if ($defined(response.cid)) { this.cid = response.cid; }
					if ($defined(response.html)) { 
					
					$('r1').set( { 'html' : 'Status: Discussion has been updated', 'class' : 'success2' });
					reply.reset_return();
					
					
					var div = new Element('div').set('html', response.html);
					div.inject($('dcontent'), 'top'); 
					
					}
					if (response.go === "1") { this.update(); }
					
					
					
				}
				
			}.bind(this),
			
			onFailure: function() { },
			
			onCancel: function() { }
		
		}).send();
	
	},
	
	highlight: function(id) 
	{
		new Fx.Tween($('u'+id), 20).start('background-color', '#d3ff87', '#e4ffcf');
								
		setTimeout(function() { new Fx.Tween($('u'+id), 20).start('background-color', '#e4ffcf', '#ffffff'); }, 3000);
	
	},
	cancel: function ()
	{
		this.req.cancel();
	}

}	