/**
 * jQuery.geeSlideshow - Creates a slide show with a hero image
 * Date: 2010/01/15
 *
 * @author Brian Xerri
 * @version 1.0.0
 * @jQuery Version: jquery-1.4.min.js
 *
 ***************************
 *
 * Copyright: GeeMultimedia
 **/

(function($) {
	jQuery.fn.geeSlideshow = function(options) {
		/*--------------------------------
		/ Defualt Settings
		--------------------------------*/
		settings = jQuery.extend({
					slideSelector: '#geeSlides li',
					autoSlide: false,
					autoSlideDelay: 5000,
					animateDuration: 200 }
					, options);
		
		/*--------------------------------
		/ Private Properties
		--------------------------------*/
		var wrapper;
		var slides;
		var slideThumbs;
		var slideCount;
		var activeSlideIndex;
		var timerDelay;
		
		
		/*--------------------------------
		/ Initialize
		--------------------------------*/
		this.each(function() {
			
			wrapper				= $(this);
			slides				= $(options.slideSelector);
			slideThumbs			= $('li', wrapper);
			slideCount			= parseInt(slideThumbs.size());
			activeSlideIndex	= 0;

			slides.hide().eq(0).show();
			slideThumbs.eq(0).addClass('active');

			autoSlider();

		});

		
		/*--------------------------------
		/ Control Buttons
		--------------------------------*/
		slideThumbs.click(function(index) {
			$.fn.geeSlideshow.goToSlide(slideThumbs.index(this));
		});

		
		/*--------------------------------
		/ Private Functions
		--------------------------------*/
		function autoSlider() {
			if ( options.autoSlide == true ) {
				if (timerDelay !== undefined)
					clearTimeout(timerDelay);
				
				timerDelay = setTimeout(function() {
					var nextIndex = activeSlideIndex >= (slideCount-1) ? 0 : (activeSlideIndex + 1);
					$.fn.geeSlideshow.goToSlide(nextIndex);
				}, options.autoSlideDelay);
			}			
		}
		

		/*--------------------------------
		/ Public Functions
		--------------------------------*/
		$.fn.geeSlideshow.goToSlide = function(slideIndex) {
			if (slideIndex != activeSlideIndex) {
			
				slideThumbs.filter('.active').removeClass('active');
				slideThumbs.eq(slideIndex).addClass('active');

				slides.eq(activeSlideIndex).stop().fadeOut(options.animateDuration);
				slides.eq(slideIndex).stop().css('opacity','').delay(options.animateDuration).fadeIn(options.animateDuration);
				activeSlideIndex = slideIndex;

				autoSlider();
			}
			return false;
		}

		

	}
})(jQuery);

