var cmurl = "";
(function($) { 
	$.fn.saveClicks = function() { 
		$(this).bind('mousedown.clickmap', function(evt) { 
			try{
				var uri = document.location.search;
				// This works in FireFox, but not in IE, so going to use 2nd method.
				// var offset = ((window.innerWidth - 1024)/2);
				var rd_offset = (((document.getElementsByTagName("html").item(0).clientWidth - 1024)/2) > 0) ? ((document.getElementsByTagName("html").item(0).clientWidth - 1024)/2) : 0;
				if ( uri.search(/page_id=/i) >= 0 )
				{
					//((window.innerWidth - 1024)/2)
					$.post(cmurl+'/clickmap.php', {  x:(evt.pageX - rd_offset),  y:evt.pageY,  savel:escape(document.location.pathname+document.location.search) });
					// Strict Mode - This resolves the url down to only the 'page_id' variable/value in the uri.
					rd_showurl = document.location.pathname+"?page_id="+((uri.search(/&/i)) ? uri.slice((uri.indexOf("=")+1), uri.indexOf("&")) : uri.slice((uri.indexOf("=")+1)) );
					//alert("Ran! Inserting: "+document.location.pathname+document.location.search);
				}
				else
				{
					//alert((window.innerWidth - 1024)/2);
					//alert(document.getElementsByTagName("html").item(0).clientWidth);
					
					$.post(cmurl+'/clickmap.php', {  x:(evt.pageX - rd_offset),  y:evt.pageY,  savel:escape(document.location.pathname) });
					//alert("Ran other...");
				}
				//alert("Save Path: "+document.location.pathname);
			}catch(e){
				var MapImg = new Image();
				MapImg.src = cmurl+'/clickmap.php' + '?x=' + evt.pageX + '&y=' + evt.pageY + '&savel=' + escape(document.location.pathname);
				//alert("Caught an error!");
			}
		}); 
	}; 
	$.fn.stopSaveClicks = function() { 
		$(this).unbind('mousedown.clickmap'); 
	}; 
	$.displayClicks = function(settings) {
		$('<div id="clickmap-overlay"></div>').appendTo('body');
		$('<div id="clickmap-loading"></div>').appendTo('body');
//		alert( document.location.pathname );
				var uri = document.location.search;
				var rd_showurl = "";
				if ( uri.search(/page_id=/i) >= 0 )
				{
					//$.post(cmurl+'/clickmap.php', {  x:evt.pageX,  y:evt.pageY,  savel:escape(document.location.pathname+document.location.search) });
					// Strict Mode - This resolves the url down to only the 'page_id' variable/value in the uri.
					rd_showurl = document.location.pathname+"?page_id="+((uri.search(/&/i)) ? uri.slice((uri.indexOf("=")+1), uri.indexOf("&")) : uri.slice((uri.indexOf("=")+1)) );
					//alert("Ran! Inserting: "+document.location.pathname+document.location.search);
				}
				else
				{
					//$.post(cmurl+'/clickmap.php', {  x:evt.pageX,  y:evt.pageY,  savel:escape(document.location.pathname) });
					rd_showurl = document.location.pathname;
					//alert("Ran other..."+document.location.pathname);
				}
				if ( uri.search(/&showmap=1/i) >= 0 )
				{
					rd_showurl = rd_showurl.replace(/&showmap=1/i, "");
				}
		$.get(cmurl+'/clickmap.php', { showl:escape( rd_showurl ) },  
			function(html) { 
				$(html).appendTo('body');$('#clickmap-loading').remove(); 
			} 
		); 
	}; 
	 
	$.removeClicks = function() { 
	    $('#clickmap-overlay').remove(); 
	    $('#clickmap-container').remove(); 
	};
})(jQuery); 