(function($) {

    $.fn.spasticNav = function(options) {
    
        options = $.extend({
            overlap : 2,
            speed : 500,
            reset : 200,
            easing : 'easeOutExpo'
        }, options);

        $.extend($.easing,
        {
            easeOutExpo: function (x, t, b, c, d) {
                return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
            },
        })

        var blobSelector = '.b-topmenu-nav-blob';
    
        return this.each(function() {
        
             var nav = $(this),
                 currentPageItem = $('.b-topmenu-nav-current', nav),
                 blob,
                 reset;
                 
             blob = $('<li class="b-topmenu-nav-blob"></li>').css({
                 width : currentPageItem.outerWidth(),
                 height : 6,
                 left : ( currentPageItem.position() && currentPageItem.position().left ) || 0,
                 top : ( currentPageItem.position() && currentPageItem.position().top +41 ) || 41
             }).appendTo( nav );
             
            $('li:not(' + blobSelector + ')', nav).hover(function() {
                var $this = $(this);
                clearTimeout(reset);
                blob.animate(
                    {
                        left : ( $this.position() && $this.position().left ) || 0,
                        width : $this.width()
                    },
                    {
                        duration : options.speed,
                        easing : options.easing,
                        queue : false
                    }
                );
            }, function() {
                reset = setTimeout(function() {
                    blob.animate({
                        width : currentPageItem.outerWidth(),
                        left : ( currentPageItem.position() && currentPageItem.position().left ) || 0,
                    }, options.speed)
                }, options.reset);
    
            });
        
        }); // each() 
    
    };

})(jQuery);

