/*
Require: jQuery
*/

$(function() {
    var t;
    var $menu = $(".menu > div > ul");
    $menu.find("li").each(function(i) {
        $(this).click(function(e) {
            $(this).children("ul").slideToggle("slow");
            //e.stopPropagation();
            if (t) {
                clearTimeout(t);
            }
        }).mouseenter(function() {
            if (t) {
                clearTimeout(t);
            }
        });
    });

    $menu.mouseleave(function() { // hide nodes when leaving the menu except breadcrums parents
        t = setTimeout(function() { $menu.find('ul').each(function() {if ($(this).children('li.bc').length <= 0) $(this).slideUp('slow')}) }, 2000);
    }).find("ul").hide(); // Init all nodes

    // Show breadcrums of current selected node
    $menu.find('li.bc').each(function() {
        $(this).parent().show();
    });

    $menu.show();
});