/////////////////////////////////////////////////////////////////////////
//
// Cross-platform (Mozilla and IE) event functions
//
// @author cboatwright
// @author $Author: jmortuza $
// @version $Revision: 1.13 $
// @date $Date: 2006/02/13 21:41:10 $
//
/////////////////////////////////////////////////////////////////////////

/**
 * The scolling object used to calculate event location
 */
var events_scrollObj = null;

/**
 * Get the current event object. If IE, window.event is returned.
 * 
 * @param e - mozilla's event object
 *
 * @author cboatwright
 */
function EVENTS_getEvent(e)
{
	if (!e) return window.event; else return e;
}

/**
 * Get the source of the current event
 *
 * @param e - mozilla's event object
 *
 * @author cboatwright
 */
function EVENTS_getEventSrcElement(e)
{	
	if (bIe)
	{
		rv = window.event.srcElement;
	}	
	else if (bMozilla)
	{
		//rv = e.currentTarget;
		rv = e.target;
	}
	
	return rv;
}

/**
 * Get the source of the current event using the cross-platform event object
 *
 * @param theEvent - the event object regardless of platform
 *
 * @author cboatwright
 */
function EVENTS_getXEventSrcElement(theEvent)
{
	var rv = null;
	
	if (bIe)
	{
		rv = theEvent.srcElement;
	}	
	else if (typeof(theEvent) != "undefined")
	{
		rv = theEvent.target;
	}
	
	return rv;
}

/**
 * Do not bubble the 'theEvent'
 *
 * @see EVENTS_getEvent(e)
 *
 * @author cboatwright
 */
function EVENTS_cancelBubble(theEvent)
{
	if (bIe) theEvent = window.event;
	
	if (theEvent)
	{
		theEvent.cancelBubble = true;
		if (theEvent.stopPropagation) theEvent.stopPropagation();
	}
}

/**
 * IE uses 'attachEvent' while Mozilla uses 'addEventListener'
 *
 * @param obj - the object to attach the event to
 * @param eventHandlers - [0] index for IE, [1] for others
 * @param callback - the function to trigger
 * 
 * @return true - The function was bound successfully to the event
 * @return false - The function was not bound to the event
 *
 * @author cboatwright
 */
function EVENTS_attachEvent(obj, eventHandlers, callback)
{
	var rv = false;
	
	if (bIe)
	{
		rv = obj.attachEvent(eventHandlers[0], callback);
	}
	else
	{
		rv = obj.addEventListener(eventHandlers[1], callback, false);
	}
	
	return rv;
}

/**
 * @param obj - the object to fire the event 
 * @param eventHandlers - [0] index for IE, [1] for others
 *
 * @author cboatwright
 */
function EVENTS_fireEvent(obj, eventHandlers)
{
	if (bIe)
	{
		obj.fireEvent(eventHandlers[0]);
	}
	else
	{
	}
}

/**
 * @author cboatwright
 */
function EVENTS_detachEvent(obj, eventHandlers, fn)
{
	var rv = false;
	
	if (bIe)
	{
		rv = fn ? obj.detachEvent(eventHandlers[0], fn) 
				: obj.detachEvent(eventHandlers[0]);
	}
	else
	{
		rv = obj.removeEventListener(eventHandlers[1]);
	}
	
	return rv;
}

/**
 * 
 *
 * @return a two-element array containing {x, y} of the event
 */
function EVENTS_getEventCoords(e)
{
	var scollObj = (events_scrollObj != null) ? events_scrollObj : document.body;
	var evt = EVENTS_getEvent(e);
	var rv = new Array();
	rv[0] = (bIe) ? evt.x : evt.pageX;
	rv[1] = (bIe) ? evt.y + scollObj.scrollTop : evt.pageY;
		
	return rv;
}


function EVENTS_placeElementAtEvent(e, element, yOffset, xOffset)
{
	var elementHeight = parseInt(CSS_getHeight(element));
	var windowHeight = parseInt(CSS_getFrameHeight());
	
	var elementWidth = parseInt(CSS_getWidth(element));
	var windowWidth = parseInt(CSS_getFrameWidth());
	
	var coords = EVENTS_getEventCoords(e);
	
	if ((elementHeight + coords[1]) > windowHeight)
	{
		coords[1] = (bIe) 
			? windowHeight - (elementHeight + 10)
			: windowHeight - (elementHeight + 30);
	}

	if ((elementWidth + coords[0]) > windowWidth)
	{
		coords[0] = (bIe) 
			? windowWidth - (elementWidth)
			: windowWidth - (elementWidth + 30);
	}
	
	yOffset = (typeof(yOffset) == "undefined") ? 0 : yOffset;
	xOffset = (typeof(xOffset) == "undefined") ? 0 : xOffset;
	
	CSS_setPosition(element, 
			parseInt(coords[1]) + parseInt(yOffset), -1, 
			parseInt(coords[0]) + parseInt(xOffset), -1);
}