$( document ).ready ( function() {
    $('#header > ul.nav > li').each(function() {
    
        // check if li has a sub nav
        if ( $(this).children('ul').length > 0 )
        {
            var drop_anchor = $(this).children('a');
            var subnav = $(this).children('ul');
            
            $("body").click(function () {
                $(subnav).hide();
            });
            
            $(drop_anchor).click(function() {
                var a_name = $(this).attr("title");
                $(subnav).each(function () {
                    if($(this).siblings("a").attr("title") == a_name) {
                        $(this).hide();
                    }
                });
                $(this).siblings("ul").toggle();
                var time_out;
                $(this).siblings("ul").mouseleave(function () {
                    time_out = setTimeout(function () {
                        var display = $("a[title=" + a_name + "]").siblings("ul").css("display");
                        if(display == "block") {
                            $("a[title=" + a_name + "]").siblings("ul").hide();
                        }
                    }, 900)
                });
                $(this).siblings("ul").mouseenter(function () {
                    clearTimeout(time_out);
                });
                return false;
            });
        }
    });
} );
