addEvent(window, "load", makeDDs);
addEvent(window, "load", ss_fixAllLinks);

var currentMenus = [];

var menuptr;
var timeoutFlag;
var timeoutLength = 750;
var menu_open_flag = 0;

var ptaffmenu;
var ptafftheTop = 0;
var ptaffold = ptafftheTop;
var ptaffpos;

function makeDDs() { /* {{{ */
    // We don't actually need createElement, but we do
    // need good DOM support, so this is a good check.
    if (!document.createElement) {
        return;
    };

    uls = document.getElementsByTagName('ul');
    for (uli = 0; uli < uls.length; uli++) {
        ul = uls[uli];
        if (ul.nodeName.toLowerCase() == "ul" && ul.className == "menubar") {
            menuptr = ul;
            processULDD(ul,0);
        };
    };

document.getElementById('topmenu').style.zIndex = '1';
}; /* }}} */
function processULDD(ul,isSubUL) { /* {{{ */
    if (!ul.childNodes || ul.childNodes.length == 0) {
        return;
    };
    // Iterate through LIs, which are menu headers in the menu bar
    for (var itemi = 0; itemi < ul.childNodes.length; itemi++) {
        var item = ul.childNodes[itemi];
        if (item.nodeName.toLowerCase() == "li") {
            // Iterate things in this LI: should be an A and a UL
            var a;
            var subul;
            for (var sitemi = 0; sitemi < item.childNodes.length; sitemi++) {
                var sitem = item.childNodes[sitemi];
                switch (sitem.nodeName.toLowerCase()) {
                    case "a":
                        a = sitem;
                        break;
                    case "ul":
                        subul = sitem;
			            processULDD(subul, 1);
                        subul.onmouseout = start_shutdown_timer;
                        subul.onmouseover = stop_shutdown_timer;
                        break;
                };
            };
            if (isSubUL) {
                // this is a UL that is in a list
                if (subul) {
                    // and it has subuls of its own, which means that
                    // it's the head of a submenu
                    associateDD(a, subul);
                    a.className += ' submenuheader';
                    // make the submenu appear to the right of this one
                    subul.style.left = item.offsetLeft + item.offsetWidth + 'px';
                    subul.style.top = item.offsetTop + 'px';
                    subul.style.width = '200px';
                    subul.style.display = 'none';
                }
                else {
                    // it is a leaf, so leave the link alone
                    // and, like the raven said, carrion regardless
                    // without doing anything.
                };
            }
            else {
                // this is the top level UL
                if (subul) {
                    // make this submenu appear *below* this LI
                    associateDD(a,subul);
                    subul.style.left = item.offsetLeft + "px";
                    subul.style.display = "none";
                    tp = DL_GetElementTop(item) + item.offsetHeight;
                    subul.style.top = tp + 'px';
                };
            };
        };
    };
}; /* }}} */
function associateDD(a,ul) { /* {{{ */
    a.onclick = function () {
        menu_open_flag = 1;
        return handle_toggle(ul, 1);
    };
    a.onmouseover = function () {
        stop_shutdown_timer();
        if (menu_open_flag == 1) {
            return handle_toggle(ul, 0);
        };
        return false;
    };
}; /* }}} */
function handle_toggle(ul, force_close) { /* {{{ */
        if (ul.style.display == 'none') {
            // Walk tree to get top level UL, saving ancestors
            var ancestors = [];
            var cn = ul;
            do {
                if (cn.nodeName.toLowerCase() == 'ul') {
                    ancestors[ancestors.length] = cn;
                };
                cn = cn.parentNode;
            } while (cn.className != 'menubar');
            // Undisplay all its children ULs
            hideChildren(cn,cn);
            cn.style.display = 'block';
            // Display all ul's ancestor uls, including ul itself
            for (var idx = 0; idx < ancestors.length; idx++) {
                ancestors[idx].style.display = 'block';
            };
        }
        else if (force_close == 1) {
            // Undisplay all ul's children uls
            hideChildren(ul,ul);
            // Undisplay ul itself
            ul.style.display = 'none';
            // menu was closed, turn off the flag
            menu_open_flag = 0;
        }
        else {
          stop_shutdown_timer();
        };
        return false;
}; /* }}} */
function hideChildren(nd,toplevel) { /* {{{ */
    // Walk all of nd's children, and hide all ULs we find
    if (nd.childNodes && nd.childNodes.length > 0) {
        for (var ndi = 0; ndi < nd.childNodes.length; ndi++) {
            hideChildren(nd.childNodes[ndi], toplevel);
        };
    };
    if (nd.nodeName.toLowerCase() == 'ul' && nd != toplevel) {
        nd.style.display = 'none';
    };
} /* }}} */
function addEvent(obj, evType, fn){ /* {{{ */
    /* adds an eventListener for browsers which support it
        Written by Scott Andrew: nice one, Scott */
    if (window.opera && obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    }
    else if (obj.addEventListener) {
        obj.addEventListener(evType, fn, true);
        return true;
    }
    else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    };
    return false;
} /* }}} */
function DL_GetElementLeft(eElement) { /* {{{ */
// DL_* functions from DHTML Diner:
// http://www.webreference.com/dhtml/diner/realpos1/
    var nLeftPos = eElement.offsetLeft;
    var eParElement = eElement.offsetParent;
    while (eParElement != null) {
        nLeftPos += eParElement.offsetLeft;
        eParElement = eParElement.offsetParent;
    };
    return nLeftPos;
} /* }}} */
function DL_GetElementTop(eElement) { /* {{{ */
// DL_* functions from DHTML Diner:
// http://www.webreference.com/dhtml/diner/realpos1/
    var nTopPos = eElement.offsetTop;
    var eParElement = eElement.offsetParent;
    while (eParElement != null) {
        nTopPos += eParElement.offsetTop;
        eParElement = eParElement.offsetParent;
    };
    return nTopPos;
} /* }}} */
function checkSentience() { /* {{{ */
    if (document.getElementById('topmenu')) {
        ptaffinit();
    }
    else {
        setTimeout('checkSentience()',200);
    };
}; /* }}} */
function ptaffinit() { /* {{{ */
    ptaffmenu = document.getElementById('topmenu');
    ptaffmovemenu();
}; /* }}} */
  function ptaffmovemenu() { /* {{{ */
    if (window.innerHeight) {
      ptaffpos = window.pageYOffset;
    }
    else {
        if (document.documentElement && document.documentElement.scrollTop) {
            ptaffpos = document.documentElement.scrollTop;
        }
        else {
            if (document.body) {
                ptaffpos = document.body.scrollTop;
            };
        };
    };
    if (ptaffpos < ptafftheTop) {
      ptaffpos = ptafftheTop;
    }
    else {
      ptaffpos += 0;
    };
    if (ptaffpos == ptaffold) {
      ptaffmenu.style.top = ptaffpos;
    };
    ptaffold = ptaffpos;
    setTimeout('ptaffmovemenu()',50);
  }; /* }}} */
function start_shutdown_timer() { /* {{{ */
    timeoutFlag = setTimeout('shutdown()', timeoutLength);
}; /* }}} */
function stop_shutdown_timer() { /* {{{ */
    clearTimeout(timeoutFlag);
}; /* }}} */
function shutdown() { /* {{{ */
    hideChildren(menuptr, menuptr);
    menu_open_flag = 0;
}; /* }}} */
function ss_fixAllLinks() { /* {{{ */
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i = 0; i < allLinks.length; i++) {
        var lnk = allLinks[i];
        if (((lnk.href) && (lnk.href.indexOf('#') != -1) && (lnk.href.charAt(lnk.href.length - 1) != '#') && (lnk.href != '')) &&  
            ((lnk.pathname == location.pathname) ||
            ('/' + lnk.pathname == location.pathname)) &&  
            (lnk.search == location.search)) {
                // If the link is internal to the page (begins in #)
                // then attach the smoothScroll function as an onclick
                // event handler
                addEvent(lnk, 'click', smoothScroll);
        };
    };
}; /* }}} */
function smoothScroll(e) { /* {{{ */
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    var target = e.target ? e.target : e.srcElement;
 
    // Make sure that the target is an element, not a text node
    // within an element
    while ((target.nodeType == 3) || (target.nodeType == 4)) {
        target = target.parentNode;
    }
 
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') {
        return true;
    };
    // If in a LI which is a tree-menu, dont scroll
    if (target.parentNode.nodeName.toLowerCase() == 'li') {
        if ((target.parentNode.className.indexOf("aq3open") != -1) || (target.parentNode.className.indexOf("aq3closed") != -1)) {
            return false;
        };
    };
 
    anchor = target.hash.substr(1);
    // Find the correct destination ID
    destinationLink = document.getElementById(anchor);

 
    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) {
        return true;
    };
 
    // Find the destination's position
    var destx = destinationLink.offsetLeft;  
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && (thisNode.offsetParent != document.body)) {
        thisNode = thisNode.offsetParent;
        destx += thisNode.offsetLeft;
        desty += thisNode.offsetTop;
    };
 
    setTimeout('ss_scrollWindow("' + anchor + '", ' + desty + ')', 1000);
 
} /* }}} */
function ss_scrollWindow(anchor, desty) { /* {{{ */
    
   window.scrollTo(0, desty - 30);
} /* }}} */

