//
// Box.js
//
// Copyright 2005 by Anthony Howe.  All rights reserved.
//

function getBorderWidths(obj)
{
	var widths = { top: 0, right: 0, bottom: 0, left: 0 };

	// Firefox kludge. Not sure if this is a problem
	// with mouse coordinate reporting or in computing
	// the offsetTop of the object and failure of
	// taking into account the border-top widths.
	if (window.getComputedStyle != null) {
		for ( ; obj != null; obj = obj.offsetParent) {
			var css = window.getComputedStyle(obj, '');

			widths.top += parseInt(css.borderTopWidth);
			widths.left += parseInt(css.borderLeftWidth);
			widths.bottom += parseInt(css.borderBottomWidth);
			widths.right += parseInt(css.borderRightWidth);
		}
	}

	return widths;
}

function getOffsetTop(obj)
{
	var val = 0;

	for ( ; obj != null; obj = obj.offsetParent) {
		val += obj.offsetTop;

		if (window.getComputedStyle != null) {
			var css = window.getComputedStyle(obj, '');
			val += 2 * parseInt(css.borderTopWidth);
		}
	}

	return val;
}

function getOffsetLeft(obj)
{
	var val = 0;

	for ( ; obj != null; obj = obj.offsetParent) {
		val += obj.offsetLeft;

		if (window.getComputedStyle != null) {
			var css = window.getComputedStyle(obj, '');
			val += 2 * parseInt(css.borderLeftWidth);
		}
	}

	return val;
}

function pageY(y)
{
	if (0 < document.body.scrollTop)
		y += document.body.scrollTop;
	else if (0 < document.documentElement.scrollTop)
		y += document.documentElement.scrollTop;

	return y;
}

function pageX(x)
{
	if (0 < document.body.scrollLeft)
		x += document.body.scrollLeft;
	else if (0 < document.documentElement.scrollLeft)
		x += document.documentElement.scrollLeft;

	return x;
}

function containsY(el, y)
{
//	y = pageY(y);

	var elTop = getOffsetTop(el);

//alert(y+' '+elTop+' '+ (elTop + el.offsetHeight));
	if (y < elTop || elTop + el.offsetHeight <= y) {
		return false;
	}

	return true;
}

function containsX(el, x)
{
//	x = pageX(x);

	var elLeft = getOffsetLeft(el);

	if (x < elLeft || elLeft + el.offsetWidth <= x) {
		return false;
	}

	return true;
}

function contains(el, x, y)
{
	return containsY(el, y) && containsX(el, x);
}

function getEvent(this_is, e)
{
	// IE doesn't pass the event object to the handler.
	// Have to get it from the current window object.
	//
	if (e == null) {
		e = window.event;
		e.target = e.srcElement;
		e.currentTarget = this_is;
		e.relatedTarget = e.toElement;

		// IE mouse event coordinates offset by (+2, +2).
		e.pageY = pageY(e.clientY-2);
		e.pageX = pageX(e.clientX-2);
	}

	return e;
}

function Box(x, y, w, h)
{
	this.offsetTop = y;
	this.offsetLeft = x;
	this.offsetWidth = w;
	this.offsetHeight = h;
}

function boxToString(el)
{
	var top = getOffsetTop(el);
	var left = getOffsetLeft(el);

	return "box=("+left+", "+top+", "+(left+el.offsetWidth)+", "+(top+el.offsetHeight)+')';
}

/////////////////////////////////////////////////////////////
// END
/////////////////////////////////////////////////////////////

