var mousePosition="";
var tmpMousePosition="";
var t;
var timer_is_on=0;
var getPageInfo=0;
var refererPage=document.location.href;
var time_same_pos = 0;

var allLogLocal = 1;

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
if (!IE) document.captureEvents(Event.CLICK)
document.onclick = getClickXY;


if(typeof(allLog)  == 'undefined') {
	allLogLocal = 1;
} else {
	allLogLocal = 2;
}

var prevX = 0;
var prevY = 0;

var tempX = 0;
var tempY = 0;
var clickX = 0;
var clickY = 0;

// Fonction permettant de recuperer le deplacement
// du curseur de souris
function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	return true;
}

function getClickXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		clickX = event.clientX + document.body.scrollLeft;
		clickY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		clickX = e.pageX;
		clickY = e.pageY;
	}  
	pushSupergo(3);
	return true;
}

// Fonction permettant de remonter les logs
// sur la plate-forme Supergonomie 
function pushSupergo(mode)
{
	if(mode == 1 && refererPage != "") {
		// PUSH Changement de page
		var script = document.createElement('script');
		script.src = 'http://www.supergonomie.com/iTesteur/index.php5?';
		script.src += 'action=mod_logguer';
		script.src += '&mode=1';
		script.src += '&allLog='+allLogLocal;
		script.src += '&siteKey='+sitekey;
		script.src += '&time='+Math.floor((new Date()).getTime() / 1000);
		script.src += '&URL='+encodeURIComponent(refererPage);
 
		script.id = 'requestMultiplier';
		script.type = 'text/javascript';
 
		document.body.appendChild(script);
		
		getPageInfo=1;	
	} else if(mode == 2 && tmpMousePosition != "") {
		// PUSH deplacement Souris
		var script = document.createElement('script');
		script.src = 'http://www.supergonomie.com/iTesteur/index.php5?';
		script.src += 'action=mod_logguer';
		script.src += '&mode=2';
		script.src += '&allLog='+allLogLocal;
		script.src += '&siteKey='+sitekey;
		script.src += '&time='+Math.floor((new Date()).getTime() / 1000);
		script.src += '&mouse='+encodeURIComponent(tmpMousePosition);
		script.src += '&URL='+encodeURIComponent(refererPage);
		
		script.id = 'requestMultiplier';
		script.type = 'text/javascript';
 
		document.body.appendChild(script);	
		
		tmpMousePosition="";
	} else if(mode == 3) {
		// PUSH click Souris
		var script = document.createElement('script');
		script.src = 'http://www.supergonomie.com/iTesteur/index.php5?';
		script.src += 'action=mod_logguer';
		script.src += '&mode=3';
		script.src += '&allLog='+allLogLocal;
		script.src += '&siteKey='+sitekey;
		script.src += '&time='+Math.floor((new Date()).getTime() / 1000);
		script.src += '&mouse='+encodeURIComponent(clickX+":"+clickY+";");
		script.src += '&URL='+encodeURIComponent(refererPage);
		
		script.id = 'requestMultiplier';
		script.type = 'text/javascript';
 
		document.body.appendChild(script);	
		
		clickX=0;
		clickY=0;
	}
}

var nbCount=0;
function timedPushSupergo()
{
	if(document.body != null && getPageInfo==0) {
		pushSupergo(1);
	}

	if(mousePosition.length > 0 && nbCount >= 10) {
		tmpMousePosition = mousePosition;
		mousePosition = '';
		pushSupergo(2);
		nbCount = 0;
	}
	nbCount++;
	
	if(prevX == tempX && prevY == tempY) {
		time_same_pos++;
	} else {
		if(time_same_pos > 0) {
			mousePosition+=time_same_pos+";";
			time_same_pos = 0;
		}
		prevX = tempX;
		prevY = tempY;
		mousePosition+=tempX+"|"+tempY+";";
	}
	t=setTimeout("timedPushSupergo()",100);
}


function doPush()
{
	if (!timer_is_on)
	{
		timer_is_on=1;
		timedPushSupergo();
	}
}

doPush();
