// JavaScript Document

(function($){
	$.fn.spasticNav = function(options){
		options = $.extend({
			overlap : 20,
			speed : 500,
			reset : 1500,
			color : '#8b0404',
			easing : 'easeOutExpo'
		}, options);

		return this.each(function(){			
			var nav = $(this),
				//currentPageItem = $('.selected', nav),
				currentPageItem,
				blob,
				reset;
				
			$('li a', nav).each(function(){
				var link = $(this).attr('href');
				if(location.pathname.indexOf(link) != -1){
					currentPageItem = $(this).parent();
				}
			});
			
			if(currentPageItem == undefined){
				currentPageItem = $('li a[href="index.html"]', nav).parent();
			}
			
			currentPageItem.addClass('selected');			
			
				
			$('<li id="blob"></li>').css({
				width : currentPageItem.width(),
				height : currentPageItem.outerHeight() + options.overlap,
				left : currentPageItem.position().left,
				top : currentPageItem.position().top - options.overlap / 2,
				backgroundColor : options.color
			}).appendTo(this);
			
			blob = $('#blob', nav);	
			
			$('li:not(#blob)', nav).hover(function(){
				//mouse over
				clearTimeout(reset);
				blob.animate({
					left : $(this).position().left,
					width : $(this).width()	
				}, {
					duration : options.speed,
					easing : options.easing,
					queue : false	
				});
			}, function(){
				//mouse out
				reset = setTimeout(function(){
					blob.animate({
						width : currentPageItem.width(),
						left : currentPageItem.position().left	
					}, {
						duration : options.speed,
						easing : options.easing,
					});	
				}, options.reset);
			});
		});
	};
})(jQuery);
