/* ===============BEGIN BSD LICENSED PORTION============= */
/*
Copyright (c) 2007, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.2.0
*/

/**
  * @function getEvent
  * @param {Event} e the event parameter from the handler
  * @return {Event} the event 
*/
function getEvent(e) {
    var ev = e || window.event;
    return ev;
}
/**
  * Returns the event's target element
  * @function getTarget
  * @param {Event} ev the event
  * @return {HTMLElement} the event's target
  * 
  */
function getTarget(ev) {
	var node = ev.target || ev.srcElement;
	/**
     * In some cases, some browsers will return a text node inside
     * the actual element that was targeted.  This normalizes the
     * return value for getTarget and getRelatedTarget.
	 */
	if (node && 3 == node.nodeType) {
	    return node.parentNode;
	} else {
	    return node;
	}	
}
/**
 * Returns a array of HTMLElements that pass the test applied by supplied boolean method.
 * For optimized performance, include a tag and/or root node when possible.
 * @function getElementsBy
 * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
 * @param {String} tag (optional) The tag name of the elements being collected
 * @param {HTMLElement} root (optional) The HTMLElement to use as the starting point 
 * @return {Array} Array of HTMLElements
 */
function getElementsBy(method, tag, root) {
    tag = tag || '*';
    
    var nodes = [];
    
    if (!root) {
        root = document;
    }
    
    var elements = root.getElementsByTagName(tag);
    
    if ( !elements.length && (tag == '*' && root.all) ) {
        elements = root.all; // IE < 6
    }
    
    for (var i = 0, len = elements.length; i < len; ++i) {
        if ( method(elements[i]) ) { nodes[nodes.length] = elements[i]; }
    }
    
    return nodes;
}

/**
 * Determines whether an HTMLElement has the given className.
 * @function hasClass
 * @param {HTMLElement} el The element or collection to test
 * @param {String} className the class name to search for
 * @return {Boolean} A boolean value
 */
function hasClass(el, className) {
    var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');                     
    return re.test(el['className']);	
}

/**
 * Returns a array of HTMLElements with the given class.
 * For optimized performance, include a tag and/or root node when possible.
 * @function getElementsByClassName
 * @param {String} className The class name to match against
 * @param {String} tag (optional) The tag name of the elements being collected
 * @param {String | HTMLElement} root (optional) The HTMLElement to use as the starting point 
 * @return {Array} An array of elements that have the given class name
 */
function getElementsByClassName(className, tag, root) {
    var method = function(el) { return hasClass(el, className); };
    return getElementsBy(method, tag, root);
}

/* ===============END BSD LICENSED PORTION============= */

