SiteClass = Class.create();
SiteClass.prototype = {
	
	// Construrctor
	initialize : function () {
		
		this._validElements 	= new Array("A","DIV","IMG","P");
		this._forbiddenElementIds = new Array("ls");
		this._elementNumber 	= 0;
		
		// Change this to accommodate different gateways hock says good, was ctxt.media.mit.edu/mtrack/	should be whatever the root mousetrack/mtrack folder is
		this.url = "";
		this._mod = "track";
	},
	
	
	GetMod : function () {
		return this._mod;
	},
	
	SetMod : function(mod) {
		this._mod = mod;
	},
	
	// 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)
				linkList[i].setAttribute("href",this._FixLink(linkList[i].getAttribute("href")));
		
		// Done
	}, 
	
	// Fixes individual links
	_FixLink : function(url)	{
		
		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);
		}
		
		// Make sure there's at least one slash between the root and the url
		if( !(root.substring(root.length-1,1) == "/" && url.charAt(url.length-1) != "/" || root.substring(root.length-1,1) != "/" && url.charAt(url.length-1) == "/" ))
			root += "/";			  
		
		url = this.encodeUrl(url);
		
		
		
		newURL = root + url;
		newURL = newURL.substring(newURL.indexOf("http"));
		
		// Return the fixed link
		return this.url + "?mod="+this._mod+"&url=" + newURL;
	},
	
	FixForms : function () {
		
		// Get the forms
		forms = document.getElementsByTagName("form");
		
		// Fix 1
		// Purpose: ensures that, when the form is submitted, all of the POST information
		// stored in the forms will first be submitted to the remote site, and then the results
		// of the POSTed page will be received and viewed
		fix1 = document.createElement("input");
		fix1.setAttribute("name","mousetrackpost");
		fix1.setAttribute("value","true");
		fix1.setAttribute("type","hidden");
		
		
		// Fix 2
		// Purpose: Not all forms are POST forms, and GET forms appear to cause problems for mtrack
		// Fix2 marks the presence of a GET->POST modification, letting mousetrack manually add
		// all of the form variables a newly constructed URL
		fix2 = document.createElement("input");
		fix2.setAttribute("name","mousemodified");
		fix2.setAttribute("value","true");
		fix2.setAttribute("type","hidden");
		
		// Cycle through the forms
		for(i=0;i<forms.length;i++)
		{
			// Apply fix 1
			forms[i].appendChild(fix1);
			
			// Fix the form's "action" line
			forms[i].setAttribute("action",this._FixLink(forms[i].getAttribute("action")));
			
			// If it is a GET based form, change it to a post form and apply Fix2
			if(linkList[i].getAttribute("method") == null || linkList[i].getAttribute("method").toUpperCase() != "POST")
				forms[i].setAttribute("method","POST");
		}
	},
	
	// Old fixForms
	// 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);
			}
		}
	},

	// 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;
	},
	
	GetPageHeight : function() {
		// Tested in both IE6 and Firefox 1.5.0.4
		return document.body.scrollHeight;
	},
	
	GetPageWidth : function() {
		// Tested in both IE6 and Firefox 1.5.0.4
		return document.body.scrollWidth;
	},
	
	//-------------------------------------------------
	// Element Related Functions
	//-------------------------------------------------
	
	GetElementsInfo : function() {
		
		
		// Create the document walker to walk through all of the nodes
		DOM = document.createTreeWalker(document, true, function() { return true; } , false);
		//NodeFilter.SHOW_ELEMENT
		//NodeFilter.FILTER_ACCEPT
		elementsString = "";
		
		// While there still are nodes
		while(DOM.nextNode())
			if(this.IsValidElement(DOM.currentNode))

			elementsString += this.GetSingleElement(DOM.currentNode) + "|";
		
		elementsString = elementsString.substring(0,elementsString.length-1); // Chop off the last "|"
		
		/*
		elementsString = "";
		
		for(current=0;current<this.trackable.length;current++)
		{
			curDOM = document.getElementsByTagName(this.trackable[current]);
			for(nodeNum=0;nodeNum<curDOM.length;nodeNum++)
				if(this.IsValidElement(curDOM[nodeNum]))
					elementsString += this.GetSingleElement(curDOM) + "|";
		}
		
		elementsString = elementsString.substring(0,elementsString.length-1); // Chop off the last "|"
		*/
		
		return elementsString;
	},
	
	GetSingleElement : function (node) {
		
		// If it has no id, give it an id
		if(node.getAttribute("id") == null)
			node.setAttribute("id",node.tagName + this._elementNumber++)
		
		elementString = new Array(node.getAttribute("id")); // ID
		elementString.push(Position.cumulativeOffset(node)[0]); // Left
		elementString.push(Position.cumulativeOffset(node)[1]); // Top
		elementString.push(Element.getDimensions(node).width); // Width
		elementString.push(Element.getDimensions(node).height); // Height
		
		return elementString;
	},
	
	IsValidElement : function(node) {
		
		validElement = false;
		forbiddenElement = false;
		
		for(i=0;i<this._validElements.length;i++)
			if(node.tagName.toUpperCase() == this._validElements[i].toUpperCase())
			{
				validElement = true;
				break;
			}
		
		for(i=0;i<this._forbiddenElementIds.length;i++)
			if(node.id != null && node.id.toUpperCase() == this._forbiddenElementIds[i].toUpperCase())
			{
				forbiddenElement = true;
				break;
			}
			
		return !forbiddenElement&&validElement;
	}
	
	// End Elements Related Functions
};