/**
 * @author David Pardo: Corunet
 * Run after loading
 */
 
var xOffset,yOffset;
var tempX = 0;
var tempY = 0;

//detect browser
var IE = document.all?true:false
//find the position of the first item on screen and store offsets
	//find the first item on screen (after body)
	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;
	}
/*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 getScrollWidth() {
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
}

function getScrollHeight() {
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
  //  window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getMouseXY(e) {

    // Use Microsoft, not W3C (which is stupid (http://www.quirksmode.org/js/events_properties.html)
    if(!e) var e = window.event;
    var leftclick;
    if(e.which) leftclick = (e.which == 1);
    else if (e.button) leftclick = (e.button == 1); 

    if (leftclick == false) return;

	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 myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	}

	//	var w = getScrollWidth();
	//var h = getScrollHeight();
	
	var screen_x = screen.width;
	var screen_y = screen.height;

  //	var win_x = document.all?document.body.clientWidth:window.innerWidth;
  //	var win_y = document.all?document.body.clientHeight:window.innerHeight; 

	//	var url='/w3perl/resources/heatmap.php?x='+tempX+'&y='+tempY+'&url='+escape(document.location.pathname+document.location.hash+document.location.search)+'&date='+date;
	//	var url='/w3perl/resources/heatmap.php?x='+tempX+'&y='+tempY+'&win_x='+myWidth+'&win_y='+myHeight+'&w='+w+'&h='+h+'&url='+escape(document.location.pathname+document.location.hash+document.location.search);
	var url='/w3perl/resources/heatmap.php?x='+tempX+'&y='+tempY+'&win_x='+myWidth+'&win_y='+myHeight+'&screen_x='+screen_x+'&screen_y='+screen_y+'&url='+escape(document.location.pathname+document.location.hash+document.location.search);
	guardar(url);
	return true;
}

//var x=e.clientX;var y=e.clientY;var w=clickHeatDocument.clientWidth !== undefined?clickHeatDocument.clientWidth:window.innerWidth;var h=clickHeatDocument.clientHeight !== undefined?clickHeatDocument.clientHeight:window.innerHeight;var scrollx=window.pageXOffset === undefined?clickHeatDocument.scrollLeft:window.pageXOffset;var scrolly=window.pageYOffset === undefined?clickHeatDocument.scrollTop:window.pageYOffset;
//params='s='+clickHeatSite+'&g='+clickHeatGroup+'&x='+(x+scrollx)+'&y='+(y+scrolly)+'&w='+w+'&b='+clickHeatBrowser+'&c='+c+'&random='+Date();
function guardar(url){
	var xmlDoc = null ;
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject('Microsoft.XMLHTTP');
	}else {
		xmlDoc = new XMLHttpRequest();
	}
	xmlDoc.open( 'GET', url, true );
	xmlDoc.send( null );
}
