var phpmyvisitesSite = contentumstatsite;
var phpmyvisitesURL = contentumstaturl;

/**
ClickHeat : Suivi et analyse des clics / Tracking and clicks analysis

@author Yvan Taviaud - LabsMedia - www.labsmedia.com
@since 27/10/2006
@update 01/03/2007 - Yvan Taviaud : correctif Firefox (Károly Marton)

Tested under :
Windows 2000 - IE 6.0
Linux - Firefox 2.0.0.1, Konqueror 3.5.5, IE 7
*/

/** Main function */
function catchClickHeat(e)
{

	/** Use a try{} to avoid showing errors to users */
	try
	{
		/** Look for the real event */
		if (e == undefined)
		{
			e = window.event;
			c = e.button;
			element = e.srcElement;
		}
		else
		{
			c = e.which;
			element = null;
		}


		/** Filter for same iframe (focus on iframe => popup ad => close ad => new focus on same iframe) */
		if (element != null && element.tagName.toLowerCase() == 'iframe')
		{
			if (element.sourceIndex == clickHeatLastIframe)
			{
				return true;
			}
			clickHeatLastIframe = element.sourceIndex;
		}

		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) return true;

		/** 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';



		if (halign=='center') {


			x = 800 + (x - (document.body.scrollWidth / 2));

		//	alert(document.body.scrollHeight); // sivun normikorkeuden
		//	alert(document.body.scrollWidth); // koko sivun leveyden...
		}
		else if (halign=='right') {
			x = 1600 - (document.body.scrollWidth - x);
		}



		if (valign=='middle') {
			if (document.body.scrollHeight>h) {
				areaheight = document.body.scrollHeight
			}
			else areaheight = h;

			y = 800 + (y - (areaheight/2));


		}
		else if (valign=='bottom') {

			if (document.body.scrollHeight>h) {
				areaheight = document.body.scrollHeight
			}
			else areaheight = h;

			y = 1600 - (areaheight - y);


		}



		params = 'siteId=' + phpmyvisitesSite + '&pageId=' + pageId + '&p=' + clickHeatPage + '&x=' + (x + scrollx) + '&y=' + (y + scrolly) + '&w=' + w + '&b=' + b0 + '&c=' + c + '&random=' + Date();

		/** Local request? Try an ajax call */



		if (clickHeatServer == '' || clickHeatServer.substring(0, 4) != 'http')
		{


			if (clickHeatServer == '')
			{
				clickHeatServer = 'http://contentum.fi/clickposition/makeclick.php';

			}
/*			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)
			{

				xmlhttp.open('GET', 'http://contentum.fi/clickposition/makeclick.php?' + params, true);

				xmlhttp.setRequestHeader('Connection', 'close');
				xmlhttp.send(null);
				clickHeatServer = '';

			}
*/

	var clickHeatImg = new Image();
			clickHeatImg.src = clickHeatServer + '?' + params;


		}
		if (clickHeatServer != '')
		{

			var clickHeatImg = new Image();
			clickHeatImg.src = clickHeatServer + '?' + params;
		}
	} catch(e) {

	}
	return true;
}

var clickHeatPage = '';
var clickHeatServer = '';
var clickHeatLastIframe = -1;
function initClickHeat(page, server)
{

	clickHeatPage = page;
	clickHeatServer = (server == undefined ? '' : server);
	/** Add onmousedown event */
	if (typeof document.onmousedown == 'function')
	{
		currentFunc = document.onmousedown;
		document.onmousedown = function(e) { currentFunc(e); catchClickHeat(e); return true; }
	}
	else
	{
		document.onmousedown = catchClickHeat;
	}
	/** Add onfocus event on iframes (mostly ads) - Does NOT work with Gecko-powered browsers, because onfocus doesn't exist on iframes */
	iFrames = document.getElementsByTagName('iframe');
	for (i = 0; i < iFrames.length; i++)
	{
		if (typeof iFrames[i].onfocus == 'function')
		{
			currentFunc = iFrames[i].onfocus;
			iFrames[i].onfocus = function(e) { currentFunc(e); catchClickHeat(e); return true; }
		}
		else
		{
			iFrames[i].onfocus = catchClickHeat;
		}
	}
}