/**
*	events.js
*
*	Provides a mechanism to add event listeners
*	to all browsers until they all support the
*	W3C standard.
*
*	@author James Edwards & Cameron Adams
*	@copyright &copy;2006 SitePoint Pty. Ltd.
*	@resource "The Javascript Anthology: 101 Essential Tips, Tricks and Hacks" ISBN:0-9752402-6-9 Page:234-244
*/

/**
*
*/
function attachEventListener(AnObject, AnEvent, AFunction, Capture) {

	if (typeof AnObject.addEventListener != "undefined") {	// W3C Compliant
		AnObject.addEventListener(AnEvent, AFunction, Capture);
	}
	else if (typeof AnObject.attachEvent != "undefined") {  // Internet Explorer
		AnObject.attachEvent("on" + AnEvent, AFunction);
	}
	else { // Old Method
		AnEvent = "on" + AnEvent;

		if (typeof AnObject[AnEvent] == "function") {
			var CurrentListener = AnObject[AnEvent];

			AnObject[AnEvent] = function() {
				CurrentListener();
				return AFunction();
			}
		}
		else {
			AnObject[AnEvent] = AFunction;
		}
	}

}

/**
*
*/
function detachEventListener(AnObject, AnEvent, AFunction, Capture) {

	if (typeof AnObject.removeEventListener != "undefined") { // W3C Compliant
		AnObject.removeEventListener(AnEvent, AFunction, Capture);
	}
	else if (typeof AnObject.detachEvent != "undefined") { // Internet Explorer
		AnObject.detachEvent("on" + AnEvent, AFunction);
	}
	else { // Old Method
		AnObject["on" + AnEvent] = null;
	}

}

/**
*
*/
function queryEventTarget(EventObj) {
	var AnObject = null;

	if (typeof EventObj.target != "undefined")
		AnObject = EventObj.target;
	else AnObject = EventObj.srcElement;

	while( (AnObject.nodeType == 3) && (AnObject.parentNode != null) )
		AnObject = AnObject.parentNode;

	return AnObject;
}

/**
*
*/
function stopDefaultAction(EventObj) {

	EventObj.returnValue = false;

	if (typeof EventObj.preventDefault != "undefined") {
		EventObj.preventDefault();
	}

}

/**
*
*/
function stopEvent(EventObj) {

	if (typeof EventObj.stopPropagation != "undefined") {
		EventObj.stopPropagation();
	}
	else {
		EventObj.cancelBubble = true;
	}

}

/**
*
*/
function scrollPosition() {
	var Position = [0, 0];

	if ( typeof window.pageYOffset != 'undefined') {
		Position = [
			window.pageXOffset,
			window.pageYOffset 
		];
	}
    else if ( (typeof document.documentElement.scrollTop != 'undefined') &&
		(document.documentElement.scrollTop > 0) ) {
		Position = [
			document.documentElement.scrollLeft,
			document.documentElement.scrollTop
		];
	}
	else if ( typeof document.body.scrollTop != 'undefined' ) {
		Position = [
			document.body.scrollLeft,
			document.body.scrollTop
		];
	}

	return Position;
}

/**
*
*/
function elementPosition(AnElement) {
	var Position = [0, 0];

	OffsetTop = 0; OffsetLeft = 0;
	while ( AnElement != null ) {
		OffsetTop += AnElement.offsetTop;
		OffsetLeft += AnElement.offsetLeft;
		AnElement = AnElement.offsetParent;
	}	

	Position = [
		OffsetLeft,
		OffsetTop
	];

	return Position;
}
