// MouseTrack v2.
// Mouse Tracking Class
// 2008 Roger
TrackScroll = Class.create();

TrackScroll.prototype = {
	
	// Constructor
	initialize : function() {
		
		// Basic mouse tracking variables
		this.position 				= 0; //posicio actual
		this.last_position 			= 0; //posicio anterior

		this.interval		= 0;		
		this.activity		= 0;
		
		// Set up the timer to work with the mouse tracking
		this.timerName 		= "ScrollUpdate";
		Timer.elapsedUpdate(this.timerName);
		
		// Stores coordinate information
		this.Aposition	= new Array();	//on anirant les posicions
		
		// Gets the session number for the website
		// Creates the variable this.session
		this.session 	= 0;
		
		// Uploader information
		this.url			= "index.php";
		this.burst			= 50; //frequencia del timer
	},

	// getPosition : function
	// Gets the coordinates of the srcoll
	getPosition : function() {
		
		if (navigator.appName == "Microsoft Internet Explorer"){
			this.position = document.body.scrollTop;
		}
		else {
			this.position = window.pageYOffset;
		}
	},
	
	// update: function
	//una vegada enviat la poscio passa a ser, una posicio anterior
	update : function() {
		this.last_position=this.position;
		
		// Update the timer
		Timer.elapsedUpdate(this.timerName);
	},
	
	// shouldUpdate : function
	// Determines whether or not to update the scroll coordinates
	// returns: true if should update
	shouldUpdate : function(e) {
		// Increase the activity measure regardless of whether or not updating should happen
		this.activity += 1;
		
		//si son posicions diferents, llavors es q ha canviat
		if(this.position!=this.last_position)
		{
		//	alert(this.position);
		//	alert(this.last_position);
			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.Aposition.push(new Array(this.position,Timer.elapsed(this.timerName),this.activity));
	},
	
	// clear : function
	// Resets the data stack
	clear : function() {
		this.Aposition 	= 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 and uploads if it is appropriate
	track : function() {
		// Get the new coordinates
		this.getPosition();
		
		// If the new coordinates qualifies for a new update, record the new points and update the coordinates

		if(this.shouldUpdate())
		{
			this.record();
			this.upload();
			this.update();
			return true;
		}
			
			
	return false;
	},
	
	// upload : function
	// Uploads mouse data
	// Clears the old data
	upload : function() {
		
		Connection.upload("scroll",this.Aposition,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.
	}

}
