// Required <div id="page"></div> with the width of the page

var ClickTrack = {
	req 			: null,
	service_url : null,
	file			: null,
	
   init : function(service_url, file, object) {
		this.service_url = service_url;
		this.file = file;		
		this.loadRequest();
      document.getElementById(object).onclick = function(e) { ClickTrack.store_click(e, this); }
   },
   
	store_click : function(e, obj) {
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		ClickTrack.call(posx - obj.offsetLeft, posy - obj.offsetTop);
	},


	call : function(x, y) {
		if(this.req) {
			this.req.open("GET", this.service_url+"?url="+window.location+"&file="+this.file+"&x="+x+"&y="+y, true);
			this.req.send("");
		}
	},

	loadRequest : function() {
		if(this.req)		
			return this.req;
			
		if(window.XMLHttpRequest) {
			try {
				this.req = new XMLHttpRequest();
			} catch(e) {
				this.req = false;
			}
		} else if(window.ActiveXObject) {
			try {
				this.req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					this.req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					this.req = false;
				}
			}
		}
		return this.req;		
	}
} 
