// A set of utility functions for performing various tasks in a browser-agnostic fasion.
// Date C: 11-28-2007

// Public interface.
var MakeXMLHttpRequest

var KeyDown
var KeyPressed
var KeyReleased

var LeftMouseDown
var LeftMousePressed
var LeftMouseReleased

var UpdateInput

// The implementation is wrapped in a function so it won't pollute the global namespace.
function UTIL_JS_INITIALIZE()
{
	// A Browser-agnostic function for returning an XMLHttpRequest object.
	MakeXMLHttpRequest = function()
	{
		try
		{
			// Firefox, Opera 8.0+, Safari 
			return new XMLHttpRequest()
		}
		catch (e)
		{
			// Internet Explorer
			try
			{
				return new ActiveXObject("Msxml2.XMLHTTP")
			}
			catch (e)
			{
				try
				{
					return new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e)
				{
					alert("Your browser does not support AJAX!")
				}
			}
		}

		return null
	}

	// Returns the numerical key code from a onkey**** event.
	function ExtractKeyCodeFromEvent( e )
	{
		if( window.event ) // IE
		{
			return window.event.keyCode
		}
		else if( e.which ) // Netscape/Firefox/Opera
		{
			return e.which
		}

		return 0
	}

	// The following code handles keeping track of keyboard input.
	var keys = new Array( 256 )
	var oldKeys = new Array( 256 )

	function OnKeyUp( e )
	{
		keys[ ExtractKeyCodeFromEvent( e ) ] = false
	}
	function OnKeyDown( e )
	{
		keys[ ExtractKeyCodeFromEvent( e ) ] = true
	}

	KeyDown = function( c ) { return keys[ c.charCodeAt() ] }
	KeyPressed = function( c ) { return keys[ c.charCodeAt() ] && !oldKeys[ c.charCodeAt() ] }
	KeyReleased = function( c ) { return !keys[ c.charCodeAt() ] && oldKeys[ c.charCodeAt() ] }

	// The following code keeps track of mouse input.
	var leftMouse = false
	var oldLeftMouse = false

	function OnMouseUp( e )
	{
		// There is no cross-browser way for determining which button has been pressed, so we have to hack.
		if( window.event ) {
			// Internet Explorer.
			leftMouse = !(1 == (1 & window.event.button))
		} else if( e.which ) {
			// Everyone else (though this isn't accurate for all browsers).
			leftMouse = !(1 == e.which)
		}
	}

	function OnMouseDown( e )
	{
		if( window.event ) {
			leftMouse = (1 == (1 & window.event.button))
		} else if( e.which ) {
			leftMouse = (1 == e.which)
		}

		// There are ways for us to get a mousedown event without getting a mouseup event
		// (say if the user presses the mouse in the window, and then releases it outside of
		// the window), so we need to be extra careful about detecting that the mouse is up.
		// This only works in IE, though.
		if( window.event )
		{
			var oldOnMouseMove = document.body.onmousemove
			document.onmousemove = function( e )
				{
					if( oldOnMouseMove ) { oldOnMouseMove( e ) }
					leftMouse = (1 == (1 & window.event.button))
					if( !leftMouse ) { document.onmousemove = oldOnMouseMove }
				}
		}
	}

	LeftMouseDown = function() { return leftMouse }
	LeftMousePressed = function() { return leftMouse && !oldLeftMouse }
	LeftMouseReleased = function() { return !leftMouse && oldLeftMouse }

	// Should be called once per frame.
	// We don't automatically add it through the dom because it needs to be called at the end of the user's update function.
	UpdateInput = function()
	{
		for( var i = 0; i < 256; i++ )
		{
			oldKeys[i] = keys[i]
		}
		
		oldLeftMouse = leftMouse
	}

	// Set everything up.
	function InitializeUtils()
	{
		var oldOnKeyUp = document.onkeyup; document.onkeyup = function( e ) { if( oldOnKeyUp ) { oldOnKeyUp( e ) } OnKeyUp( e ) }
		var oldOnKeyDown = document.onkeydown; document.onkeydown = function( e ) { if( oldOnKeyDown ) { oldOnKeyDown( e ) } OnKeyDown( e ) }

		var oldOnMouseUp = document.onmouseup; document.onmouseup = function( e ) { if( oldOnMouseUp ) { oldOnMouseUp( e ) } OnMouseUp( e ) }
		var oldOnMouseDown = document.onmousedown; document.onmousedown = function( e ) { if( oldOnMouseDown ) { oldOnMouseDown( e ) } OnMouseDown( e ) }
	}
	var oldOnLoad = window.onload; window.onload = function() { if( oldOnLoad ) { oldOnLoad() } InitializeUtils() }
}
UTIL_JS_INITIALIZE()
// We should try just calling the function declaration.
