(function(jQuery){ 

	jQuery.fn.underTabs = function(options) {

		var defaults = {interval:5000, fadeDuration:500, cycle:true}; 
		var opts = jQuery.extend(defaults, options);
		
		return this.each(function()
		{
			var jUnderTabs = jQuery(this);
			
			var hover = false;
			var hoverTimout = false;
			
			jUnderTabs.mouseover(function()
				{
					if(hoverTimout !== false)
					{
						window.clearTimeout(hoverTimout);
					}
				
					if(!hover)
					{
						hover = true;
					}
				});
				
			jUnderTabs.mouseout(function()
				{
					hoverTimout = window.setTimeout(function(){
							hover = false;
						}, 100);
				});
			
			var jTabs = jUnderTabs.find('li');
			
			// seems to be a bug with 'is'? therefore the double negative
			jTabs.not(':not(:first)').addClass('first active');
			jTabs.not(':not(:last)').addClass('last');
			
			var tabWidth = (100 / jTabs.size()) - 0.1;
			
			jTabs.css({width: (tabWidth + '%')});
			
			var jImages = jTabs.children().filter('a, img').prependTo(jUnderTabs);
			
			jImages.wrap('<div class="tabContent"></div>');
			
			var jTabContent = jUnderTabs.find('div.tabContent');
			
			jTabContent.each(function(i)
				{
					var jContent = jQuery(this);
					var jComplexContent = jTabs.eq(i).children().filter('div');
					var contentText = jContent.find('img').attr('alt');
					
					if(i === 0)
					{
						jContent.addClass('active');
						
					}
					
					if(jComplexContent.size())
					{
						contentText = jComplexContent.html();
						jComplexContent.remove();
					}
					jContent.append('<div></div><span>' + contentText + '</span>');
					
				});
	
			var animating = false;
			
			function showNewTab(index)
			{
				if(animating)
				{
					return;
				}
				
				animating = true;
			
				jTabs.removeClass('active');
				jTabs.eq(index).addClass('active');
				
				var jCurrContent = jTabContent.filter('.active');
				var jNewContent = jTabContent.eq(index);
				
				var oldIndex = jCurrContent.prevAll().size();
				
				if(oldIndex == index)
				{
					animating = false;
					return;
				}
				
				jNewContent.show();

				jCurrContent.removeClass('active');
				jNewContent.css({opacity:0, 'z-index':1}).addClass('active');
				
				jNewContent.animate({opacity:1}, {duration:opts.fadeDuration, complete:function(){
						jCurrContent.hide();
						jNewContent.removeAttr('style');
						animating = false;
					}});
				
			}
			
			jTabContent.find('div').css({opacity:.8});
			jTabContent.not(':first').hide();
			
			var tabButtons = jTabs.find('span');
			
			tabButtons.css({cursor:'pointer'});
			
			tabButtons.click(function()
				{
					var index = jQuery(this).parent().prevAll().size();
					
					showNewTab(index);
				});
			
			tabButtons.mouseover(function()
				{
					jQuery(this).addClass('over');
				});
				
			tabButtons.mouseout(function()
				{
					jQuery(this).removeClass('over');
				});
			
			if(opts.cycle)
			{
				window.setInterval(function()
					{
						if(!hover)
						{
							var jActiveTab = jTabs.filter('.active');
							
							var jTab = jActiveTab.next();
							
							if(!jTab.size())
							{
								var jTab = jTabs.eq(0);
							}
							
							var index = jTab.prevAll().size();
							
							showNewTab(index);
						}
					}, opts.interval);
			}
		});
	};

})(jQuery);