/***************************************************************
*  Copyright notice
*
*  (c) 2008-2009 Clara Brocar <cbrocar@pagemachine.de>
*  (c) 2011 Helmut Hummel <typo3-ext@naw.info>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
* Some code and inspiration taken from ClickHeat by labsmedia.com (published under GNU GPL)
*/

HeatmapPro = {
	init: function (options) {
		this.pageId = options.pageId;
		this.pageLanguageId = options.pageLanguageId;
		this.clickInterval = options.clickInterval || 1000;

		if (document.addEventListener) {
			document.addEventListener('mousedown', this.bind(this, this.logClick), false);
		} else if (document.attachEvent) {
			document.attachEvent('onmousedown', this.bind(this, this.logClick));
		}
	},

	bind: function (scope, fn) {
		return function () {
			fn.apply(scope, arguments);
		}
	},

	request: false,

	logClick: function (event) {
		var x = event.clientX;
		var y = event.clientY;
		var w;
		var h;
		var scrollyx;
		var scrolly;
		if (document.documentElement != undefined && document.documentElement.clientHeight != 0) {
			w = document.documentElement.clientWidth != undefined ? document.documentElement.clientWidth: window.innerWidth;
			h = document.documentElement.clientHeight != undefined ? document.documentElement.clientHeight: window.innerHeight;
			scrollx = window.pageXOffset == undefined ? document.documentElement.scrollLeft: window.pageXOffset;
			scrolly = window.pageYOffset == undefined ? document.documentElement.scrollTop: window.pageYOffset;
		} else {
			w = document.body.clientWidth != undefined ? document.body.clientWidth: window.innerWidth;
			h = document.body.clientHeight != undefined ? document.body.clientHeight: window.innerHeight;
			scrollx = window.pageXOffset == undefined ? document.body.scrollLeft: window.pageXOffset;
			scrolly = window.pageYOffset == undefined ? document.body.scrollTop: window.pageYOffset;
		}

		var realx = x + scrollx;
		var realy = y + scrolly;

		// only log once in a second
		time = new Date();
		if (time.getTime() - this.clickTime < this.clickInterval) {
			return true;
		}
		this.clickTime = time.getTime();

		// nicht loggen, wenn auf einen Scrollbalken geklickt wurde
		if (x > w || y > h) {
			return true;
		}

		if (!this.request) {
			// Ajax Request
			if (window.ActiveXObject) {
				try {
					// IE 6 and higher
					this.request = new ActiveXObject("MSXML2.XMLHTTP");
				} catch(e) {
					try {
						// IE 5
						this.request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
						this.request = false;
					}
				}
			} else if (window.XMLHttpRequest) {
				try {
					// Mozilla, Opera, Safari ...
					this.request = new XMLHttpRequest();
				} catch(e) {
					this.request = false;
				}
			}
		}

		if (!this.request) {
			//alert("An Error occured when trying to initialize XMLHttpRequest!");
			return false;
		} else {
			var params = 'eID=heatmap_fe&s=' + this.pageId + '&l=' + this.pageLanguageId + '&x=' + realx + '&y=' + realy + '&w=' + w;
			var url = 'index.php' + '?' + params;
			this.request.open('GET', url, true);
			this.request.send(null);
		}

		return true;
	}
}


