// Manages all asynchronous calls in this mousetracking script
// MTrack .v2 2006

// ConnectionClass
ConnectionClass	= Class.create();
ConnectionClass.prototype = {
	
	// Constructor
	// Only needs to know the location of the server
	initialize : function () {
		
		this.ajaxurl 	= globalurl + "/wp-content/plugins/recycled-data/mousetrack/index.php";
		this.session	= 0;
		
		// Associated objects, objects to modify later
		this.associated = new Array();
		
	},
	
	// Uploads data
	// Parameters :
	// type (string) - name of the datatype being uploaded
	// data (string) or (array) - data to upload
	// interval (int) - the interval of data uploaded
	// extra (string) - extra parameters (optional)
	// handler (function) optional function handler to call upon upload
	upload : function (type, data, interval, extra, handler) {
		// Create the right parameters to upload
		params = {
			method		: 'get', 
			parameters	: "mod=server&action=upload&datatype="+type+"&data="+data+"&session="+this.session+"&url="+globalurl+"&suburl="+suburl+"&interval="+interval+"&condition="+experimentcode+ (extra ? extra : ""),
			onComplete	: (handler ? handler : this.uploadDefaultHandler.bind(this))
		};
		
		Logger.log(params.parameters);
		
		// Do the request
		ajaxRequest = new Ajax.Request(this.ajaxurl,params);

	},
	
	// Default Response to uploads
	// Parameters: receives the server response, but since it's an upload, unlikely to contain
	// anything
	uploadDefaultHandler : function (response)	{
		// Nothing
	},
	
	// Downloads data
	// Parameters:
	// type (string) the type of data to be downloaded
	// extra (string) extra parameters (optional)
	// handler (function) function to call upon success
	download : function (type, extra, handler) {
		
		// Set the correct parameters for the ajax requester
		params = { 
		method 		: "get",
		parameters 	: "mod=server&action=download&datatype="+type+ (extra ? extra : ""),
		onComplete  : (handler ? handler : this.downloadDefaultHandler.bind(this))
		};
		
		Logger.log(params.parameters);
		
		ajaxRequest = new Ajax.Request(this.ajaxurl,params);
	},
	
	// Download response
	// Generic response, empty
	downloadDefaultHandler : function (response){
	},
	
	/*
	// Executes a specific command (i.e. disconnect users, connect user
	command : function (command, extra, handler) {
		
		// Set the correct parameters for the ajax requester
		params = { 
		method 		: "get",
		parameters 	: "mod=server&action=command&command="+command+ (extra ? extra : ""),
		onComplete  : (handler ? handler : this.commandDefaultHandler.bind(this))
		};
		
		Logger.log(params.parameters);		
	
		ajaxRequest = new Ajax.Request(this.ajaxurl,params);
	},
	*/
	command : function (type, extra, handler) {
		
		// Set the correct parameters for the ajax requester
		params = { 
		method 		: "get",
		parameters 	: "mod=server&action=command&command="+type+ (extra ? extra : ""),
		onComplete  : (handler ? handler : this.commandDefaultHandler.bind(this))
		};
		
		Logger.log(this.ajaxurl + "?" + params.parameters);
		
		ajaxRequest = new Ajax.Request(this.ajaxurl,params);
	},
	
	commandDefaultHandler : function (response) {
		//Logger.log((response.responseText != "" && response.responseText != null) ? response.responseText : "No Response");
	},
	
	// sessionGet : function
	// Uses the this.download function to get it
	// Takes a parameterlist following the form:
	// Parameter 1 : variable name to change
	// Parameters 2+ : objects with variable name to change
	sessionGet : function() {
		
		// Save the certain certain objects in the arguments list to change later
		for(i=0;i<arguments.length;i++)
			this.associated.push(arguments[i]);
		
		this.download("session","&url="+this.get("url"),this.sessionRecord.bind(this));
	},
	
	sessionGetExperiment : function() {
		
		// Save the certain certain objects in the arguments list to change later
		for(i=0;i<arguments.length;i++)
			this.associated.push(arguments[i]);
		
		this.download("session_experiment","&url="+this.get("url"),this.sessionRecord.bind(this));
	},
	
	// sessionRecord : function
	sessionRecord : function (response) {
		
		// Uses eval command to save the response text into the variable name specified
		// in the first position of the arguments array. 
		for(i=0;i<this.associated.length;i++)
			this.associated[i].session = response.responseText;
		

		// Save into Connect's session for future use
		this.session = response.responseText;
	},
	
	// get : function
	// equivalent of the $_GET array in php. Will return the value of the GET field associated with
	// the name... or false if it doesn't exist
	get : function (name) {
		var defaultLocation = window.location.toString();
		
		// Check to make sure the field exists
		if(defaultLocation.indexOf(name)<0)
			return false;
		
		return defaultLocation.substring(defaultLocation.indexOf(name)+name.length+1, defaultLocation.indexOf('?',defaultLocation.indexOf(name))>0 ? defaultLocation.indexOf('?',defaultLocation.indexOf(name)) : defaultLocation.length);
	},
	
	
	// Decodes an url
	decodeUrl : function(url) {
		
		// Replace the special characters
		url = url.replace("*A*","&");
		url = url.replace("*E*","=");
		url = url.replace("*D*","-");
		url = url.replace("*Q*","?");
		url = url.replace("*P*","+");
		
		return url;
	},
	
	// Encodes the url
	encodeUrl : function(url) {
				
		// Replace the special characters
		url = url.replace("&","*A*");
		url = url.replace("=","*E*");
		url = url.replace("-","*D*");
		url = url.replace("?","*Q*");
		url = url.replace("+","*P*");
		
		return url;
	}
};