function classClickStats() {
	
	this.coordinates = new Array();
	this.index = 0;
	
	this.pageWidth = 1050;
	
	this.top = 1;
	this.left = 2;
	this.content = 3;
	this.content_wide = 4;
	this.right = 5;
	this.footer = 6;
	
	this.holder = 7;
	
	classClickStats.prototype.log = function(div,e,type) {
		if(bdb.feature_count_clicks) {	
			if(typeof e.pageX != "undefined") {
	
				var template = $(".templateName",$("#content")).attr("innerHTML");
				if(!template) {	
					template = $(".templateName",$("#content_wide")).attr("innerHTML");
					if(!template) {
						template = this.getTemplatePath(bdb.ajax.lastHref);
					}
				}
				
				var position = div.position();
				var divY = position.top;
		
				var height = $(window).height();
				var width = $(window).width();
				
				var offsetX = parseInt((width - this.pageWidth) / 2);
				if(offsetX < 0) {
					offsetX = 0;
				}
	
				var xValue = parseInt(e.pageX - offsetX);
				var yValue = parseInt(e.pageY - divY);
				
				//Deal with margins and paddings.
				if(type == 6) { //footer got margin 40.
					yValue -= 40;
				} else if(type == 3) {
					xValue -= 188; //content
				} else if(type == 5) {
					xValue -= 802; //right
				} else if(type == 2) {
					xValue += 18; //left 18px padding
				} else if(type == 1) {
					xValue += 18; //top
				}
				
				if(this.index == 0 || !(this.coordinates[this.index-1]["x"] == parseInt(xValue) && this.coordinates[this.index-1]["y"] == parseInt(yValue))) {
					
					var coord = new Array();
					coord["x"] = parseInt(xValue);
					coord["y"] = parseInt(yValue);
					coord["count"] = 1;
					coord["type"] = type;
					coord["template"] = template;
					
					this.coordinates[this.index] = coord;
					this.index++;
					
					if(this.index >= 10) {
						this.save();
						this.clear();
					}
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	* Function fetches the templatepath from the history.
	**/
	classClickStats.prototype.getTemplatePath = function(template) {
		var templatename;
		if(template.indexOf(".") != -1) {
			templatename = template.substr(0,template.indexOf(".")) + ".html";
			if(templatename.indexOf("/p/") != -1) {
				templatename = templatename.substr(templatename.indexOf("/p/"));
			}
			if(templatename.indexOf("_content") != -1) {
				templatename = templatename.replace("_content","");
			}
		} else {
			templatename = template + ".html";
		}
		return templatename;
	}
	
	classClickStats.prototype.print = function() {
		if(this.index > 0) {
			for(i=0;i<this.index;i++) {
				console.log(this.coordinates[i]["x"] + "," + this.coordinates[i]["y"] + " on type = " + this.coordinates[i]["type"]);
			}
		}
	}
	
	classClickStats.prototype.clear = function() {
		this.coordinates = new Array();
		this.index = 0;
	}
	
	classClickStats.prototype.save = function() {
		var queryStr = "";
		
		for(i=0;i<this.index;i++) {
			queryStr += "&x["+i+"]="+this.coordinates[i]["x"]+"&y["+i+"]="+this.coordinates[i]["y"]+"&type["+i+"]="+this.coordinates[i]["type"]+"&template["+i+"]="+this.coordinates[i]["template"];
		}
		$.getJSON("/p/ajax.html?action=saveClickStats"+queryStr);
	}
	
	classClickStats.prototype.paint = function(div,template) {
		
		var position = div.position();

		var height = div.height();
		var width = div.width();

		var padding_left = 0;
		var padding_top = 0;
		if(div.attr("id") == "content_wide") {
			padding_left = 18;
		}
		else if(div.attr("id") == "left") {
			height += 18*2;
			width += 18*2;
		}
		else if(div.attr("id") == "footer") {
			padding_top = 40;
			padding_left = 8;
			width += 40;
			height += 8;
		}
		else if(div.attr("id") == "right") {
			//right has no height set on the div...
			height = 1695;
		}
		
		var newdiv = "<div id='clickstatsDiv"+div.attr("id")+"' style='min-height:"+height+"px; min-width:"+width+"px; position:absolute; z-index:100; left:"+(position.left+padding_left)+"px; top:"+(position.top+padding_top)+"px;'></div>";
		div.append(newdiv);
		
		var imgdiv = "<img src='/p/ajax.html?action=drawClickStats&type="+div.attr("id")+"&template="+template+"&width="+width+"&height="+height+"'>";
		$("#clickstatsDiv"+div.attr("id")).append(imgdiv);
		
		var top = 0;
		switch(div.attr("id")) {
			case "top":
				top = 0;
				break;
			case "left":
				top = 100;
				break;
			case "content_wide":
				top = 200;
				break;
			case "content":
				top = 200;
				break;
			case "right":
				top = 300;
				break;
			case "footer":
				top = 400;
				break;
		}
		
		var position = $("#holder").position();
		
		var left = position.left - 120;
		
		var imgdiv = "<img style='position:absolute; left:"+left+"px; top:"+top+"px;' src='/p/ajax.html?action=drawClickStatsLegend&type="+div.attr("id")+"&template="+template+"'>";
		$("#holder").append(imgdiv);
	}
	
}

var old_click = jQuery.fn.click;
jQuery.fn.click = function(func) {
	var newFunc = function(e) {
		
		//Check which div it belongs to to see where to save the stats.
		//If it was the exact div we clicked on then the click events in framework deals with saving the stats from there already.
		if(bdb.feature_count_clicks) {
			if($(this).parents("#top").length) {
				bdb.clickstats.log($("#top"),e,bdb.clickstats.top);
			} else if($(this).parents("#left").length) {
				bdb.clickstats.log($("#left"),e,bdb.clickstats.left);
			} else if($(this).parents("#content").length) {
				bdb.clickstats.log($("#content"),e,bdb.clickstats.content);
			} else if($(this).parents("#content_wide").length) {
				bdb.clickstats.log($("#content_wide"),e,bdb.clickstats.content_wide);
			} else if($(this).parents("#right").length) {
				bdb.clickstats.log($("#right"),e,bdb.clickstats.right);
			} else if($(this).parents("#footer").length) {
				bdb.clickstats.log($("#footer"),e,bdb.clickstats.footer);
			}
		}

		return func.apply(this, arguments);
		
	}
	old_click.call(this,newFunc);
}
