//------------------------------------------------------------------------
// Name: LW_Events_Handler
// Desc: Handles all the events used.
//------------------------------------------------------------------------
LW_Events_Handler =
{

	//----------------------------------------------------------------------
  // Name: addEvent
  // Desc: Adds an event to an element. If there was one already assigned
	//       it will chain them so that as many as the user wants can be
	//       assigned without problems.
  //----------------------------------------------------------------------
	addEvent: function( element, element_event, event_function )
	{

		// Get the old event handler.
		var old_handler = element[element_event];

		// This function wraps a call to the old function and the new function.
		// We do this so that we can keep assigning event functions to this element.
		var new_function = function( e )
		{

		  // Call the old event handler if there is one.
		  if ( old_handler != null )
			  old_handler( e );

			// Call the new event handler function.
			return event_function( e );

		}

		// Store the new function as this elements event handler.
		element[element_event] = new_function;

	},


	//----------------------------------------------------------------------
  // Name: getCursorPos()
  // Desc: Returns the cursor position in page coordinates.
  //----------------------------------------------------------------------
	getCursorPos: function( e )
	{

		var pos = new LW_Structure_Point();

		if ( !e ) var e = window.event;

		if ( e.pageX || e.pageY )
		{

			pos.X = e.pageX;
			pos.Y = e.pageY;

		}
		else if ( e.clientX || e.clientY )
		{

			pos.X = e.clientX + document.body.scrollLeft;
			pos.Y = e.clientY + document.body.scrollTop;

		}

    // Finally, return the cursor position.
		return pos;

	}

}