
/**
 * Provides static utility methods.
 */
function Util () {
}

/**
 * Registers a function to be called when the page loads.
 */
Util.registerOnload = function ( func ) {
	Util.onloadFunctions[ Util.onloadFunctions.length ] = func;
}
Util.onloadFunctions = [];

/**
 * @private
 */
Util.load = function () {
	for ( var i=0; i < Util.onloadFunctions.length; i++ )
		Util.onloadFunctions[ i ]();	
}

/**
 * Provides inheritance from the base class to the subclass. A class must be derived from BaseObject to be extended.
 * @param	newClass Object
 * @param	baseClass Object
 */
Util.extend = function ( newClass, baseClass ) {
	var temp = function () {}
	temp.prototype = baseClass.prototype;
	newClass.prototype = new temp();
	newClass.prototype.constructor = newClass;  // This is an intrinsic property on instances of the Function class.
	newClass.superConstructor = baseClass;
}

/**
 * Returns a unique ID.
 * @return Integer
 */
Util.getUID = function () {
	return Util.nextID++;
}
Util.nextID = 1;

/**
 * Registers an object returning an ID for later lookup.
 * @param	instance Object
 * @return Integer
 */
Util.registerInstance = function ( instance ) {
	var id = Util.getUID();
	Util.classInstances[ id ] = instance;
	return id;
}
Util.classInstances = [];

/**
 * Returns an object registered with Util.registerInstance.
 * @param	id Integer
 */
Util.getInstance = function ( id ) {
	return Util.classInstances[ id ];
}

/**
 * Converts a function's arguments object into an array so it can be more easily manipulated.
 * @param	argumentsObject Object		The arguments object of a function.
 * @param?	startIndex Integer		The index at which to start copying. Arguments before this index won't be in the returned array.
 * @return Array
 */
Util.argumentsToArray = function ( argumentsObject, startIndex ) {
	// The arguments object is not actually an array, making manipulation diffcult.
	var args = [];
	for ( var i=startIndex || 0; i < argumentsObject.length; i++ )
		args[ args.length ] = argumentsObject[ i ];
	// BOZO - A more elegant solution may be:  Array.prototype.slice.apply( args );
	return args;
}

/**
 * Returns a delegate function that will invoke a method on a class instance.
 * @param	instance Object
 * @param?	methodName String		If null the instance is assumed to be the function to invoke.
 * @return Function
 */
Util.getDelegate = function ( instance, methodName ) {
	var func = new Function ( "var f=arguments.callee; return Util.invokeDelegate(f.instance,f.methodName,f.firstArgs,arguments);" );
	func.instance = instance;
	func.methodName = methodName;
	func.firstArgs = Util.argumentsToArray( arguments, 2 );
	return func;
}

/**
 * @private
 */
Util.invokeDelegate = function ( instance, methodName, firstArgs, argumentsObject ) {
	var args = firstArgs.concat( Util.argumentsToArray(argumentsObject) );
	var func = methodName ? instance[ methodName ] : instance;  // If there is no methodName assume the instance is the function to invoke.
	func.apply( instance, args );
}

/**
 * Trims a character from the beginning and ends of a function.
 * @param	text String
 * @param	character String	If omitted a space is assumed.
 */
Util.trim = function ( text, character ) {
    if ( !text ) return "";
	 if ( !character ) character = " ";
	 text += "";
    while ( text.substring(0, 1) == character )
        text = text.substring( 1, text.length );
    while ( text.substring(text.length - 1, text.length) == character )
        text = text.substring( 0, text.length - 1 );
   return text;
}

/**
 * Sets a cookie.
 * @param	name String
 * @param	value String
 */
Util.prototype.setCookie = function ( name, value ) {
	document.cookie = name + "=" + escape( value ) + "; expires=Thu, 01-Jan-2200 00:00:01 GMT; path=/";
}

/**
 * Gets a cookie.
 * @param	name String
 */
Util.prototype.getCookie = function ( name ) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf( "; " + prefix );
	if ( begin == -1 ) {
		begin = dc.indexOf( prefix );
		if ( begin != 0 ) return "";
	} else
		begin += 2;
	var end = document.cookie.indexOf( ";", begin );
	if ( end == -1 ) end = dc.length;
	return unescape( dc.substring(begin + prefix.length, end) );
}

window.onload = Util.load;
