// MouseTrack v2.
// Element tracker
// 2006 Will Wei

TrackElements = Class.create();

TrackElements.prototype = {
	
	// Constructor
	initialize : function () {
		
		// Stores a list of all of the elements to be tracked
		// Could be useful or not, depending on how things work out
		this.list 			= new Array();
		this.trackable		= new Array("a","div","img","p"); 
		this.elementNum 	= 1;
		this.interval		= 1;
		this.session 		= 0;
		
		// Time data stored here
		this.data 	= new Array();
		
		// Upload configuration
		// How much time should pass before an upload in milliseconds
		this.burst 	= 200;
		this.url 	= "index.php";
	},
	
	// setup : function
	// Sets up a specific element to call certain functions in this class
	// On mouse overs and on mouse outs
	// If the element doesn't already have an ID, it gets an id assigned to it
	setup : function (node) {

		// If the node satisfies the criteria
		//if(!this.shouldTrack(node))
		//	return false;
		
		
		// If it has no id, give it an id
		if(node.id == "" || node.id == null)
			node.id = node.tagName + this.elementNum++;
//		else
//			node.id = node.tagName + "|" + node.id;
		
		// Add it onto the list of trackable elements
		this.list.push(node.id);
		
		// Now setup the mouseover and mouseout events for the specific element
		//Event.observe(id,"mouseover",this.over.bind(this),false);
		//Event.observe(id,"mouseout",this.out.bind(this),false);
		if(navigator.appName=="Netscape")
		{
			node.addEventListener("mouseover",this.over.bind(this),true);
			node.addEventListener("mouseout",this.out.bind(this),true);
		}
		else
		{
			node.attachEvent("onmouseover",this.over.bind(this));
			node.attachEvent("onmouseout",this.out.bind(this));	
		}
		
		return true;
	},
	
	// over : function
	// Updates the elapsed time
	over : function (e) {
		
		id = "";
		if(navigator.appName=="Netscape")
			id = e.target.id;
		else
			id = event.srcElement.id;
			
		if(id == "")
			return false;
		// Add a task to the timer to start timing
			Timer.elapsedUpdate(id);
			
		
	},
	
	// out : function
	// Saves the elapsed time
	// Removes time from timer
	out : function (e) {
		
		target = "";
		if(navigator.appName=="Netscape")
			target = e.target;
		else
			target = event.srcElement;
		
		id = "";
		if(navigator.appName=="Netscape")
			id = e.target.id;
		else
			id = event.srcElement.id;
		
		if(id == "" || Timer.elapsed(id) == 0)
			return false;
			
		// Add the data to the stack
		this.data.push(target.tagName+"|"+id+":"+Timer.elapsed(id));
		
		// Reset the elasped time in the timer
		Timer.remove(id);
	},
	
	// run : function
	// Sets up all of the trackable elements shown in the this.trackable array
	// also sets the timer to upload the new data every so often
	run : function () {
		
		/*
		// Only firefox compatible
		// Create the document walker
		DOM = document.createTreeWalker(document, true, function() { return NodeFilter.FILTER_ACCEPT; } , false);
		
		// NodeFilter.SHOW_ELEMENT
		// While there still are nodes
		while(DOM.nextNode())
			this.setup(DOM.currentNode);
			
		*/
		
		// New way
		for(current=0;current<this.trackable.length;current++)
		{
			curDOM = document.getElementsByTagName(this.trackable[current]);
			for(nodeNum=0;nodeNum<curDOM.length;nodeNum++)
				this.setup(curDOM[nodeNum]);
			
		}		
	},
	
	// shouldTrack : function
	// Gets passed a document node, and based on a certain amount of criteria
	// Such as id or whatnot, will determine whether or not to track the node
	shouldTrack: function (node) {

		// Is the tag appropriate (filters out body/head/script tags)
		for(i=0;i<this.trackable.length;i++)
		{
			if(node.tagName.toUpperCase() == this.trackable[i])
			{
				// Add other conditions here
				return true;
			}
		}
		// otherwise it shouldn't be tracked
		return false;
	},
	
	// Upload : function
	// Uploads the Data
	upload : function () {

		// First ensure that there should be uploading happening
		if(!this.shouldUpload())
			return false;

		Connection.upload("element",this.data,this.interval++,false,false);
		
		/*
		params = {
			method		: 'get', 
			parameters	: "mod=server&operation=upload&datatype=element&data="+this.data+"&session="+this.session+"&url="+TrackCommon.get("url")+"&interval="+this.interval++,//new Array(this.session,this.coordinates), 
			onComplete	: this.uploadResponse.bind(this)
		};
		
				Logger.log(params.parameters);
		
		ajaxRequest = new Ajax.Request(this.url,params);
		
		*/
		this.clear();
	},
	
	// UploadReponse : function
	// Nothing
	uploadResponse : function (response) {
		// Don't really need to do anything, but might log the data received just to ensure, etc
	},
	
	// shouldUpload : function
	shouldUpload : function() {
		// If there actually is data to upload, upload it
		// Currently there aren't any other requirements
		// Perhaps you could filter data here, but nothing is required... yet
		if(this.data.length>0)
			return true;
	},
	
	// clear : function
	// Clears the data
	clear : function () {
		this.data = new Array();
	}
};

/*

TrackElementEventArgs = Class.create();

TrackElementEventArgs.prototype = {
	
	initialize : function (elementID) {
		this.elementID = elementID;
	},
};
*/
