/***************************************************************
*  Copyright notice
*
*  (c) 2008-2009 Clara Brocar <cbrocar@pagemachine.de>
*  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)
*/

var id = '';
var clickTime = 0;
var request = '';

function logClick(e) {
  var x = e.clientX;
  var y = e.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;

  // nicht loggen, wenn letzter Klick weniger als 1 Sekunde zur�ck liegt
  time = new Date();
  if (time.getTime() - clickTime < 1000) {
    return true;
  }
  clickTime = time.getTime();

  // nicht loggen, wenn auf einen Scrollbalken geklickt wurde
  if (x > w || y > h) {
    return true;
  }

  // Ajax Request
  if (window.ActiveXObject) {
    try {
      // IE 6 and higher
      request = new ActiveXObject("MSXML2.XMLHTTP");
    } catch(e) {
      try {
        // IE 5
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        request = false;
      }
    }
  } else if (window.XMLHttpRequest) {
    try {
      // Mozilla, Opera, Safari ...
      request = new XMLHttpRequest();
    } catch(e) {
      request = false;
    }
  }

  if (!request) {
    //alert("An Error occured when trying to initialize XMLHttpRequest!");
    return false;
  } else {
    var params = 'eID=heatmap_fe&s=' + id + '&x=' + realx + '&y=' + realy + '&w=' + w;
    var url = 'index.php' + '?' + params;
    request.open('GET', url, true);
    request.send(null);
  }

  return true;
}

function initHeatmap() {
  if (document.addEventListener) {
    document.addEventListener('mousedown', logClick, false);
  } else if (document.attachEvent) {
    document.attachEvent('onmousedown', logClick);
  }
}