/************************************************************************\

Slide Menu based on unordered lists
version 2
Feb 20, 2010

Hang mouse over a menu item to open it, click links to follow them.

Alternative operation - click to open a sumbenu, then click the link again to follow it.
Set ClickDriven = true;

Menus and sumbenues are organised in nested ULs of unlimited depth.
No requirements of ids or class names.

The engine is designed as an easy add-on over any existing CMS.
Can be applied to any list by assigning ul_top, i.e. in WordPress

    ul_top = document.getElementById('pages')
    for (child in ul_top.childNodes)
	if (ul_top.childNodes[child].tagName && ul_top.childNodes[child].tagName == 'UL') {
	  ul_top = ul_top.childNodes[child];
          break;
        }
        
Once you've pinned down the top list, call SlideMenuInit();

If suitable, you can give your top UL the predefined id="ul-top" or "ul_top",
and SlideMenuInit() will find it.

If you want to display debug info, create a textarea and assign slum_debug_window to point to it.
Or give it the predefined id ="slum_debug_window"

written by Piotr Kalachyn (c) Yo-Site E-Business 
http://yo-site.biz

tested
Win IE 6 & 7, Opera 9, FF 3, Chrome
Mac Safari 3, Opera 3, FF 3
Linux Konqueror 3.5, FF 2
\************************************************************************/

var timed_element = new Array();
var menu_timeout;
var ul_top = null;
var slum_debug_window = null;
var hover_time_ms = 700; // default is 0.7 sec
var ClickDriven = false; // defult is you just hover

function expand_menu(timed_element_index){

    if (timed_element[timed_element_index] != timed_element[timed_element.length-1])
        return; // the user hasn't hanged enough
    var li_to_open = timed_element[timed_element_index];

    while (li_to_open.tagName.toUpperCase() != 'LI') {
        if (li_to_open == ul_top) return;
        li_to_open = li_to_open.parentNode;
        
    }
    
    if (ClickDriven) {
        // if no ul kids or kids open, activate the first link within li_to_open
        var kids = li_to_open.childNodes;
        display_debug("--------\n" + kids);
        var href = null;
        var should_relocate = true; // if true we will change window.location
        for (iChild in kids) {
            if (kids[iChild].tagName && kids[iChild].tagName.toUpperCase() == 'A')
                href = kids[iChild].href;
            else if (kids[iChild].tagName && kids[iChild].tagName.toUpperCase() == 'UL')
                if (kids[iChild].style.display != "block") // not opened yet
                    should_relocate = false;
                
        }
        
        display_debug(href + " " + should_relocate);
        
        if (should_relocate && href) {
            window.location = href;
            return;
        }
        
        
    }
    ul_to_close = li_to_open.parentNode;
    while (ul_to_close.tagName.toUpperCase() != 'UL')
        ul_to_close = ul_to_close.parentNode;
    
    var kids = ul_to_close.childNodes;
    
    for (iChild in kids)
        if (kids[iChild].tagName && kids[iChild].tagName.toUpperCase() == 'LI') {
            if (kids[iChild] == li_to_open)
                var the_display = 'block';
            else
                var the_display = 'none';
            grandkids = kids[iChild].childNodes;
            for (iBaby in grandkids)
                if (grandkids[iBaby].tagName && grandkids[iBaby].tagName.toUpperCase() == 'UL')
                    grandkids[iBaby].style.display = the_display;

        }

}
    

function prepare_to_open_menu(e)  {
    if (window.event) e = window.event; 
    var srcEl = e.srcElement? e.srcElement : e.target;
    if (srcEl.nodeType == 3)       // for Safari
        srcEl = srcEl.parentNode;
    
    timed_element[timed_element.length] = srcEl;
    display_debug ( timed_element );
    if (ClickDriven)
        expand_menu(timed_element.length -1 );
        
    else
        menu_timeout = setTimeout('expand_menu(' + (timed_element.length -1 ) + ')', hover_time_ms);
    
    event.cancelBubble = true;
    if (ClickDriven)
        return false;

}

function display_debug(str){
    if (slum_debug_window && slum_debug_window.tagName && slum_debug_window.tagName.toUpperCase() == "TEXTAREA")
        slum_debug_window.value = slum_debug_window.value + str;
}



function ClearMenuTimeout() {
    clearTimeout(menu_timeout);
}

// default action is hover, pass "click" if you want to click to open menus
function SlideMenuInit(action) {
    if (!ul_top)
        ul_top = document.getElementById('ul_top');
    if (!ul_top)
        ul_top = document.getElementById('ul-top');
        
    if (ul_top) {
        
        if (action == "click") {
            ClickDriven = true;
            ul_top.onclick = prepare_to_open_menu;
        } else {
            ul_top.onmouseover = prepare_to_open_menu;
            ul_top.onmouseout = ClearMenuTimeout;
        }

    }
    if (!slum_debug_window)
        slum_debug_window = document.getElementById('slum_debug_window');
    display_debug("Hello, UL slide menu debug activated.\n");
}