/********************************
/* Copyright 2009 Tim Santeford *
/* All Rights Reserved          *
/********************************/
(function($){  
	
	$.fadeshow = {
		defaults: {
			interval: 8000,
			fadeSpeed: 800,
			randomStart: false,
			showNavigation:false,
			previousButton: '<div class="previous"></div>',
			nextButton: '<div class="next"></div>'
		}
	};

	$.fn.extend({
		fadeshow:function(config) {	
			
			config = $.extend({}, $.fadeshow.defaults, config);
	
			return this.each(function() { 
			
				// Initialize Elements
				var list = $(this);				
				var items = $('li', list);
				var $container = $(document.createElement("DIV"));
				$container.css('position','relative');
				$container.attr('id', list.attr('id')+'_container');
										
				list.before($container);
				$container.append(list);

				// Add Buttons
				var previousButton;
				var nextButton;
				if (config.showNavigation == true) {
					previousButton = $(config.previousButton);
					previousButton.hide();
					nextButton = $(config.nextButton);
					nextButton.hide();
					list.before(previousButton);
					list.before(nextButton);
					
					previousButton.click(function(){showPreviousItem(config.fadeSpeed/2);});					
					nextButton.click(function(){showNextItem(config.fadeSpeed/2);});	
				}
			
				// Fix List Style
				list.css('position', 'relative');
				//list.css('width', this.clientWidth);
			
				var maxh = 0;		
				var zIndex = items.length;
				items.each(function () {
					//$(this).css("zIndex", zIndex);	
					$(this).css("position", "absolute");
					$(this).css("left", "0px");
					$(this).css("top", "0px");
					zIndex = zIndex-1;
					var h = this.clientHeight;	
					if (h > maxh) { maxh = h; }	
					$(this).hide(); 
				});
				list.css('height', maxh);		
				
				var currentIndex = 0;
				if (config.randomStart == false) {
					$(items[currentIndex]).show();					
				} else { 
					currentIndex = Math.floor(Math.random()*items.length);
					$(items[currentIndex]).show();	
				}			
									
				// Event Wireup
				var rotateId = startRotate(config.interval);
				$container.hover(
					function () {
									clearInterval(rotateId);
									if (config.showNavigation == true) {
										previousButton.stop();
										previousButton.css('opacity', '1');
										previousButton.fadeIn('fast');
										nextButton.stop();
										nextButton.css('opacity', '1');
										nextButton.fadeIn('fast');
									}
								}, 
					function () {
									rotateId = startRotate(config.interval);
									if (config.showNavigation == true) {
										previousButton.stop();	
										previousButton.fadeOut('fast');
										nextButton.stop();
										nextButton.fadeOut('slow');
									}
								}
				);
				
				
				
				// Helper Methods
				function startRotate(interval) { 
					return setInterval(function () {
						showNextItem(config.fadeSpeed);
					}, interval);
				}
				
				function showNextItem(fadeSpeed) {
					var prevli = $(items[currentIndex]);
					currentIndex += 1;
					if (currentIndex > items.length - 1) {
						currentIndex = 0;
					}
					var nextli = $(items[currentIndex]);	
					prevli.fadeOut(fadeSpeed);									
					nextli.fadeIn(fadeSpeed);
				}
				
				function showPreviousItem(fadeSpeed) {
					var prevli = $(items[currentIndex]);
					currentIndex -= 1;
					if (currentIndex < 0) {
						currentIndex = items.length - 1;
					}
					var nextli = $(items[currentIndex]);	
					prevli.fadeOut(fadeSpeed);									
					nextli.fadeIn(fadeSpeed);
				}
			});
		}
	});

})(jQuery);  
