(function($) {

    $.fn.spasticNav = function(options) {
        options = $.extend({
            overlap : 20,
            speed : 500,
            reset : 1500,
            color : '#0b2b61',
            easing : 'easeOutExpo'
        }, options);

        return this.each(function() {
            if($(this).html()!="") {
                var nav = $(this),
                currentPageItem = $('.tr_menu_current_page', nav),
                blob,reset;
                $('<div id="blob"><input type="hidden" value="/" name="same-window"></div>').appendTo($(this));
                if(currentPageItem.text()=="" && $('.tr_menu_current_path', nav).text()!="") {
                    currentPageItem = $('.tr_menu_current_path', nav);
                } else {
                    currentPageItem = $('.tr_menu_tab:first', nav);
                }
                blob = $('#blob');
                var text = currentPageItem.find(".tr_menu_tab_middle").find("a").text();
                $('<div class="tr_menu_tab_left"></div>').appendTo(blob);
                $('<div class="tr_menu_tab_middle tr_menu_item"><a href="#" style="color:#FFFFFF;text-decoration:none;">'+text+'</a></div>').css({
                    /*width : currentPageItem.find(".tr_menu_tab_middle").width(),*/
                    height : currentPageItem.find(".tr_menu_tab_middle").height(),
                    float:"left"
                }). appendTo(blob);
            
                $('<div class="tr_menu_tab_right"></div>').appendTo(blob);
                blob.css({
                    position: "absolute",
                    left : currentPageItem.position().left,
                    top : (currentPageItem.position().top - options.overlap / 2)+10,
                    "z-index":"900",
                    display:"none"
                });
                $('.tr_menu_tab:not(.tr_menu_current_page):not(.tr_menu_current_path)', nav).hover(function() {

                    var current_left = $(this).position().left-6;
                    var current_text = $(this).find(".tr_menu_tab_middle").find("a").html();
                    clearTimeout(reset);
                    blob.css({
                        display:"block"
                    });
                    blob.animate({
                        left: current_left
                    },{
                        duration : options.speed,
                        easing : options.easing,
                        queue : false,
                        step:function(){
                            $(".tr_menu_tab:not(.tr_menu_current_page):not(.tr_menu_hover):not(.tr_menu_current_path)").find(".tr_menu_tab_middle").find("a").css({
                                "color":"#FFFFFF"
                            });
                        }
                    });
                    blob.find(".tr_menu_tab_middle").find("a").html(current_text).css({
                        "color":"#bcf9ff"
                    });
                    $(this).find(".tr_menu_tab_middle").find("a").css({
                        "color":"#008C99"
                    });
                    blob.css({
                        "z-index":"900"
                    });
                    blob.animate({
                        left: Math.round(current_left)
                    },{
                        duration : options.speed,
                        easing : options.easing,
                        queue : false
                    });
                }, function() {
                    // mouse out
                    var currentPageItem = $('.tr_menu_current_page', nav);
                    var curr_left = 0;
                    if(currentPageItem.html()==null || currentPageItem.html()=="") {
                        curr_left = 0;
                    } else {
                        curr_left = currentPageItem.position().left;
                    }
                    blob.stop().animate({
                        left : $(this).position().left-6
                    }, options.speed);
                    reset = setTimeout(function() {
                    
                        blob.animate({
                            left : curr_left-6
                        }, {
                            duration: options.speed,
                            step: function(now, fx){
                                blob.find(".tr_menu_tab_middle").find("a").empty().text(currentPageItem.find(".tr_menu_tab_middle").find("a").text());
                            
                            },
                            complete:function(){
                                $(".tr_menu_tab:not(.tr_menu_current_page):not(.tr_menu_current_path)").find(".tr_menu_tab_middle").find("a").css({
                                    "color":"#FFFFFF"
                                });
                                blob.css({
                                    display:"none"
                                });
                            }
                        });
                    }, options.reset);
                });
            }
        }); // end each


    };

})(jQuery);