jQuery(function() {
    var $ = jQuery;
    // ie 6
    var ie6 = ($.browser.msie && $.browser.version < 7);
    // 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;
    };
    // base rules
    $('#nav-primary').addClass('js-active');
    $('ul li ul', '#nav-primary').css('visibility', 'hidden');
    $('ul>li', '#nav-primary').hover(
        function(){
            var ul = $('ul:first', this);
            if (ul.length){
                if (!ul[0].hei) ul[0].hei = ul.height();
                ul.css({height: 0, overflow: 'hidden'}).retarder(100, function(i){
                    //$('a:first', ul[0].parentNode).css('background', '#000');
                    $('#nav-primary').removeClass('js-active');
                    i.css('visibility', 'visible').animate({height: ul[0].hei}, {duration: 500, complete : function(){ ul.css('overflow', 'visible'); }});
                });
            }
            else setTimeout(function(){ $('#nav-primary').removeClass('js-active'); }, 500);
        },
        function(){
            var ul  = $('ul:first', this);
            if (ul.length){
                var css = {visibility: 'hidden', height: ul[0].hei};
                //$('a:first', ul[0].parentNode).css('background', 'none');
                ul.stop().retarder(1, function(i){ i.css(css); });                
            }
            $('#nav-primary').addClass('js-active');
        }
    );
   $('ul ul li', '#nav-primary').hover(
        function(){
            var ul = $('ul:first', this);
            if (ul.length){
                if (!ul[0].wid) ul[0].wid = ul.width();
               ul.css({width: 0, overflow: 'hidden'}).retarder(100, function(i){
                    i.css('visibility', 'visible').animate({width: ul[0].wid}, {duration: 500, complete : function(){ ul.css('overflow', 'visible'); }});
                });
                if (!ul[0].hei) ul[0].hei = ul.height();
                
            }
        },
       function(){
            var ul  = $('ul:first', this);
           if (ul.length){
                var css = {visibility: 'hidden', width: ul[0].wid};
                ul.stop().retarder(1, function(i){ i.css(css); });                
            }
      }
  );
//    // lava lamp
//    if (ie6){
//        $('#nav-primary ul>li').hover(function(){ $(this).addClass('hover'); }, function(){ $(this).removeClass('hover'); });
//        $('#nav-primary ul').lavaLamp({speed: 300});
//    }
//    else $('#nav-primary ul').lavaLamp({fx: 'backout', speed: 300});
//    // animation
//    $('ul ul a', '#nav-primary').hover(
//        function(){ $(this).stop().animate({textIndent: 10}, 400); },
//        function(){ $(this).stop().animate({textIndent: 0}, {duration: 400, complete: function(){}}); }
//    );
});


