( function($) {
$(document).ready(function() {

var movementgridsize = 5;
var prevMoveX = 0;
var prevMoveY = 0;
var clickArray = new Array();
var mouseArray = new Array();
clickArray[0] = window.location.pathname;
clickArray[1] = $('#siteframe').width();
clickArray[2] = $('#siteframe').height();
mouseArray[0] = window.location.pathname;
mouseArray[1] = $('#siteframe').width();
mouseArray[2] = $('#siteframe').height();

function clickObj(x,y){
return {
x:x,
y:y
};
};

window.onbeforeunload = function() {
var stringArray = JSON.stringify(clickArray);
var stringMovementArray = JSON.stringify(mouseArray);//clickArray.toJSONString();
//alert("sending:" + stringArray);
$.ajax({
      url: "/content/heatmousemap.html",
      type: "POST",
      data: stringMovementArray,
	  processData: false,
      async: false,
      success: function(msg){
       $.ajax({
      url: "/content/heatmap.html",
      type: "POST",
      data: stringArray,
	  	  processData: false,
      async:false,
      success: function(msg){
        // alert(msg);
      }
  }   
);
      }
  }   
)
};



//Click HeatMap
$('#siteframe').click(
function(evt){
var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
 winW = document.body.offsetWidth;
 winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
    document.documentElement &&
    document.documentElement.offsetWidth ) {
 winW = document.documentElement.offsetWidth;
 winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
 winW = window.innerWidth;
 winH = window.innerHeight;
}
thex = evt.pageX;
they = evt.pageY;
if(winW > 750){
//the minus ten is for scrollbars which i am assuming always visible on the website.
thex = thex - ((winW - 750)/2) + 6;
}
thex = Math.round(thex);
they = Math.round(they);
clickArray.push(clickObj(thex,they));
//console.log(clickArray);

});


//Movement HeatMap
$('#siteframe').mousemove(
function(evt){
var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
 winW = document.body.offsetWidth;
 winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
    document.documentElement &&
    document.documentElement.offsetWidth ) {
 winW = document.documentElement.offsetWidth;
 winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
 winW = window.innerWidth;
 winH = window.innerHeight;
}
thex = evt.pageX;
they = evt.pageY;
if(winW > 750){
//the minus ten is for scrollbars which i am assuming always visible on the website.
thex = thex - ((winW - 750)/2) + 6;
}
thex = Math.round((parseInt(thex)/5)*5);
they = Math.round((parseInt(they)/5)*5);
if(thex != prevMoveX || they != prevMoveY){
mouseArray.push(clickObj(thex,they));
//console.log(thex + " : " + they);
}
prevMoveX = thex;
prevMoveY = they;
});


});

} ) ( jQuery );
