// utility functions
// 2008-03-23 created MET.

//---------------------------------------------------------------------------

(function (libname) {
//---------------------------------------------------------------------------

var g_dbg = false; // set true to issue debug alerts

var o_lib =
{
//--------------------------------

isSupported: function ()
{
	if 
	(
		document.getElementsByTagName 
		&& 
		document.getElementById
	)
		return true;
	
	return false;
},
//--------------------------------

// Queue up a function to be executed after all existing
// 'onload' functions.
addLoadEvent: function (func)
{
	var old_onload = window.onload;
	if (typeof old_onload == 'function')
	{
		window.onload = function()
		{
			old_onload();
			func();
		}
	}
	else
	{
		window.onload = func;
	} 
},
//---------------------------------------------------------------------------

// add a class to an element specified by id or object.
// returns null for invalid input, or className otherwise
addClass: function (el, classname)
{
	if (!(el = o_lib.$(el)))
		return null;

	if (typeof classname != 'string')
		return null;

	if (!el.className || /^\s*$/.test(el.className))
		el.className = classname;
	else
	{
		reClass = new RegExp('\\b' + classname + '\\b');
		if (!reClass.test(el.className))
		{
			el.className += ' ' + classname;
		}
	}

	return el.className;
},
//---------------------------------------------------------------------------

// the standard 'dollar' function: return an object or array of objects, 
// given one or more ids or element objects.
// If input argument is neither string nor object, return null for that element.
$: function ()
{
	if (!document.getElementById)
		return null;

	if (arguments.length < 1)
		return null;

	if (arguments.length == 1)
	{
		if (arguments[0] instanceof Array)
		{
			var a_in = arguments[0];
			var a_out = [];
			for (var i = 0, len = a_in.length; i < len; i++)
				a_out[a_out.length] = arguments.callee(a_in[i]);

			return a_out;
		}
		else
		{
			var el = arguments[0];

			if (typeof el == 'string')
				return document.getElementById(el);
			else if (typeof el == 'object')
				return el;
			else
				return null;
		}
	}

	var a_els = [];

	for (var i = 0, len = arguments.length; i < len; i++)
		a_els[a_els.length] = arguments.callee(arguments[i]);

	return a_els;
},
//---------------------------------------------------------------------------

// does not return a 'live' nodeList, just an array of elements
getElementsByClassName: function (o_root, classname)
{
	var re = new RegExp('\\b' + classname + '\\b');
	var o_nodes = o_root.getElementsByTagName('*');

	// this won't be a live nodeList.
	var a_matches = [];
	for (var i = 0, o_node; (o_node = o_nodes[i]); ++i)
		if (o_node.nodeType == 1 
			&& o_node.className 
			&& re.test(o_node.className))
		{
			a_matches[a_matches.length] = o_node;
		}

	return a_matches;
},
//---------------------------------------------------------------------------

_last_entry: null // so all the others can have trailing commas!
//---------------------------------------------------------------------------

}; // end of MET

// execute this code immediately:

	// library may already have been started elsewhere:
	if (!window[libname])
		window[libname] = {};

	// add library functions to namespace:
	for (var func in o_lib)
		if (typeof o_lib[func] == 'function')
			window[libname][func] = o_lib[func];

	if (g_dbg)
	{
		var msg = '';
		for (var prop in window[libname])
		{
			if (window[libname].hasOwnProperty(prop))
			{
				msg += '\n' + prop;
			}
		}
		alert('Library contains: ' + msg);
	}


})('MET'); // execute immediately
//---------------------------------------------------------------------------

// end of file
