

// less important variables for code
var coordinates = new Array(); 
var fps = 10;
var posx = 0; 
var posy = 0;
var newPosx = 0; 
var newPosy = 0;
var timer;
var req_fifo;


// important variables for logging
var duration = 0; // duration from beginning in seconds
var clickCount = 0;  // count clicks on this page
var activity = 0; // holds the amount of mouse activity
var docurl = document.URL; //holds the current url


 

function getCoordinates(e){
	  posx = (window.Event) ? e.pageX : event.clientX + document.body.scrollLeft;
  	posy = (window.Event) ? e.pageY : event.clientY + document.body.scrollTop;
}



function click(){
	//grab existing click
	//clickCount = document.getElementById("clicks").value;
	clickCount++;
	
	//update clicks field
	//document.getElementById("clicks").value = clickCount;
}



function disableAction(){
    document.getElementsByTagName('html')[0].onclick = click;
}


function enableAction(){
    document.getElementsByTagName('html')[0].onclick = null;
}



function calculateDistance() {
	// A. CALCULATE MOUSE MOVED DISTANCE BY PIXELS
	// calculate distanceChange
	distanceChange = Math.sqrt(((newPosx - posx) * (newPosx - posx)) + ((newPosy - posy) * (newPosy - posy)));
	distanceChange = parseInt(distanceChange);
	
	//update movement field
	//oldMovement = parseInt(document.getElementById("movement").value);
	newMovement = oldMovement + distanceChange;
	//document.getElementById("movement").value = newMovement;
	
	//set new position
  newPosx = posx;
  newPosy = posy;
}

function calculateMovement() {
	// B. CALCULATE ONLY WHEN THE MOUSE IS BEING MOVED AND INCREMENT - simpler
	activity++;
	//document.getElementById("movement").value = activity;
}



function setCookie(name, value) {
  var curCookie = name + "=" + escape(value);
  document.cookie = curCookie;
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function logDuration() {
	duration++;
	//document.getElementById("duration").value = duration;
}


// GotAsyncData is the read callback for the above XMLHttpRequest() call.
// This routine is not executed until data arrives from the request.
// We update the "ajaxdata" area on the page when data does arrive.
function GotAsyncData() {
	// only if req_fifo shows "loaded"
	if (req_fifo.readyState != 4 || req_fifo.status != 200) {
	return;
	}

	//update ajaxdata
	activityID = req_fifo.responseText
	//document.getElementById("response1").value=activityID;
	
	//set new cookie
	setCookie("activityID",activityID)
	
	return;
}


function getActivityID() {
	// check existing cookie
	activityID = getCookie('activityID');
	//alert('activityID:' + activityID)
	
	// if no cookie set, generate one by first grabbing a new activityID from the mysql DB
	if (activityID == null) { 
		
		// call getActivityID.cgi
		var cgiurl = "http://oneyearinaustralia.com/cgi-bin/getActivityID.cgi";

		
		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest) {
			req_fifo = new XMLHttpRequest();
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			req_fifo = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (req_fifo) {
			req_fifo.abort();
			req_fifo.onreadystatechange = GotAsyncData;
			req_fifo.open("POST", cgiurl, true);
			req_fifo.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			req_fifo.send('url=' + docurl);
		}

		
		// set new cookie using GotAsyncData
	}

}





function recordActivity() {
	
	// the path of the cgi to execute
	var cgiurl2 = "http://oneyearinaustralia.com/cgi-bin/updateActivity.cgi";

		
	// update mysql table with the latest data
	if (window.XMLHttpRequest) {
		req_fifo2 = new XMLHttpRequest();
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req_fifo2 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (req_fifo2) {
		req_fifo2.abort();
		req_fifo2.open("POST", cgiurl2, true);
		req_fifo2.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		whattosend = 'duration=' + duration + '&url=' + docurl + '&clicks=' + clickCount + '&activity=' + activity + '&activityID=' + activityID;
		req_fifo2.send(whattosend);
		//alert(whattosend);
	}
}



// *****************************************************

function initialize(){
	
	// A. calculate activity as actual pixels moved by cursor
	//if (window.Event) { document.captureEvents(Event.MOUSEMOVE); }
	//document.onmousemove = getCoordinates;
	//timer = window.setInterval('calculateDistance()', Math.floor(1000/fps));
	
	// B. calculate activty as mouse movement duration
	document.onmousemove = calculateMovement;
	
	// this is to enable mouse clicks logging
	disableAction();
	
	// begin logging duration
	window.setInterval('logDuration()', 1000);
	
	// figure out activityID
  getActivityID();
  
  // let's see the url  
  //document.getElementById("url").value = docurl;
}






window.onload = initialize;
window.onunload = recordActivity;