/* XML Node Module (void) */
function xmlnode(){
	this.tagname = "";
	this.attributes = new Array();
	this.value = "";
	this.addAttribute = function(name, value){
		this.attributes[this.attributes.length] = new Array(name, value);
	}
	var tmp = new Date();
	this.time = tmp.getTime();
		
}

/* HTML XML Request Module (string, string, string, object) */
function ajax(page, getPost, sendVars, readyStateHandler){
	//Builld xml http request object
    var xmlHttpReq = false;
    if (window.XMLHttpRequest) 
       xmlHttpReq = new XMLHttpRequest();// Mozilla/Safari
    else if (window.ActiveXObject)// IE
       xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	//post request to fetchpage php file
    xmlHttpReq.open(getPost, page, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function (){ 
		if(xmlHttpReq.readyState == 4){//On load state
			try{
				readyStateHandler(xmlHttpReq.responseText);
			}catch(e){
			}
		}
	}
    xmlHttpReq.send(sendVars);
}


function RTUS(self){

	this.xmllog = new Array();
	this.loginterval = 1000; //one second 
	this.minloglength = 1; 
	this.datetime = new Date();	
	this.self = self;
	
	//dump xml log to screen
	this.dumpXmlLogToScreen = function(){
		alert("Log length:"+this.xmllog.length);
		alert("Min Log length:"+this.minloglength);
		alert(this.generateXmlDoc());
	}
	
	this.generateXmlDoc = function(){
//		var xmldoc = "<entry>";
		xmldoc = "<window";
		xmldoc += " width='"+window.innerWidth+"'";
		xmldoc += " height='"+window.innerHeight+"'";
		xmldoc += " location='"+window.location+"'";
		xmldoc += " time='"+this.datetime.getTime()+"'";
		xmldoc += " />";
		for(var i=0; i<this.xmllog.length; i++){
			var tmp = "<";
			tmp += this.xmllog[i].tagname;
			tmp += " time='"+this.xmllog[i].time+"'";
			for(var k=0; k<this.xmllog[i].attributes.length; k++){
				tmp += " "+this.xmllog[i].attributes[k][0];
				tmp += "='"+this.xmllog[i].attributes[k][1]+"'";
			}
			tmp += ">";
			tmp += this.xmllog[i].value;
			tmp += "</"+this.xmllog[i].tagname+">";
			xmldoc += tmp;
		}
//		xmldoc += "</entry>";
		return xmldoc;
	}		
				
	
	//add xml node to xml log
	this.addXmlNode = function(xmlnode){
		this.xmllog[this.xmllog.length] = xmlnode;
	}
	
	//record mouse position
	this.onmousemove = function(evt){
		var node = new xmlnode();
		node.tagname = "mousemove";
		node.addAttribute("pagex", evt.pageX);
		node.addAttribute("pagey", evt.pageY);	
		this.addXmlNode(node);
	}
	
	//record mouse down
	this.onmousedown = function(evt){
		var node = new xmlnode();
		node.tagname = "mousedown";
		node.addAttribute("targetid",evt.target.id);
		node.addAttribute("targetparentnodeid", evt.target.parentNode.id);
		this.addXmlNode(node);
	}
	
	//record mouse up
	this.onmouseup = function(evt){
		var node = new xmlnode();
		node.tagname = "mouseup";
		node.addAttribute("targetid",evt.target.id);
		node.addAttribute("targetparentnodeid", evt.target.parentNode.id);
		this.addXmlNode(node);
	}
	
	//record on focus
	this.onfocus = function(evt){
		if(evt.target.id){
			var node = new xmlnode();		
			node.tagname = "focus";
			node.addAttribute("pagex", evt.pageX);
			node.addAttribute("pagey", evt.pageY);			
			node.addAttribute("targetid",evt.target.id);
			node.addAttribute("targetparentnodeid", evt.target.parentNode.id);
			this.addXmlNode(node);
		}
	}
	
	//record on key down
	this.onkeydown = function(evt){
		var node = new xmlnode();		
		node.tagname = "keydown";
		node.addAttribute("pagex", evt.pageX);
		node.addAttribute("pagey", evt.pageY);
		node.addAttribute("targetid",evt.target.id);
		node.addAttribute("targetparentnodeid", evt.target.parentNode.id);
		node.addAttribute("which", evt.which);
		node.addAttribute("modifiers", evt.modifiers);
		this.addXmlNode(node);
	}	
	
	//tmp
	this.onclick = function(evt){
//		this.dumpXmlLogToScreen();
	}
	

	//send log to server every second
	this.sendLog = function(){		
		//check to see if log needs to be sent
		if(this.xmllog.length>=this.minloglength){
			var tmp = this.generateXmlDoc();
			tmp = "log="+tmp;
			ajax("/rtus/rtus.php", "POST", tmp, function(txt){ });
			this.xmllog = new Array();
		}
		window.setTimeout(this.self+".sendLog()", this.loginterval);		
	}	
	
	this.sendLog();

}
RTUS.prototype = new Observer;
//-----------------------------------------------------------------------
function cursorFun(){
	this.onmousemove = function(evt){
		this.cursor = document.getElementById('cursorimage');
		this.cursor.style.left = evt.pageX+"px";
		this.cursor.style.top = evt.pageY+"px";
	}
}	
cursorFun.prototype = new Observer;
//-----------------------------------------------------------------------


	var webpageSubject = new Subject(document.body, 'webpageSubject');
	//create instance of rtus
	var observer1 = new RTUS('observer1');
	webpageSubject.registerObserver(observer1);
	//var observer2 = new cursorFun();
	//webpageSubject.registerObserver(observer2);

