/**
 * Event-related utilities which build on the YUI events module.
 */

WOL.util.Evt = {
   /**
    * <p>Given a tag name and an event, returns either:</p>
    * 
    * <ul>
    * <li>The target element of that event if the target element's tag name is
    * identical to the specified tag name, or</li>
    * <li>The first ancestor of the event's target element whose tag name is identical
    * to the specified tag name, or</li>
    * <li><code>false</code> if neither the target element nor any of its ancestors in the DOM
    * match the specified tag name.</li>
    * </ul>
    *
    * <p>An optional boolean third argument specifies whether to prevent the default action of the
    * event (e.g., if a link were clicked, whether to prevent following the link). Default when not
    * supplied is <code>false</code>.</p>
    *
    * @param {Event} e The event to work with.
    * @param {String} tagName The (case-insensitive) tag name to look for.
    * @param {Boolean} preventDefault Whether to prevent the default action of the event; this is
    * optional and assumed <code>false</code> if not given.
    * @type HTMLElement
    */
   getTargetByTagName: function(e, tagName, preventDefault) {
      var target = Evt.getTarget(e);
      if(typeof(preventDefault) != 'undefined') {
         Evt.preventDefault(e);
      };
      tagName = tagName.toUpperCase(); // Allow lower-case tag names to be passed in.
      if(target.nodeType == 3) { // In case we're starting out in a TextNode.
         target = target.parentNode;
      };
      while(target.nodeName != tagName) {
         if(target.nodeType == 9) {
            // We're at the root node of the document without a match; bail out.
            return false;
         };
         target = target.parentNode;
      };
      return target;
   },
   
   
   /* Remainder of code here comes from the
    * improved onload script by Dean Edwards:
    * http://dean.edwards.name/weblog/2006/06/again/
    * 
    * This version registers functions in an array and
    * loops through them at the proper time, executing them
    * in the order they were registered.
    */
   
   /**
    * Adds an event listener for a given function to fire on page load.
    * 
    * @param {Function} func the function to fire on page load.
    */
   addOnLoad: function(func) {
      WOL.util.Evt.onLoadFunctions[WOL.util.Evt.onLoadFunctions.length] = func;
   },
   
   /**
    * The list of functions to execute on load.
    * 
    * @type Array
    */
   onLoadFunctions: [],

   /**
    * The one function which actually does run on load,
    * and calls all other functions which have been
    * registered.
    *
    * @type void
    */
   init: function() {
      // quit if this function has already been called
      if (arguments.callee.done) {
         return;
      };
      
      // flag this function so we don't do the same thing twice
      arguments.callee.done = true;
      
      // kill the timer
      if (_timer) {
         clearInterval(_timer);
         _timer = null;
      };
      
      for(var i=0, max=WOL.util.Evt.onLoadFunctions.length; i<max; i++) {
         WOL.util.Evt.onLoadFunctions[i].call();
      };
   }
};

/* for Mozilla */
if (document.addEventListener) {
   document.addEventListener("DOMContentLoaded", WOL.util.Evt.init, false);
}

/*
 * Remainder of file is from Dean's onload solution, and ensured that
 * WOL.util.Evt.init is called on load.
 */

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
WOL.util.Evt.init(); // call the onload handler
}
};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
   var _timer = setInterval(function() {
                               if (/loaded|complete/.test(document.readyState)) {
                                  WOL.util.Evt.init(); // call the onload handler
                               }
                            }, 10);
}

/* for other browsers */
window.onload = WOL.util.Evt.init;
