


//extra
if (!console) {

	var console = {

		log: function(text) {},
		info: function(text) {}

	};
}
//extra


var xOffset,yOffset;
var tempX = 0;
var tempY = 0;
var IE;
var firstElement;
var marginLeftExplorer;
var marginTopExplorer;
var starttime;

function statsinit() {
starttime=new Date();
console.log('statsinit');
	//detect browser
	IE = document.all?true:false
	if (!IE) {
		document.captureEvents(Event.MOUSEMOVE)
	}
	//find the position of the first item on screen and store offsets
	//find the first item on screen (after body)
	firstElement=document.getElementsByTagName('body')[0].childNodes[1];
	//find the offset coordinates
	xOffset=findPosX(firstElement);
	yOffset=findPosY(firstElement);
	if (IE){ // In IE there's a default margin in the page body. If margin's not defined, use defaults
		marginLeftExplorer  = parseInt(document.getElementsByTagName('body')[0].style.marginLeft);
		marginTopExplorer   = parseInt(document.getElementsByTagName('body')[0].style.marginTop);
		/*assume default 10px/15px margin in explorer*/
		if (isNaN(marginLeftExplorer)) {marginLeftExplorer=10;}
		if (isNaN(marginTopExplorer)) {marginTopExplorer=15;}
		xOffset=xOffset+marginLeftExplorer;
		yOffset=yOffset+marginTopExplorer;
	}
	/*attach a handler to the onmousedown event that calls a function to store the values*/
	document.onmousedown = getMouseXY;

}

/*Functions*/
/*Find positions*/
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
}

function getMouseXY(e) {
	console.log('getMouseXY');
	if (IE) {
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {
		tempX = e.pageX
		tempY = e.pageY
	}
	tempX-=xOffset;
	tempY-=yOffset;
	//var url='http://ramonantonio.net/stats/save?x='+tempX+'&y='+tempY+'&location='+window.location;
var loc = urlencode(window.location.href.toString());

	var url='http://ramonantonio.net/stats/save/click/'+tempX+'/'+tempY+'/'+loc+'/0';
console.log(url);
	ajad_send2(url);

	return true;
}
function urlencode(str) {
return escape(str)
       .replace(/\+/g, '%2B')
          .replace(/\"/g,'%22')
             .replace(/\'/g, '%27')
             .replace(/\//g, '%3B');

}

var ajad_ndx_script = 0;

function ajad_do (u) {
	// Create new JS element
	var js = document.createElement('SCRIPT');
	js.type = 'text/javascript';
	ajad_ndx_script++;
	js.id = 'ajad-' + ajad_ndx_script;
	js.src = u;

	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild(js);

	return true;
}

function ajad_get (r) {
	// Create URL
	var u = r;


	// Do AJAD
	return ajad_do(u);
}



function ajad_send2(url) {

	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			console.info(transport.responseText);
		}
	});

}

function ajad_send(url) {
	// referrer
	// r = window.location;

	var r = url;
	// send it
	ajad_get(r);

	// remove the last script node.
	document.body.removeChild(document.getElementById('ajad-' + ajad_ndx_script));
	ajad_ndx_script--;
}


function overlay() {
var firstElement=document.getElementsByTagName('body')[0].childNodes[1];
//find the offset coordinates
xOffset=findPosX(firstElement);
yOffset=findPosY(firstElement);
if (IE){ // In IE there's a default margin in the page body. If margin's not defined, use defaults
	var marginLeftExplorer  = parseInt(document.getElementsByTagName('body')[0].style.marginLeft);
	var marginTopExplorer   = parseInt(document.getElementsByTagName('body')[0].style.marginTop);
	/*assume default 10px/15px margin in explorer*/
	if (isNaN(marginLeftExplorer)) {marginLeftExplorer=10;}
	if (isNaN(marginTopExplorer)) {marginTopExplorer=15;}
	xOffset=xOffset+marginLeftExplorer;
	yOffset=yOffset+marginTopExplorer;
}
// add the image element to the dom, absolutely positioned
var style="z-index:100;position:absolute;left:"+xOffset+"px;top:"+yOffset+"px";
var bodytag=document.getElementsByTagName('body')[0]
var newdiv = document.createElement('div');
var currentpage=location.href;
//var loc=currentpage.replace(/\W/g,'');
var loc = urlencode(window.location.href.toString());
var url = 'http://ramonantonio.net/stats/view/'+loc;

newdiv.setAttribute('id','heatmap');
newdiv.innerHTML = '<img src="'+url+'" style="'+style+'" >';
//newdiv.innerHTML = '<img src="'+escapedpage+'.final.png" style="'+style+'" >';
bodytag.appendChild(newdiv);
}

Event.observe(window, 'load', function() { statsinit() });