jQuery(document).ready(function(){
    var $ = jQuery;
    
    $('#nav > .menu-item > a').each(function(){
        var link = $(this);
        var menu = link.parent().find('ul');
        if (menu.length > 0) {
            link.click(function(event) {
                event.preventDefault();
                element.fadeIn();
            });
        }
    })
    
    var timers = [];
    $(".menu-item").hover(function() {
       var element = $(this);
       if (timers[element.attr('id')] != null) {
           clearTimeout(timers[element.attr('id')]);
           timers[element.attr('id')] = null;
       } else {
           element.find('ul').fadeIn();
       }
    },
    function() {
          var element = $(this);
          timers[element.attr('id')] = setTimeout(function(){
              element.find('ul').fadeOut();
              timers[element.attr('id')] = null;
          }, 1000);
    });
});
