/**
clickmap.js

Handover clicks to saveClickScript (x,y,width)

*/

/** Main function */
function transferClick(e)
{
	/** Use a try{} to avoid showing errors to users */
	try
	{
		if (maxClicksPerPage == 0)
		{
			if (debugThis == true)
			{
				alert('Click not logged: quota reached');
			}
			return true;
		}
		/** Look for the real event */
		if (e == undefined)
		{
			e = window.event;
			c = e.button;
			element = e.srcElement;
		}
		else
		{
			c = e.which;
			element = null;
		}
		if (c == 0)
		{
			if (debugThis == true)
			{
				alert('Click not logged: no button pressed');
			}
			return true;
		}

		x = e.clientX;
		y = e.clientY;
		d = document.documentElement != undefined && document.documentElement.clientHeight != 0 ? document.documentElement : document.body;
		
		scrollx = window.pageXOffset == undefined ? d.scrollLeft : window.pageXOffset;
		scrolly = window.pageYOffset == undefined ? d.scrollTop : window.pageYOffset;
		
		w = window.innerWidth == undefined ? d.clientWidth : window.innerWidth;
		h = window.innerHeight == undefined ? d.clientHeight : window.innerHeight;
		
		/** Is the click in the viewing area? Not on scrollbars */
		if (x > w || y > h)
		{
			if (debugThis == true)
			{
				alert('Click not logged: out of document (should be a click on scrollbars under IE)');
			}
			return true;
		}
		
		/** Check if last click was at least 1 second ago */
		clickTime = new Date();
		if (clickTime.getTime() - timeOffset < 1000)
		{
			if (debugThis == true)
			{
				alert('Click not logged: at least 1 second between clicks');
			}
			return true;
		}
		timeOffset = clickTime.getTime();
		if (maxClicksPerPage > 0)
		{
			maxClicksPerPage = maxClicksPerPage - 1;
		}
		
		/** Also the User-Agent is not the best value to use, it's the only one that gives the real browser */
		b = navigator.userAgent != undefined ? navigator.userAgent.toLowerCase().replace(/-/g, '') : '';
		b0 = b.replace(/^.*(firefox|kmeleon|safari|msie|opera).*$/, '$1');
		if (b == b0 || b0 == '') b0 = 'unknown';
		params = 'p=' + usePage + '&x=' + (x + scrollx) + '&y=' + (y + scrolly) + '&w=' + w + '&b=' + b0 + '&c=' + c + '&random=' + Date();
		
		/** Local request? Try an ajax call */
		var sent = false;
		
		var xmlhttp = false;
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e)
		{
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	}
			catch (oc) { xmlhttp = null; }
		}
		if (!xmlhttp && typeof XMLHttpRequest != undefined) xmlhttp = new XMLHttpRequest();
		if (xmlhttp)
		{
			if (debugThis == true)
			{
				xmlhttp.onreadystatechange = function()
				{
					if (xmlhttp.readyState == 4)
					{
						if (xmlhttp.status == 200)
						{
							alert('Click logged to ' + saveClickScript + ' with the following parameters:\nx = ' + (x + scrollx) + ' (' + x + 'px from left + ' + scrollx + 'px of horizontal scrolling)\ny = ' + (y + scrolly) + ' (' + y + 'px from top + ' + scrolly + 'px of vertical scrolling)\nwidth = ' + w + '\npage = ' + usePage + '\nbrowser = ' + b0 + '\nclick = ' + c + '\n\nServer answer: ' + xmlhttp.responseText);
						}
						else if (xmlhttp.status == 404)
						{
							alert('click.php was not found at: ' + (saveClickScript != '' ? saveClickScript : '/clickheat/click.php') + ' please set saveClickScript value');
						}
						else
						{
							alert('click.php returned a status code ' + xmlhttp.status + ' with the following error: ' + xmlhttp.responseText);
						}
					}
				}
			}
			xmlhttp.open('GET', saveClickScript + '?' + params, true);
			xmlhttp.setRequestHeader('Connection', 'close');
			xmlhttp.send(null);
			sent = true;
		}
		
		/* Problems with the AJAX-Call */
		if (sent == false)
		{

		}
	}
	catch(e)
	{
		if (debugThis == true)
		{
			alert('An error occurred while processing click');
		}
	}
	return true;
}

var saveClickScript = '/eyekit/scripts/saveClick.php';
var timeOffset = 1000;
var maxClicksPerPage = 10;
var debugThis = 0;
function catchClick()
{
	
	/* Mousedown-Event */
	if (typeof document.onmousedown == 'function')
	{
		currentFunc = document.onmousedown;
		document.onmousedown = function(e) { transferClick(e); return currentFunc(e); }
	}
	else
	{
		document.onmousedown = transferClick;
	}

	if (debugThis == true)
	{
		alert('ClickHeat initialised with:\npage = ' + usePage + '\nserver = ' + saveClickScript + '\nquota = ' + (maxClicksPerPage == -1 ? 'unlimited' : maxClicksPerPage));
	}
}
