// MouseTrack v2.
// Mouse Tracking Class
// 2006 Will Wei
TrackMouse = Class.create();

TrackMouse.prototype = {
	
	// Constructor
	initialize : function() {
		
		// Basic mouse tracking variables
		this.lastX 			= 0;
		this.lastY 			= 0;
		this.curX 			= 0;
		this.curY 			= 0; 
		this.moveRadius		= 25; // In pixels
		this.moveTime		= .5; // In seconds
		this.interval		= 0;
		this.activity		= 0;
		
		// Set up the timer to work with the mouse tracking
		this.timerName 		= "MouseUpdate";
		Timer.elapsedUpdate(this.timerName);
		
		// Stores coordinate information
		this.coordinates	= new Array();
		
		// Gets the session number for the website
		// Creates the variable this.session
		this.session 	= 0;
		
		// Uploader information
		this.url			= "index.php";
		this.burst			= 5;
	},
	
	// getCoordinates : function
	// Gets the coordinates of the mouse pointer
	getCoordinates : function(e) {
		if(navigator.appName=="Netscape")
		{
			this.curX = e.pageX + document.documentElement.scrollLeft;
			this.curY = e.pageY + document.documentElement.scrollTop;
      
		//this.curX = //Event.pointerX(e);
		//this.curY = //Event.pointerY(e);
		}
		else
		{
			this.curX = event.clientX + document.body.scrollLeft;
			this.curY = event.clientY + document.body.scrollTop;
		}
	},
	
	// update: function
	// Sets the "last mouse position" equal to the current position
	// Updates the timer
	update : function(e) {
		// Update the mouse position
		//this.lastX = Event.pointerX(e);
		//this.lastY = Event.pointerY(e);
		
		
		if(navigator.appName=="Netscape")
		{
			this.lastX = e.pageX + document.documentElement.scrollLeft;
			this.lastY = e.pageY + document.documentElement.scrollTop;
      
		//this.curX = //Event.pointerX(e);
		//this.curY = //Event.pointerY(e);
		}
		else
		{
			this.lastX = event.clientX + document.body.scrollLeft;
			this.lastY = event.clientY + document.body.scrollTop;
		}
		
		// Update the timer
		Timer.elapsedUpdate(this.timerName);
	},
	
	// shouldUpdate : function
	// Determines whether or not to update the mouse coordinates
	// returns: true if should update
	shouldUpdate : function(e) {
		// Increase the activity measure regardless of whether or not updating should happen
		this.activity += 1;
		
		// If the distance between current coords and new coords is large enough, update mouse coordinates		
		if(Math.sqrt((this.curX-this.lastX)*(this.curX-this.lastX)+(this.curY-this.lastY)*(this.curY-this.lastY))>this.moveRadius)
			return true;
		// Otherwise, if the amount of time elapsed has been enough, update mouse coordinates
		// Time based updating isn't used yet
		else if(Timer.elapsed(this.timerName)>this.moveTime)
			return true;

		else
			return false;
	},
	
	// record : function
	// Adds the data onto the stack of coordinates
	record : function() {
		// Add the new coordinates onto the array
		this.coordinates.push(new Array(this.curX,this.curY,Timer.elapsed(this.timerName),this.activity));
	},
	
	// clear : function
	// Resets the data stack
	clear : function() {
		this.coordinates 	= new Array();
		this.activity 		= 0;
	},
	
	// track : function
	// Will get called for every mouse motion or every so many seconds
	// 1. gets new coordinates
	// 2. determines whether to update the new points based on criteria of shouldUpdate
	// 3. uploads if it is appropriate
	track : function(e) {
		// Get the new coordinates
		this.getCoordinates(e);
		
		// If the new coordinates qualifies for a new update, record the new points and update the coordinates

		if(this.shouldUpdate(e))
		{
			this.record();
			this.update(e);
			
			return true;
		}
		
		if(this.shouldUpload())
			this.upload();
			
	return false;
	},
	
	// upload : function
	// Uploads mouse data
	// Clears the old data
	upload : function() {
		
		Connection.upload("mouse",this.coordinates,this.interval++,false,false);
		
		/*
		// The proper format for the prototype Ajax.Request function
		params = {
			method		: 'get', 
			parameters	: "mod=server&operation=upload&datatype=mouse&coords="+this.coordinates+"&session="+this.session+"&url="+TrackCommon.get("url")+"&interval="+this.interval++,
			onComplete	: this.uploadResponse.bind(this)
		};
		
		// Do the request
		ajaxRequest = new Ajax.Request(this.url,params);
		
		Logger.log(params.parameters);
		
		// Clear the old coordinates
		*/
		this.clear();
	},
	
	// uploadResponse : function
	// Nothing
	uploadResponse : function(response) {
	// Should have no need for a response, since this just uploads data
	// Could log what was uploaded by looking at the response text.
	},
	
	// shouldUpload : function
	shouldUpload : function() {
		if(this.coordinates.length>this.burst)
			return true;
	}	
}
