/************************************************************************************
  A-Team Systems Source Code
  
  Copyright (c) 2001-2011, All Rights Reserved

  FULL SUPPORT IS AVAILABLE
  Email:  assistance@ateamsystems.com         USA Phone:   1.877.883.1394
    Web:  http://www.ateamsystems.com/       Intl Phone:  +1.603.244.3974
      
  You are hereby granted non-transferable rights to use this code in applications,
  software and scripts developed for you by A-Team Productions, LLC.
  
  Do not modify, resell, transfer or otherwise copy this code except for backup or
  migration purposes.
************************************************************************************/

var OldMouseX;
var OldMouseY;
var OriginalPageLoadTime = (new Date().getTime()) / 1000;


var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// ---- Return the approprate timer interval for the footprint to
//      be sent, based on how old the page is.
function GenerateFootprintInterval ( )
	{
	var PageAge = (new Date().getTime()/1000) - OriginalPageLoadTime;
	var Interval = 60*60*48; // 2 days default (ie; for super old)
	
	if ( PageAge < 60*2 )		//  2 min
	   Interval = 15;
	else if ( PageAge < 60*30 )		// 30 min
	   Interval = 30;
	// After 30 min, default applies

	return Interval * 1000;
	}

// ---- Generate and send the footprint data which composes of:
//
// - The current location and referrer URLs.
// - If mouse movement* has been detected since the last check in.
// - The current time zone offset
// - How old the page is (in seconds)
//
//   * NOTE: Mouse movement is only within the page itself, not
//           the user's computer entirely.
//
function SendFootprint ( )
	{
	var Movement = 'No';
	var PageAge = (new Date().getTime()/1000) - OriginalPageLoadTime;

	if (( OldMouseX != CurMouseX ) || ( OldMouseY != CurMouseY ))
		Movement = 'Yes';
	
	var JSONFootprint = { "Location" : window.location.href,
								 "Referrer" : document.referrer,
								 "TZOffset" : TZOffset,
								 "Movement" : Movement,
								 "PageAge" : Math.round(PageAge)
								 };
	
	var RequestURL = '/_tools/shared/footprint/footprint.php?Z=' + new Date().getTime();
	
	// Pause JSON UI feedback if needed
	if ( typeof JSONShowLoadingBar != 'undefined' )
		{
		origJSONShowLoadingBar = JSONShowLoadingBar;
		JSONShowLoadingBar = false;
		}
	
	// Send the footprint
	//alert('sending footprint');
	var JSONFootprintRequestor = new Ajax.Request(RequestURL,
			{
			method:'Post',
			postBody: 'JSONData=' + encodeURIComponent(Object.toJSON(JSONFootprint)),
			onSuccess: function(transport)
				{
				},
			onFailure: function(transport)
				{
				}
			});
	
	// Resume JSON UI feedback if needed
	if ( typeof JSONShowLoadingBar !== 'undefined' )
		JSONShowLoadingBar = origJSONShowLoadingBar;

	OldMouseX = CurMouseX;
	OldMouseY = CurMouseY;
	setTimeout("SendFootprint();", GenerateFootprintInterval() );
	}

// ---- Wait before sending the first one
setTimeout("SendFootprint();", GenerateFootprintInterval() );
