﻿var AP_POSITION = function()
{
	this.documentRoot = 
		(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body';

	if(window.navigator.userAgent.indexOf('MSIE')>-1) {
		this.showWidth	= function(){return document[this.documentRoot].clientWidth;}
		this.showHeight	= function(){return document[this.documentRoot].clientHeight;}
		this.pageLeft	= function(){return document[this.documentRoot].scrollLeft;}
		this.pageTop	= function(){return document[this.documentRoot].scrollTop;}
		this.get_x		= get_xDesperado;
		this.get_y		= get_yDesperado;
		this.get_mouseX	= function(){return document[this.documentRoot].scrollLeft + event.clientX;}
		this.get_mouseY	= function(){return document[this.documentRoot].scrollTop + event.clientY;}
	} else {
		this.showWidth	= function(){return window.innerWidth;}
		this.showHeight	= function(){return window.innerHeight;}
		this.pageTop	= function(){return window.pageXOffset;}
		this.pageLeft	= function(){return window.pageYOffset;}
		this.get_x		= get_xStandard;
		this.get_y		= get_yStandard;
		this.get_mouseX	= function(evt){return evt.pageX;}
		this.get_mouseY	= function(evt){return evt.pageY;}
	}

	function get_xStandard(el)
	{
		var x = 0;
		while(el && el.tagName.toUpperCase() != 'HTML') {
			x	+= el.offsetLeft;
			el	= el.offsetParent;
		}
		return x;
	}

	function get_yStandard(el)
	{
		var y = 0;
		while(el && el.tagName.toUpperCase() != 'HTML') {
			y	+= el.offsetTop;
			el	= el.offsetParent;
		}
		return y;
	}

	function get_xDesperado(el)
	{
		return el.getBoundingClientRect().left + 
			document[this.documentRoot].scrollLeft - 2;
	}
	
	function get_yDesperado(el)
	{
		return el.getBoundingClientRect().top + 
			document[this.documentRoot].scrollTop - 2;
	}
}

AP_POSITION.prototype =
{
	set_x:function(el, x)
	{
		var parentObj = (!!el.offsetParent) ? el.offsetParent : el;
		var parentX = this.get_x(parentObj);
		
		el.style.left = x - parentX + 'px';
	},
	set_y:function(el, y)
	{
		var parentObj = (!!el.offsetParent) ? el.offsetParent : el;
		var parentY = this.get_y(parentObj);
		
		el.style.top = y - parentY + 'px';
	},
	is_inArea:function(el, point)
	{
		var x	= this.get_x(el);
		var y	= this.get_y(el);
		var rect	= {x:[], y:[]};
		
		rect['x'][0]	= x;
		rect['x'][1]	= x + el.offsetWidth;
		rect['y'][0]	= y;
		rect['y'][1]	= y + el.offsetHeight;
		
		var inX = rect['x'][0] < point[0] && point[0] < rect['x'][1];
		var inY = rect['y'][0] < point[1] && point[1] < rect['y'][1];
		return inX && inY
	}
}

var Pos = new AP_POSITION();