var wiredmindsHeatmapCount = {
	
	lastClickTime: 0,
	custnum: wm_custnum,
	cnt_server : "alpine.wiredminds.de",
	cnt_path: '/track/countxy.php',

	getClick: function(e) {
		
		var click_x = 0;
		var click_y = 0;
		var max_allowed_x = 2500;
		
		// Check if last click was at least 1 second ago
		var time = new Date();
		if ((time.getTime() - this.lastClickTime) < 1000){
			return;
		}
		this.lastClickTime = time.getTime();
		
		/**
		 * Check if the heatmap is started
		 * If the overlay is still set, then the overlay is active, so there is no need to count the click.
		 * No need to validate the key, the counting pixel already did, and invalid keys are not stored.
		 */
		var overlayKey = wiredminds.ReadCookie('WMOverlayKey');
		if ((overlayKey) && (overlayKey.length >= 32)){
			return;
		}

		// Grab coordinate x,y of clicks
		var browser = this.getBrowser();
		if (browser == "IE6" || browser == "IE7" ||  browser == "IE8") {
			click_x = event.clientX;
			click_y = event.clientY;
		} else {
			click_x = e.clientX;
			click_y = e.clientY;
		}

		var scrollbarWidth = this.getScrollbarWidth();
		var d = document.documentElement != undefined && document.documentElement.clientHeight != 0 ? document.documentElement : document.body;
		var scrollx  = window.pageXOffset == undefined ? d.scrollLeft : window.pageXOffset;
		var scrolly  = window.pageYOffset == undefined ? d.scrollTop : window.pageYOffset;
		var client_w = window.innerWidth == undefined ? d.clientWidth : window.innerWidth;
		var client_h = window.innerHeight == undefined ? d.clientHeight : window.innerHeight;
		var body_w   = d.offsetWidth;
		var body_h   = d.offsetHeight;

		// Check if clicks was on the scrollbars
		if ((body_h > client_h) && (click_x > (client_w - scrollbarWidth))) {
			return;
		}

		// Add the width/height of scroll to coordinate
		click_x = click_x + scrollx;
		click_y = click_y + scrolly;

		/**
		 * If wm_content_width provided content is centered, we need to calculate the mouse position relative to content width.
		 * Else content is left aligned, no need to modify mouse position.
		 */
		var click_x_orig = click_x;
		if ((typeof (wm_content_width) != 'undefined' ) && (wm_content_width <= body_w)){
			click_x = (click_x - (body_w - wm_content_width)/2 );
		}
		
		var page_name = wiredminds.getPageName();
		var params = new Array(
			'custnum='	+ this.custnum,
			'pagename='	+ encodeURIComponent(page_name),
			'cx='		+ Math.round(click_x),
			'cy='		+ Math.round(click_y),
			'body_height=' + Math.round(body_h),
			'browser='	+ browser
			);

		if ((typeof (wm_content_width) != 'undefined')){
			if (!isNaN(parseInt(wm_content_width)) && (parseInt(wm_content_width) > 0)){
				max_allowed_x = ((body_w - wm_content_width)/2) + wm_content_width;
				params.push('content=' + parseInt(wm_content_width));
			}
		}
		// send request
		if ((click_x > 0) && (click_x_orig < max_allowed_x)){
			var proto = wiredminds.getProto();
			var count_heat_URL = proto + '//' + this.cnt_server + this.cnt_path;
			var heatmap_container = new Image();
			heatmap_container.src=count_heat_URL + '?' + params.join('&');
		}
	},
	
	/**
	 * Get the width of scrollbar
	 * 
	 */
	getScrollbarWidth: function() {
		
		var scr = null;
		var inn = null;
		var wNoScroll = 0;
		var wScroll = 0;

		// Outer scrolling div
		scr = document.createElement('div');
		scr.style.position = 'absolute';
		scr.style.top = '-1000px';
		scr.style.left = '-1000px';
		scr.style.width = '100px';
		scr.style.height = '50px';
		// Start with no scrollbar
		scr.style.overflow = 'hidden';

		// Inner content div
		inn = document.createElement('div');
		inn.style.width = '100%';
		inn.style.height = '200px';

		// Put the inner div in the scrolling div
		scr.appendChild(inn);
		// Append the scrolling div to the doc
		document.body.appendChild(scr);

		// Width of the inner div sans scrollbar
		wNoScroll = inn.offsetWidth;
		
		// Add the scrollbar
		var browser = this.getBrowser();
		if (browser == "IE6" || browser == "IE7" || browser == "IE8") {
			scr.style.overflow = 'scroll';
		} else {
			scr.style.overflow = 'auto';
		}
		// Width of the inner div width scrollbar
		wScroll = inn.offsetWidth;

		// Remove the scrolling div from the doc
		document.body.removeChild(document.body.lastChild);

		// Pixel width of the scroller
		return (wNoScroll - wScroll);
	},

	/**
	 * Detect the browsers the clickers used to view page.
	 * TODO: Moment take care the function only 3 Browsers: IE6/IE7/IE8/FF
	 */
	getBrowser: function() {
		var version = navigator.appVersion;
		if (version.search(/MSIE 6/i) != -1) return "IE6";
		if (version.search(/MSIE 7/i) != -1) return "IE7";
		if (version.search(/MSIE 8/i) != -1) return "IE8";
		if (version.search(/MSIE/i)   == -1) return "Others";
	}
}

/**
 * Catch click for Heatmap
 *
 */
var getClick = function(e){
	wiredmindsHeatmapCount.getClick(e);
}
