/**
 * @author David Pardo: Corunet
 * Run after loading
 */

var xOffset,yOffset;
var tempX = 0;
var tempY = 0;

var page_width = 0;
var page_height = 0;

var window_width = 0;
var window_height = 0;

var absX = 0;
var absY = 0;

//detect browser
var 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)
	
	var firstElement=document.getElementById("header");
	GetAbsCoordsForElement("header");
	
	GetPageSize();
	
	//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 = function(e){
	getMouseXY(e);
	GetPageSize();
	GetWindowSize();
}


/*

function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

addEventSimple(document,'click',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) {
		
	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://corpuschristi.ca/empty.map?x='+tempX+'&y='+tempY + '&page_width=' + page_width + '&page_height=' + page_height + '&screen_width=' + screen.width + '&screen_height=' + screen.height + '&host=' + document.location.hostname + '&path=' + document.location.pathname + '&port=' + document.location.port + '&protocol=' + document.location.protocol + '&window_width=' + window_width + '&window_height=' + window_height;

	// var url='http://127.0.0.1:3000/empty.map?x='+tempX+'&y='+tempY + '&page_width=' + page_width + '&page_height=' + page_height + '&screen_width=' + screen.width + '&screen_height=' + screen.height + '&host=' + document.location.hostname + '&path=' + document.location.pathname + '&port=' + document.location.port + '&protocol=' + document.location.protocol + '&window_width=' + window_width + '&window_height=' + window_height;
	
	guardar(url);
	
	return true;
}

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 );
}

function GetPageSize(){
	
	obj=document.getElementById("wrapper");
	page_height = obj.clientHeight;
	
	obj=document.getElementById("header");
	page_width = obj.clientWidth;
	
/*
	alert(obj);
	alert(page_width);

*/	
}

function GetWindowSize(){
	
	obj=document.getElementById("wrapper");
	window_width = obj.clientWidth;
	window_height = obj.clientHeight;
	
}


 function GetAbsCoordsForElement(elName){
	  var obj=document.getElementById(elName);
	  xOffset = getAbsX(obj);
	  yOffset = getAbsY(obj);
}


function getAbsX(obj)
{
	  var leftOffset = 0;
	  if (obj.offsetParent)
	  {
			while (obj.offsetParent)
			{
				  leftOffset += obj.offsetLeft;
				  obj = obj.offsetParent;
			}
	  }
	  else if (obj.x) //for NN4
	  {
			leftOffset = obj.x;
	  }
	  return leftOffset;
}

    
function getAbsY(obj)
{
	  var topOffset = 0;
	  if (obj.offsetParent)
	  {
			while (obj.offsetParent)
			{
				  topOffset += obj.offsetTop;
				  obj = obj.offsetParent;
			}
	  }
	  else if (obj.y) // for NN4
	  {
			topOffset = obj.y;
	  }
	  return topOffset;
}