/*
 * Redirects the browser to the URL
 * of the first descendant `a` tag
 * of `el`.  `el` is a document node.
 * If no `a`s are found (or if they
 * don't have the `href` attribute),
 * then no redirection occurs.
 *
 * Copyright (C) 2007 By Ryan Timmons
 */
var leapToFirstChildA = function(el)
{
    if ( null == el )
        return;
    
    var current = el.firstChild;
    // This exploits the "linked list"
    // nature of sibling elements
    // in the HTML Document Tree.
    while (     current             != null
           // JavaScript "short-circuits"
           // evaluation.  We would have the
           // JavaScript equivalent of a
           // "null-pointer exception" if it
           // didn't.
           &&   current.href        == null
           &&   current.nextSibling != null )
        current = current.nextSibling;
    if ( current != null )
        window.location = current.href;
}; // leapToFirstChildA

window.onload = function()
{
    var elts = document.getElementsByTagName('li');
    for ( key in elts )
    {
        var children = elts[key].childNodes;
        // determine if a link (<a> tag) is the first- or second-child
        if ( children != null && (   (children.length > 1 && children[1].tagName == "A")
                                  || (children.length > 2 && children[2].tagName == "A")) )
        {
            // if so, then trigger redirection to that <a> tag upon
            // clicking on our <li>
            elts[key].onclick = function()
            {
                leapToFirstChildA(this);
            };
        }
    }
};

