/** Main variables */
var clickHeatGroup = '1';
var clickHeatSite = '';
var clickHeatServer = 'http://www.pagecounter.nl/click.php';
var clickHeatServerExit = 'http://www.pagecounter.nl/exit.php';
var clickHeatLastIframe = -1;
var clickHeatTime = 0;
var clickHeatQuota = -1;
var clickHeatBrowser = '';
var clickHeatDocument = '';
var clickHeatWait = 500;
var clickHeatLocalWait = 0;
var clickHeatDebug = (window.location.href.search(/debugclickheat/) !== -1);
var clickHeatSiteId = 0;
var w=window;

function plugNS()
{
	var b=1;
	var o=0;
	var p=new Array("Shockwave Flash","Shockwave for Director","RealPlayer","QuickTime","VivoActive","LiveAudio","VRML","Dynamic HTML Binding","Windows Media Services");
	var np=navigator.plugins;
	for(var x=0;x<p.length;x++)
	{
		for(var i=0;i<np.length;i++)
		if(np[i].name.indexOf(p[x])>=0)
			o|=b;
		b*=2;
	}
	return o;
}
function plugIE()
{
	if(!document.body)
		document.write('<body>');
	var db=document.body;
	var o=0;
	var b=1;
	var p=new Array("D27CDB6E-AE6D-11CF-96B8-444553540000","2A202491-F00D-11CF-87CC-0020AFEECF20","23064720-C4F8-11D1-994D-00C04F98BBC9","","","","90A7533D-88FE-11D0-9DBE-0000C0411FC3","9381D8F2-0288-11D0-9501-00AA00B911A5","22D6F312-B0F6-11D0-94AB-0080C74C7E95");
	db.addBehavior("#default#clientcaps");
	for(var i=0;i<p.length;i++)
	{
		if(p[i])
		if(db.isComponentInstalled("{"+p[i]+"}","componentid"))
			o|=b;
		b*=2;
	}
return o;
}
function pagecounter(id,options)
{
	var n=navigator;
	var ver=n.appVersion;
	var d=document;
	var verIE=parseInt(ver.substring(ver.indexOf("MSIE")+5,ver.indexOf("MSIE")+6));
	if(verIE>0)ver=verIE;
	else ver=parseInt(ver);
	var u="http://www.pagecounter.nl/img.php?siteid="+id;
	var r;
	var loc="&loc="+top.document.location;
	if(options&1)r=d.referrer;
	else r=top.document.referrer;
	if(!(options&2))
	{
		var rc=r;
		var i=rc.indexOf('?');
		if(i>=0)rc=rc.substring(0,i);i=rc.lastIndexOf('/');
		if(i>=0)rc=rc.substring(0,i+1);
		var l=''+d.location;
		if(l.indexOf(rc)==0)r='';
	}
	if(r&&(r!=d.location))
		u+="&r="+escape(r);
	if((n.appName=="Netscape"&&ver>=3))
	u+="&p="+plugNS();
	if(verIE>=5&&n.appVersion.indexOf('Win')>=0&&n.userAgent.indexOf('Opera')<0)u+="&p="+plugIE();
	if(ver>=4)
	{
		var s=screen;
		var w=s.width;
		var h=s.height;
		var c=s.colorDepth;
		if(w)u+="&w="+w;
		if(h)u+="&h="+h;
		if(c)u+="&c="+c;
	}
	u+="&v=1";
	d.write('<a target=_blank href="http://www.pagecounter.nl/stats.php?siteid='+id+'"><img src="'+u+loc+'" border=0 width=18 height=18 alt="Pagecounter - Free web site statistics"></a>');
	clickHeatSiteId = id;
	initClickHeat();
}

/**
* Shows a debug string
**/
function showClickHeatDebug(str)
{
	if (clickHeatDebug === true)
	{
		document.getElementById('clickHeatDebuggerSpan').innerHTML = str;
		document.getElementById('clickHeatDebuggerDiv').style.display = 'block';
	}
}
/** Main function */
function catchClickHeat(e)
{
	/** Use a try{} to avoid showing errors to users */
	try
	{
		showClickHeatDebug('Gathering click data...');
		if (clickHeatQuota === 0)
		{
			showClickHeatDebug('Click not logged: quota reached');
			return true;
		}
		if (clickHeatGroup === '')
		{
			showClickHeatDebug('Click not logged: group name empty (clickHeatGroup)');
			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)
		{
			showClickHeatDebug('Click not logged: no button pressed');
			return true;
		}
		/** 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)
			{
				showClickHeatDebug('Click not logged: same iframe (a click on iframe opens a popup and popup is closed => iframe gets the focus again)');
				return true;
			}
			clickHeatLastIframe = element.sourceIndex;
		}
		else
		{
			clickHeatLastIframe = -1;
		}
		var x = e.clientX;
		var y = e.clientY;
		var w = clickHeatDocument.clientWidth !== undefined ? clickHeatDocument.clientWidth : window.innerWidth;
		var h = clickHeatDocument.clientHeight !== undefined ? clickHeatDocument.clientHeight : window.innerHeight;
		var scrollx = window.pageXOffset === undefined ? clickHeatDocument.scrollLeft : window.pageXOffset;
		var scrolly = window.pageYOffset === undefined ? clickHeatDocument.scrollTop : window.pageYOffset;
		/** Is the click in the viewing area? Not on scrollbars. The problem still exists for FF on the horizontal scrollbar */
		if (x > w || y > h)
		{
			showClickHeatDebug('Click not logged: out of document (should be a click on scrollbars)');
			return true;
		}
		/** Check if last click was at least 1 second ago */
		clickTime = new Date();
		if (clickTime.getTime() - clickHeatTime < 1000)
		{
			showClickHeatDebug('Click not logged: at least 1 second between clicks');
			return true;
		}
		clickHeatTime = clickTime.getTime();
		if (clickHeatQuota > 0)
		{
			clickHeatQuota = clickHeatQuota - 1;
		}
		params = 's=' + clickHeatSite + '&g=' + clickHeatGroup + '&x=' + (x + scrollx) + '&y=' + (y + scrolly) + '&w=' + w + '&b=' + clickHeatBrowser + '&c=' + c + '&siteid=' + clickHeatSiteId + '&random=' + Date();
		showClickHeatDebug('Ready to send click data...');
		/** Local request? Try an ajax call */
		var sent = false;
		if (clickHeatServer.substring(0, 4) !== 'http')
		{
			var xmlhttp = false;
			try
			{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (er)
			{
				try
				{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (oc)
				{
					xmlhttp = null;
				}
			}
			if (!xmlhttp && typeof XMLHttpRequest !== undefined)
			{
				xmlhttp = new XMLHttpRequest();
			}
			if (xmlhttp)
			{
				if (clickHeatDebug === true)
				{
					xmlhttp.onreadystatechange = function()
					{
						if (xmlhttp.readyState === 4)
						{
							if (xmlhttp.status === 200)
							{
								showClickHeatDebug('Click recorded at ' + clickHeatServer + ' with the following parameters:<br />x = ' + (x + scrollx) + ' (' + x + 'px from left + ' + scrollx + 'px of horizontal scrolling)<br />y = ' + (y + scrolly) + ' (' + y + 'px from top + ' + scrolly + 'px of vertical scrolling)<br />width = ' + w + '<br />browser = ' + clickHeatBrowser + '<br />click = ' + c + '<br />site = ' + clickHeatSite + '<br />group = ' + clickHeatGroup + '<br /><br />Server answer: ' + xmlhttp.responseText);
							}
							else if (xmlhttp.status === 404)
							{
								showClickHeatDebug('click.php was not found at: ' + (clickHeatServer !== '' ? clickHeatServer : '/clickheat/click.php') + ' please set clickHeatServer value');
							}
							else
							{
								showClickHeatDebug('click.php returned a status code ' + xmlhttp.status + ' with the following error: ' + xmlhttp.responseText);
							}
							/** Stop waiting */
							clickHeatLocalWait = 0;
						}
					};
				}
				xmlhttp.open('GET', clickHeatServer + '?' + params, true);
				xmlhttp.setRequestHeader('Connection', 'close');
				xmlhttp.send(null);
				sent = true;
			}
		}
		if (sent === false)
		{
			/** This test is needed, as it includes the call to click.php in the iframe */
			if (clickHeatDebug === true)
			{
				showClickHeatDebug('Click recorded at ' + clickHeatServer + ' with the following parameters:<br />x = ' + (x + scrollx) + ' (' + x + 'px from left + ' + scrollx + 'px of horizontal scrolling)<br />y = ' + (y + scrolly) + ' (' + y + 'px from top + ' + scrolly + 'px of vertical scrolling)<br />width = ' + w + '<br />browser = ' + clickHeatBrowser + '<br />click = ' + c + '<br />site = ' + clickHeatSite + '<br />group = ' + clickHeatGroup + '<br /><br />Server answer:<br />' + '<iframe src="' + clickHeatServer + '?' + params + '" width="700" height="60"></iframe>');
			}
			else
			{
				var clickHeatImg = new Image();
				clickHeatImg.src = clickHeatServer + '?' + params;
			}
		}
		/** Little waiting cycle: default is to wait until Ajax sent or until the end of the time if no Ajax is available */
		var now = new Date();
		clickHeatLocalWait = now.getTime() + clickHeatWait;
		while (clickHeatLocalWait > now.getTime())
		{
			now = new Date();
		}
	}
	catch(err)
	{
		showClickHeatDebug('An error occurred while processing click (Javascript error): ' + e.message);
	}
	return true;
}

function initClickHeat()
{
	/** Debug Window */
	if (clickHeatDebug === true)
	{
		document.write('<div id="clickHeatDebuggerDiv" style="padding:5px; display:none; position:absolute; top:10px; left:10px; border:1px solid #888; background-color:#eee; z-index:99;"><strong>ClickHeat debug: <a href="#" onmouseover="document.getElementById(\'clickHeatDebuggerDiv\').style.display = \'none\'; return false">Rollover to close</a></strong><br /><br /><span id="clickHeatDebuggerSpan"></span></div>');
	}

	if (clickHeatGroup === '' || clickHeatServer === '')
	{
		showClickHeatDebug('ClickHeat NOT initialised: either clickHeatGroup or clickHeatServer is empty');
		return false;
	}

	/** If current website has the same domain as the script, we remove the domain so that the call is made using Ajax */
	domain = window.location.href.match(/http:\/\/[^/]+\//);
	if (domain !== null && clickHeatServer.substring(0, domain[0].length) === domain[0])
	{
		clickHeatServer = clickHeatServer.substring(domain[0].length - 1, clickHeatServer.length);
	}
	/** Add onmousedown event using listeners */
	if (document.addEventListener)
	{
		document.addEventListener('mousedown', catchClickHeat, false);
	}
	else if (document.attachEvent)
	{
		document.attachEvent('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 (document.addEventListener)
		{
			iFrames[i].addEventListener('focus', catchClickHeat, false);
		}
		else if (document.attachEvent)
		{
			iFrames[i].attachEvent('onfocus', catchClickHeat);
		}
	}
	/** Preparing main variables */
	clickHeatDocument = (document.documentElement !== undefined && document.documentElement.clientHeight !== 0) ? document.documentElement : document.body;
	/** Also the User-Agent is not the best value to use, it's the only one that gives the real browser */
	var b = navigator.userAgent !== undefined ? navigator.userAgent.toLowerCase().replace(/-/g, '') : '';
	clickHeatBrowser = b.replace(/iceweasel/, 'firefox').replace(/^.*(firefox|kmeleon|safari|msie|opera).*$/, '$1');
	if (b === clickHeatBrowser || clickHeatBrowser === '')
	{
		clickHeatBrowser = 'unknown';
	}
	showClickHeatDebug('ClickHeat initialised with:<br />site = ' + clickHeatSite + '<br />group = ' + clickHeatGroup + '<br />server = ' + clickHeatServer + '<br />quota = ' + (clickHeatQuota === -1 ? 'unlimited' : clickHeatQuota) + '<br /><br />browser = ' + clickHeatBrowser);
}

/** Exit function */
function catchExit(e)
{
	/** Use a try{} to avoid showing errors to users */
	try
	{
		params = 'siteid=' + clickHeatSiteId + '&random=' + Date();
		showClickHeatDebug('Ready to send click data...');
		/** Local request? Try an ajax call */
		var sent = false;
		if (clickHeatServerExit.substring(0, 4) !== 'http')
		{
			var xmlhttp = false;
			try
			{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (er)
			{
				try
				{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (oc)
				{
					xmlhttp = null;
				}
			}
			if (!xmlhttp && typeof XMLHttpRequest !== undefined)
			{
				xmlhttp = new XMLHttpRequest();
			}
			if (xmlhttp)
			{
				if (clickHeatDebug === true)
				{
					xmlhttp.onreadystatechange = function()
					{
						if (xmlhttp.readyState === 4)
						{
							if (xmlhttp.status === 200)
							{
								showClickHeatDebug('Click recorded at ' + clickHeatServerExit + ' with the following parameters:<br />x = ' + (x + scrollx) + ' (' + x + 'px from left + ' + scrollx + 'px of horizontal scrolling)<br />y = ' + (y + scrolly) + ' (' + y + 'px from top + ' + scrolly + 'px of vertical scrolling)<br />width = ' + w + '<br />browser = ' + clickHeatBrowser + '<br />click = ' + c + '<br />site = ' + clickHeatSite + '<br />group = ' + clickHeatGroup + '<br /><br />Server answer: ' + xmlhttp.responseText);
							}
							else if (xmlhttp.status === 404)
							{
								showClickHeatDebug('click.php was not found at: ' + (clickHeatServerExit !== '' ? clickHeatServerExit : '/clickheat/click.php') + ' please set clickHeatServerExit value');
							}
							else
							{
								showClickHeatDebug('click.php returned a status code ' + xmlhttp.status + ' with the following error: ' + xmlhttp.responseText);
							}
							/** Stop waiting */
							clickHeatLocalWait = 0;
						}
					};
				}
				xmlhttp.open('GET', clickHeatServerExit + '?' + params, true);
				xmlhttp.setRequestHeader('Connection', 'close');
				xmlhttp.send(null);
				sent = true;
			}
		}
		if (sent === false)
		{
			/** This test is needed, as it includes the call to click.php in the iframe */
			if (clickHeatDebug === true)
			{
				showClickHeatDebug('Click recorded at ' + clickHeatServerExit + ' with the following parameters:<br />x = ' + (x + scrollx) + ' (' + x + 'px from left + ' + scrollx + 'px of horizontal scrolling)<br />y = ' + (y + scrolly) + ' (' + y + 'px from top + ' + scrolly + 'px of vertical scrolling)<br />width = ' + w + '<br />browser = ' + clickHeatBrowser + '<br />click = ' + c + '<br />site = ' + clickHeatSite + '<br />group = ' + clickHeatGroup + '<br /><br />Server answer:<br />' + '<iframe src="' + clickHeatServerExit + '?' + params + '" width="700" height="60"></iframe>');
			}
			else
			{
				var clickHeatImg = new Image();
				clickHeatImg.src = clickHeatServerExit + '?' + params;
			}
		}
		/** Little waiting cycle: default is to wait until Ajax sent or until the end of the time if no Ajax is available */
		var now = new Date();
		clickHeatLocalWait = now.getTime() + clickHeatWait;
		while (clickHeatLocalWait > now.getTime())
		{
			now = new Date();
		}
	}
	catch(err)
	{
		showClickHeatDebug('An error occurred while processing click (Javascript error): ' + e.message);
	}
	/** return true;*/
}
w.onbeforeunload=catchExit;

