points = '';
count = 0;
pageKey = '';

chunk_size = 256;

function finishTrack(event)
{
   send();
   accumulateTrack(event);
}

function accumulateTrack(event)
{
   //alert('yo');
   point = new Object();
   //point.x = event.pageX - $('body').offset().left - $('body').width()/2;
   tracksMiddleX = $('#tracks').offset().left + $('#tracks').width()/2;
      
   point.x = event.pageX - tracksMiddleX;
   point.y = event.pageY /*+ $(window).scrollTop()*/;
   //points.push(point);
   points = points + point.x + ',' + point.y + ';';
   count++;

   if(count > chunk_size)
   {
      count = 0;
      finishTrack(event);
   }
}

function initTracks(thePageKey)
{
   pageKey = thePageKey;
   $('#tracks').css('background', "url('/tracks/tracks.gif.php?" + pageKey + "') center top");

   $('body').bind('mouseenter', function(event)
   {
      $('#log').prepend('enter' + '<br>');
   });

   $('body').bind('mouseleave', finishTrack);
   $(window).bind('unload',     finishTrack);

   $('body').bind('mousemove', accumulateTrack);
}


function send()
{
   jQuery.post("/tracks/push.php", pageKey + "=" + points);
   points = '';
}

