(function() {

	var eL = {
		pageViewId: null
		, topLeftContainerId: null
		, topLeftContainer: null
		, topLeftContainerX: 0
		, topLeftContainerY: 0
		, offsetY: 0
		, offsetX: 0
		
		, SetOffset: function(x, y) {
			this.offsetX = x;
			this.offsetY = y;
		}

		, Init: function(pageViewId, topLeftContainerId) {

			var me = this;
			this.pageViewId = pageViewId;

			if (window.attachEvent) {
				window.attachEvent('onunload', function() { me.RegisterExit(); });
			}
			else if (window.addEventListener) {
				window.addEventListener('unload', function() { me.RegisterExit(); }, false);
			}
			else if (window.opera) {
				// Opera doesn't run unload or beforeunload
				//window.opera.addEventListener(window, 'unload', function() { me.RegisterExit(); }, false);
			}
			else if ((navigator.userAgent) && (navigator.userAgent.indexOf('WebKit') != 0)) {
				window.addEventListener('beforeunload', function() { me.RegisterExit(); }, false);
				window.addEventListener('pagehide', function() { me.RegisterExit(); }, false);
			}

			if ((topLeftContainerId) && (topLeftContainerId != '')) {

				this.topLeftContainerId = topLeftContainerId;

				var container = document.getElementById(topLeftContainerId);

				if (container) {
					this.topLeftContainer = container;
					this.topLeftContainerX = 0;
					this.topLeftContainerY = 0;

					if (container.offsetParent) {
						do {
							this.topLeftContainerX += container.offsetLeft;
							this.topLeftContainerY += container.offsetTop;
						} while (container = container.offsetParent);
					}

					if (document.addEventListener) {
						document.addEventListener('mousedown', function(e) { me.RegisterClick(e); }, false);
					}
					else if (document.attachEvent) {
						document.attachEvent('onmousedown', function(e) { me.RegisterClick(e); });
					}

					var iFrames = document.getElementsByTagName('iframe');
					for (i = 0; i < iFrames.length; i++) {
						if (document.addEventListener) {
							iFrames[i].addEventListener('focus', function(e) { me.RegisterClick(e); }, false);
						}
						else if (document.attachEvent) {
							iFrames[i].attachEvent('onfocus', function(e) { me.RegisterClick(e); });
						}
					}
				}
			}
		}

		, RegisterClick: function(e) {

			if (e == undefined) {
				e = window.event;
				element = e.srcElement;
			}
			else {
				element = null;
			}

			var doc = (document.documentElement != undefined && document.documentElement.clientHeight != 0) ? document.documentElement : document.body;
			var x = e.clientX;
			var y = e.clientY;
			var w = doc.clientWidth != undefined ? doc.clientWidth : window.innerWidth;
			var h = doc.clientHeight != undefined ? doc.clientHeight : window.innerHeight;
			var scrollx = window.pageXOffset == undefined ? doc.scrollLeft : window.pageXOffset;
			var scrolly = window.pageYOffset == undefined ? doc.scrollTop : window.pageYOffset;
			
			if (x <= w || y <= h) {
				var clickX = Math.round(x - this.topLeftContainerX - this.offsetX + scrollx);
				var clickY = Math.round(y - this.topLeftContainerY - this.offsetY + scrolly);

				// Do we need to reolve this URL?
				this.PostData('/sites/global/webservices/LoggingWebService.svc/RegisterClick', '{"pageViewId":' + this.pageViewId + ',"x":' + clickX + ',"y":' + clickY + '}');
			}

			return true;
		}

		, RegisterExit: function() {
			// Do we need to reolve this URL?
			this.PostData('/sites/global/webservices/LoggingWebService.svc/RegisterEnd', '{"pageViewId":' + this.pageViewId + '}');
			return true;
		}

		, PostData: function(url, data) {
			var xmlhttp = false;
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (er) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (oc) {
					xmlhttp = null;
				}
			}
			if (!xmlhttp && typeof XMLHttpRequest != undefined) {
				xmlhttp = new XMLHttpRequest();
			}
			if (xmlhttp) {
				xmlhttp.open('POST', url, true);
				xmlhttp.setRequestHeader('Connection', 'close');
				xmlhttp.setRequestHeader('Content-Type', 'application/json')
				xmlhttp.send(data);
			}
		}
	};
	
	window.eL = eL;
	
})();
