/*
 * jQuery.slides - A simple slideshow library for jQuery.
 *
 * Written by Ryan Wilke at Octopus Creative (ryan@octopuscreative.com).
 * http://octopuscreative.com/
 * 
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 *
 * @author Ryan Wilke
 * @version 1.0
 *
 */

(function($){
	
	var settings, methods = {
		
		/* --------------------------------------------------
		Slides: Initialize
		-------------------------------------------------- */
		init: function(options){
			methods.setDefaults(this, options);
			methods.rotateSlides();
		},
	
		/* --------------------------------------------------
		Slides: Default Settings
		-------------------------------------------------- */
		setDefaults: function(element, options){
			settings = {
				container: element,
				slides: element.find('.slide'),
				delayFirstSlideBy: 3000,
				delaySlidesBy: 5000,
				onSlideChange: function(){}
			};

			if (options) $.extend(settings, options);
		},

		/* --------------------------------------------------
		Slides: Rotate Slides
		-------------------------------------------------- */
		rotateSlides: function(){

			// Automatically flip through the slides after a 3 second delay
			$(document).oneTime(settings.delayFirstSlideBy, function(){
				$(document).everyTime(settings.delaySlidesBy, function(){

					// Grab the current slide
					var current = settings.slides.filter('.current');

					// Select the next slide in the set
					methods.setCurrentSlide((current.is(':last-child')) ? (0) : (current.index() + 1));
				});		
			});
		},
	
		/* --------------------------------------------------
		Slides: Set Current Slide
		-------------------------------------------------- */
		setCurrentSlide: function(index, killTimer){

			// Kill the timers
			if (killTimer) $(document).stopTime();
			
			// Show the selected slide provided by the user
			settings.slides.eq(index).addClass('current').siblings().removeClass('current');
			
			// Let the user run some code
			settings.onSlideChange(settings.slides.filter('.current'));
		}
	};
	
	/* --------------------------------------------------
	Slides: Brain
	-------------------------------------------------- */
	$.fn.slides = function(method){
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' +  method + ' does not exist on jQuery.slides');
		}
	};

})(jQuery);
