$(document).ready(function(){
    
    // retarder
    $.fn.retarder = function(delay, method){
        var node = this;
        if (node.length){
            if (node[0]._timer_) clearTimeout(node[0]._timer_);
            node[0]._timer_ = setTimeout(function(){ method(node); }, delay);
        }
        return this;
    };
    
    $('header nav').addClass('js-active');
        $('ul ul', 'header nav').css('visibility', 'hidden');
        $('li', 'header nav').hover(
            function(){
                var ul     = $('ul:first', this);
                if (ul.length){
                    if (!ul[0].hei) ul[0].hei = ul.height();
                    if (!ul[0].wid) ul[0].wid = ul.width();
                    ul.css({opacity:0, height: 0, overflow: 'hidden', width: ul[0].wid}).retarder(0.5, function(i){
                        i.css({'visibility':'visible','display':'block'}).stop().animate({height: ul[0].hei, opacity:1}, {duration: 400, complete : function(){ ul.css('overflow', 'visible'); }});
                    });
                }
            },
            function(){
                var ul  = $('ul:first', this);
                if (ul.length){
                    var css = {opacity:0, height: 0};
                    ul.stop().retarder(1, function(i){ i.animate(css,400,function(){
                        i.css({'display':'none'});
                    }); });
                    
                }
            }
        );
});

