/**
 * used to queue function calls required on window.load
 * written by Simon Willison: www.incutio.com;
 * @param	{function} func	function to run window.onload;
 */
function addLoadEvent( func )
{
	var oldonload = window.onload;
	//if window.onload has no function attached to it...;
	if ( typeof window.onload != 'function' )
	{
		//...attach function;
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

/**
 * add a class to particular element;
 * @param {String} el The element to add the class to;
 * @param {String} value name of class to add;
 */
function addClass( elem, className )
{
	if ( hasClass( elem, className ) ) return;  // already present
    elem['className'] = [elem['className'], className].join(' ');
}

/**
 * removes a class to particular element;
 * @param {String} el The element to add the class to;
 * @param {String} value name of class to add;
 */
function removeClass( elem, className )
{
	var ex = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)', 'g');
	if (!hasClass(elem, className)) return; // not present
	var c = elem['className'];
	elem['className'] = c.replace(ex, ' ');
	if ( hasClass( elem, className ) ) // in case of multiple adjacent;
	{ 
        removeClass( elem, className );
    }
}

/**
 * Replace a class with another class for a given element or collection of elements.
 * If no oldClassName is present, the newClassName is simply added.
 * @param {String/HTMLElement/Array} el The element or collection to remove the class from;
 * @param {String} oldClassName the class name to be replaced;
 * @param {String} newClassName the class name that will be replacing the old class name;
 */
function replaceClass( el, oldClassName, newClassName ) 
{
	var ex = new RegExp('(?:^|\\s+)' + oldClassName + '(?:\\s+|$)', 'g');
	el['className'] = el['className'].replace( ex, ' ' + newClassName + ' ' );
	if ( hasClass( el, oldClassName ) ) // in case of multiple adjacent;
	{ 
	   replaceClass(el, oldClassName, newClassName);
	}
}

/**
 * Determines whether an HTMLElement has the given className
 * @param {String} el The element to add the class to;
 * @param {String} value name of class to add;
 */
function hasClass( el, className )
{
	var ex = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
	return ex.test(el['className']);
}

/**
 *Used to insert a tag after a defined tag (in the print version)
 *to replicate the printed version of the binder;
 *@param	newElement		tag to insert;
 *@param	targetElement	tag to insert newElement after;  
 */
function insertAfter( newElement, targetElement )
{
	  var parent = targetElement.parentNode;		//get target parent;
	  if ( parent.lastChild == targetElement )		//check if target is lastChild in parent;
	  {	
			parent.appendChild( newElement );		//if true add newElement to end of parent;	  
	  }
	  else
	  {
			parent.insertBefore( newElement, targetElement.nextSibling );	//insert newElement;	  
	  }  
}

/**
 * the ubiquitous dollar function;
 * @returns	{String}	ref to the requested elements/elements;
 */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}