jQuery(function()
{
    // Changable Settings
    var fadeTime = 300;
    var fadeDelay = 300;
    var shortFadeDelay = 100;
    var ulId = 'hover-nav';
    
    // Internal use variables
    var timeOut = [];
    
    // Set mouse events for each menu item with a sub-menu
    jQuery('#nav ul > li').each(function(element)
    {
        if(jQuery(this).parent().attr('id') == ulId)
        {
            jQuery(this).hover(
                function() { 
                    mouseOver(jQuery(this));
                },
                function() {
                    mouseOut(jQuery(this));
                }
            );
        }
    });

    function mouseOver(obj)
    {
        window.clearTimeout(timeOut[obj.attr('id')]);
        
        currentMenuItem = obj;

        fadeIn(obj);
    }
    
    function mouseOut(obj)
    {
        delayTime = shortFadeDelay;
        timeOut[obj.attr('id')] = window.setTimeout(function() {
            fadeOut(obj);
        },delayTime); 
    }
    
    function fadeIn(obj)
    {
        jQuery(obj).find('ul').fadeIn(fadeTime);
    }
    
    function fadeOut(obj)
    {
        jQuery(obj).find('ul').fadeOut(fadeTime);
    }
});
