(function($) {
    $.fn.slideshow = function(options) {
        return this.each(function() {
        	var opts = $.extend({}, $.fn.slideshow.defaults, options);
        	
        	if (this.cycleTimeout)
        		clearTimeout(this.cycleTimeout);
        	this.cycleTimeout = 0;
        	this.cyclePause = 0;
        
        	opts.images = opts.images.split(','); 
	        	
	        // only cycle multiple images	
	        if (opts.images.length == 1)
	        	return;  	
	        	
	        opts.parent = $(this);
	        opts.slides = new Array();
	        
	        if (opts.parent.css('position') == 'static') 
            	opts.parent.css('position', 'relative');
	        
	        //check if first image is already in container
	        var $first = opts.parent.find('img').eq(0);
	        if (!$first.length) {
	        	//add first image
				var img = new Image(); 
				$first = $(img).attr('src', opts.folder+$.trim(opts.images[0]));
				opts.parent.append($first);
	        }
	        $first.css({position: 'absolute', top:0, left:0, 'z-index':opts.images.length});
	        opts.slides.push($first);
	        
	        opts.currSlide = 0;
	        opts.nextSlide = 1;
	        
	        
	        if (opts.timeout)
            	this.cycleTimeout = setTimeout(function() {
                	changeSlide(opts, 0);
            }, opts.timeout + (opts.delay||0));     	
		});

    };	
    
    
	function changeSlide(opts) {
		
			if (opts.images.length>opts.slides.length) {
				preloadImage(opts);
	      	} else {
	      	
				if (opts.nextSlide)
		        	opts.slides[opts.currSlide].fadeOut(opts.speed);
		        else {
		        	opts.slides[opts.nextSlide].fadeIn(opts.speed, function(){
		        		//show all the images
		        		opts.parent.children().not(':eq(0)').show();
		       		});	      	
		       	}
	      	
	      		var roll = (opts.nextSlide + 1) == opts.images.length;
	        	opts.nextSlide = roll ? 0 : opts.nextSlide+1;	        	
	        	opts.currSlide = roll ? opts.images.length-1 : opts.nextSlide-1;
	        
		        setTimeout(function() { changeSlide(opts) }, opts.timeout);
	      	}
	      	
	};    
	
	function preloadImage(opts) {
		// image preloading
		var img = new Image();
		$(img).load(function () {
	
	    	opts.parent.append($(this));
		    opts.slides.push($(this));
		    opts.slides[opts.currSlide].fadeOut(opts.speed);	        
		        	
		    var roll = (opts.nextSlide + 1) == opts.images.length;
	        opts.nextSlide = roll ? 0 : opts.nextSlide+1;
	        opts.currSlide = roll ? opts.images.length-1 : opts.nextSlide-1;	        	
	
		    setTimeout(function() { changeSlide(opts) }, opts.timeout);
		        	 
		}).error(function(){
			//skip the image
			opts.images.splice(opts.nextSlide, 1);
			
			setTimeout(function() { changeSlide(opts) }, opts.timeout);
		})
		  .attr('src', opts.folder+$.trim(opts.images[opts.nextSlide]))
		  .css({position: 'absolute', top:0, left:0, 'z-index':opts.images.length-opts.nextSlide}); 
	}
    
    $.fn.slideshow.defaults = {
    		images	:	'',
			timeout	:	10000, 
			speed	:	2000, 
			height	:	'auto',
			fit		:	0,    
			pause	: 	0,    
			delay	:	5000,    
			folder	:	'/img/' 
	};
})(jQuery); 
