// MouseTrack v2.
// The Tracking Scripts
// 2006 Will Wei
// REQUIRES:
//
// track.common.js
// - The "common" class that contains all functions used by both tracking features (mouse and element)
// track.elements.js
// - Responsible for tracking elements ONLY
// track.mouse.js
// - Responsible for tracking the mouse ONLY
// track.timer.js
// - A class that keeps the time. A timer. Simple
//
//
// ALSO REQUIRES:
// prototype.js
// - the prototype javascript library
// logger.js
// - debugging

ClientClass = Class.create();
ClientClass.prototype = {
	
	// Construrctor
	initialize : function () {
		// Change this to accommodate different gateways
		this.url = "http://ctxt.media.mit.edu/mtrack/";
		
		// "Static" Classes
		// - Since javascript has no native "static" support, create these two objects that will represent static classes
		// in the rest of the script
		Timer = new TimerClass();
		Connection = new ConnectionClass();
		
		// Objects
		Mouse = new TrackMouse();
		Elements = new TrackElements();
		
		// Gets the session numbers 
		// Takes an unlimited amount of arguments, but the arguments must
		// be objects
		Connection.sessionGet(Mouse,Elements);
		
	},
	
	run : function () {
		
		// Starts the tracking, as well as uploading of the data
		window.setInterval('Timer.increment()',Timer.interval);
		window.setInterval('Elements.upload()',Elements.burst);
		
		document.onload = this.loaded.apply(this);
		document.onunload = this.unloaded.apply(this);
		
		// Start tracking the mouse
		document.onmousemove = Mouse.track.bind(Mouse);
	},
	
	// Gatheres all of the links and passes individual links to fix to
	// the fixLink function
	fixLinks : function () {
		// Gets the list of all links in the page
		//linkList = document.getElementsByTagName("a");
		
		// For each link, modify the link to go through the gateway
		//for(i=0;i<linkList.length;i++)
		//	if(linkList[i].getAttribute("href") != null)
		//		this.fixLink(linkList[i]);

		// Done
	}, 
	
	// Fixes individual links
	fixLink : function(curLink)	{
		// Get the old url
		url = curLink.getAttribute("href");
		root = ""; // Create an empty root
		
		// If there is no http in the link location, get the location root from the window location
		if(url.indexOf("http")<0)
		{
			// Gets the http location in the url GET array
			root = Connection.get("url").substring(0,Connection.get("url").indexOf("/",7) > 0 ? Connection.get("url").indexOf("/",7) : Connection.get("url").length );
			
			// Now trims off everything past the *Q*
			root = root.indexOf("*Q*") > 0 ? root.substring(0,root.indexOf("*Q*")) : root;
			
			// Decode any special characters that appear in the root part of the hyperlink
			root = this.decodeUrl(root);
		}
		
		url = this.encodeUrl(url);
		
		if(url.indexOf("*Q*mod*E*experiment_client") > 0)
			return false;
			
		// Update the link
		curLink.setAttribute("href",this.url + "?mod=track&url=" + root + url);
	},
	
	// Fixes the forms
	fixForms : function() {
		linkList = document.getElementsByTagName("form");
		
		postFix = document.createElement("input");
		postFix.setAttribute("name","mousetrackpost");
		postFix.setAttribute("value","true");
		postFix.setAttribute("type","hidden");
		
		// Fix each form on the page
		for(i=0;i<linkList.length;i++)
		{
			linkList[i].appendChild(postFix); // Add the posting indicator
			
		
			// If the form is not a post form
			if(linkList[i].getAttribute("method") == null || linkList[i].getAttribute("method").toUpperCase() != "POST")
			{
				// Change it to a post form
				linkList[i].setAttribute("method","POST")
				// Mark the change in a new input
				
				postFix2 = document.createElement("input");
				postFix2.setAttribute("name","mousemodified");
				postFix2.setAttribute("value","true");
				postFix2.setAttribute("type","hidden");
				
				linkList[i].appendChild(postFix2);
			}
		}
	},
	
	// Functions to be called when the document loads
	loaded : function() {
		
		Elements.run();
		this.fixLinks();
		this.fixForms();
	
	},
	
	// Functions to be called when the user leaves the document
	unloaded : function() {
		
		Elements.upload.bind(Elements);
		Mouse.upload.bind(Mouse);
		
	},
	

	// Decodes an url
	decodeUrl : function(url) {
		
		// Replace the special characters
		url = url.replace(/\*A\*/g,"&");
		url = url.replace(/\*E\*/g,"=");
		url = url.replace(/\*D\*/g,"-");
		url = url.replace(/\*Q\*/g,"?");
		url = url.replace(/\*P\*/g,"+");
		
		return url;
	},
	
	// Encodes the url
	encodeUrl : function(url) {
				
		// Replace the special characters
		url = url.replace(/&/g,"*A*");
		url = url.replace(/=/g,"*E*");
		url = url.replace(/-/g,"*D*");
		url = url.replace(/\?/g,"*Q*");
		url = url.replace(/\+/g,"*P*");
		
		
		return url;
	}
};	

Client = new ClientClass();
window.onload = function () { Client.run(); };

