;(function($) {
	
	$.slideshow = function(el, options) {
		
		var defaults = {
			delay: 6000,
			speed: 1000,			
			beforeFadeOut: function() {},
			afterFadeOut: function() {}
		}
		
		var plugin = this;
		plugin.settings = {};
		
		var currentImage;
		var currentIndex = -1;
		var timer;
		
		
		var init = function() {
			
			plugin.settings = $.extend({}, defaults, options);
			plugin.el = el;// the actual element
			plugin.slides = plugin.el.children();
			plugin.image = currentImage;
			
			timer = setTimeout(showNext, plugin.settings.delay);
			showNext(); //loads first image
		}
		
		//plugin.showNext = function() {
			// code goes here
		//}
		
		var showImage = function(index) {
			if(index < plugin.slides.length){
				var indexImage = plugin.slides[index];
				//alert(indexImage.src);
				if(currentImage){
					if(currentImage != indexImage ){
						var holder = $(currentImage);
						holder.css('z-index',2);
						clearTimeout(timer);
						plugin.settings.beforeFadeOut();
						holder.fadeOut(plugin.settings.speed, function() {
							timer = setTimeout(showNext, plugin.settings.delay);
							$(this).css({'display':'none','z-index':1});
							plugin.settings.afterFadeOut();
						});
					}
				}
				$(indexImage).css({'display':'block','opacity':1});
				plugin.image = currentImage = indexImage;
				currentIndex = index;
			}
		}
		
		var showNext = function() {
			//alert('showNext');
			var len = plugin.slides.length;
			var next = currentIndex < (len-1) ? currentIndex + 1 : 0;
			showImage(next);
		};
		
		// call the "constructor" method
		init();
		
	}
	
})(jQuery);

/*
// USAGE
var myplugin = new $.slideshow($('#slides'),{delay:8000, speed:2000});

// call a public method
//myplugin.public_method();

// get the value of a public property
//myplugin.settings.property;
*/
