//////////////////////////////////////////////////////////////////////////////////////////////
//
//		Получение координат мыши и заданного эл-та
//
//////////////////////////////////////////////////////////////////////////////////////////////
/*

Coordinates.zone( event , elNode ) = 				Сектор элемента над которым в данный момент курсор
	{
		left	: 1 | 0,
		top		: 1 | 0,
		center	: 1 | 0,
		middle	: 1 | 0,
		sector	: 1 | 0
	}
	default null;


Coordinates.mouse( event , elNode || null) = 		Координаты мыши относительно элемента (если задан) или документа
	{
		x	: Number,
		y	: Number
	}
	default null;


Coordinates.element( elNode ) = 		Координаты элемента относительно документа
	{
		x	: Number,
		y	: Number
	}
	default null;

*/

/******************************************************************************/
var Coordinates = {


  /****************************************************************/
	zone : function(event , el){
		var
			oThis = this,
			rez = new Object();


		if(el.offsetHeight){
			var A = oThis.mouse(event,el);

			if(A){
				var rez = new Object();

				rez.x = A.x;
				rez.y = A.y;

				// Удваиваем масштаб...
				var A0 = {
					x: (el.offsetWidth),
					y: (el.offsetHeight)
				}
				// ... и сдвигаем координаты мыши на 1
				var A1 = {
					x: (A.x * 2 + 1 - A0.x),
					y: (A.y * 2 + 1 - A0.y)
				}

				rez.left = (A1.x < 0 ? 1 : 0);
				rez.top = (A1.y < 0 ? 1 : 0);

				// Получаем модуль
				A1.x = (rez.left ? - A1.x : A1.x);
				A1.y = (rez.top ? - A1.y : A1.y);

				rez.center = (A1.x < A0.x/2 ? 1 : 0);
				rez.middle = (A1.y < A0.y/2 ? 1 : 0);
				rez.sector = ( (A0.y / A0.x) < (A1.y / A1.x) ? 1 : 0);

			}
		}
		return rez;
	},


  /****************************************************************/
	mouse : function( event, el){
		var
			oThis = this,
			position = new Object(),
			elEvent = Events.init(event);

		if(el){
//			var elPos = elementOffset(el);
//			var oMousePos = mousePos(event);

			var oMousePos = oThis.mouse(event);
			var elPos = oThis.element(el);

			position.x = (oMousePos.x - elPos.x);
			position.y = (oMousePos.y - elPos.y);

			/*
			var border = oThis._getBorders(el);
			if(
				( (- border.left) <= position.x && position.x <= (el.offsetWidth - border.left)) &&
				( (- border.top) <= position.y && position.y <= (el.offsetHeight - border.top) )
			){
				return position;
			}/**/
		} else {

			if (isMSIE){
				position.x = elEvent.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
				position.y = elEvent.clientY + document.documentElement.scrollTop + document.body.scrollTop;
			}
			if (isGecko){
				position.x = elEvent.clientX + window.scrollX;
				position.y = elEvent.clientY + window.scrollY;
			}
			/*
			position = {
 				x : mousePosX(event),
 				y : mousePosY(event)
 			};
			/**/
			return position;
		}
		return position;
	},


  /****************************************************************/
	element : function(el){
		var
			oThis = this,
			position = new Object();

		if(el){
			position.x = 0;
			position.y = 0;

			var tempEl = el;
			var tempOffsetEl = el;
			var border = null;

			while(tempEl && tempEl.tagName != 'BODY'){
				if(isMSIE){
					border =  oThis._getBorders(tempEl);
					position.x += border.left;
					position.y += border.top;
				}
				position.x += (tempEl.offsetLeft || 0);
				position.y += (tempEl.offsetTop || 0);

				tempOffsetEl = tempEl;
				tempEl = tempEl.offsetParent;

				while (tempOffsetEl && tempOffsetEl != tempEl){
					if(isGecko){
						border =  oThis._getBorders(tempOffsetEl);
						position.x += border.left;
						position.y += border.top;
					}

					tempOffsetEl = tempOffsetEl.parentNode;

					if(tempOffsetEl){
						position.x -= (tempOffsetEl.scrollLeft || 0);
						position.y -= (tempOffsetEl.scrollTop || 0);
					}
				}

			}
			if(tempEl){
				border =  oThis._getBorders(tempEl);
				position.x += border.left;
				position.y += border.top;
			}
			position.x += document.body.scrollLeft;
			position.y += document.body.scrollTop;

			if (isMSIE && !window.opera){
					position.x += document.documentElement.scrollLeft;
					position.y += document.documentElement.scrollTop;
			}

			return position;
		}
		return null;
	},




  /****   Внутренние методы класса   ****/

  /****************************************************************/
	_getBorders : function(el){
		var border = new Object();
		border.top = 0;
		border.left = 0;

		if(window.getComputedStyle){
			border.top = parseInt(el.style.borderTopWidth || window.getComputedStyle(el,null).borderTopWidth) || 0;
			border.left = parseInt(el.style.borderLeftWidth || window.getComputedStyle(el,null).borderLeftWidth) || 0;
		} else {
			border.top = (el.clientTop || 0);
			border.left = (el.clientLeft || 0);
		}
		return border;
	}

};
/******************************************************************************/

/******************************************************************************/
var Coordinates11 = new function(){

	var isMSIE = document.attachEvent != null;
	var isGecko = !document.attachEvent && document.addEventListener;
	var elEvent = null;

  /****   Внешние методы класса   ****/

  /****************************************************************/
	this.zone = function(event , el){
		var rez = null;

		initEvent(event);

		if(el.offsetHeight){
			var A = Coordinates.mouse(event,el);

			if(A){
				var rez = new Object();

				rez.x = A.x;
				rez.y = A.y;

				// Удваиваем масштаб...
				var A0 = {
					x: (el.offsetWidth),
					y: (el.offsetHeight)
				}
				// ... и сдвигаем координаты мыши на 1
				var A1 = {
					x: (A.x * 2 + 1 - A0.x),
					y: (A.y * 2 + 1 - A0.y)
				}

				rez.left = (A1.x < 0 ? 1 : 0);
				rez.top = (A1.y < 0 ? 1 : 0);

				// Получаем модуль
				A1.x = (rez.left ? - A1.x : A1.x);
				A1.y = (rez.top ? - A1.y : A1.y);

				rez.center = (A1.x < A0.x/2 ? 1 : 0);
				rez.middle = (A1.y < A0.y/2 ? 1 : 0);
				rez.sector = ( (A0.y / A0.x) < (A1.y / A1.x) ? 1 : 0);
			}
		}
		return rez;
	}


  /****************************************************************/
	this.mouse = function( event, el){
		var position = new Object();

		initEvent(event);

		if(el){
			var mousePos = Coordinates.mouse(event);
			var elPos = Coordinates.element(el);

			position.x = (mousePos.x - elPos.x);
			position.y = (mousePos.y - elPos.y);

			var border = _getBorders(el);
			if(
				( (- border.left) <= position.x && position.x <= (el.offsetWidth - border.left)) &&
				( (- border.top) <= position.y && position.y <= (el.offsetHeight - border.top) )
			){
				return position;
			}
		} else {
			if (isMSIE){
				position.x = elEvent.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
				position.y = elEvent.clientY + document.documentElement.scrollTop + document.body.scrollTop;
			}
			if (isGecko){
				position.x = elEvent.clientX + window.scrollX;
				position.y = elEvent.clientY + window.scrollY;
			}
			return position;
		}
		return null;
	}


  /****************************************************************/
	this.element = function(el){
		if(el){
			var position = new Object();
				position.x = 0;
				position.y = 0;

			var tempEl = el;
			var tempOffsetEl = el;
			var border = null;

			while(tempEl.tagName != 'BODY'){
				if(isMSIE){
					border = _getBorders(tempEl);
					position.x += border.left;
					position.y += border.top;
				}
				position.x += tempEl.offsetLeft;
				position.y += tempEl.offsetTop;

				tempOffsetEl = tempEl;
				tempEl = tempEl.offsetParent;

				while (tempOffsetEl != tempEl){
					if(isGecko){
						border = _getBorders(tempOffsetEl);
						position.x += border.left;
						position.y += border.top;
					}
					tempOffsetEl = tempOffsetEl.parentNode;
					position.x -= tempOffsetEl.scrollLeft;
					position.y -= tempOffsetEl.scrollTop;
				}
			}

			border = _getBorders(tempEl);
			position.x += border.left;
			position.y += border.top;

			if (isMSIE){
				position.x += document.body.scrollLeft + document.documentElement.scrollLeft;
				position.y += document.body.scrollTop + document.documentElement.scrollTop;
			}
			if (isGecko){
				position.x += window.scrollX;
				position.y += window.scrollY;
			}
			return position;
		}
		return null;
	}




  /****   Внутренние методы класса   ****/

  /****************************************************************/
	function _getBorders(el){
		var border = new Object();
		border.top = 0;
		border.left = 0;

		if(window.getComputedStyle){
			border.top = parseInt(el.style.borderTopWidth || window.getComputedStyle(el,null).borderTopWidth);
			border.left = parseInt(el.style.borderLeftWidth || window.getComputedStyle(el,null).borderLeftWidth);
		} else {
			border.top = (el.clientTop || 0);
			border.left = (el.clientLeft || 0);
		}
		return border;
	}
  /****************************************************************/
 	function initEvent(evt){
		elEvent = null;
		if (isMSIE)		elEvent = window.event;
		if (isGecko)	elEvent = evt;
	}

  /****************************************************************/
}
/******************************************************************************/

