(function($) {
	
	$.fn.magicBanners = function(options){

		var s = $.extend({
			speed: 2000,
			timeout: 1000
		}, options||{});
		var t = this;
		var imgs = t.find('img');
		if( imgs.length <= 1 ) return this;
		
		var cnt = imgs.length;
		var current;
		
		imgs.each(function(i){
			var img = $(this);
			img.css('z-index', cnt-i).attr('mbi', i);	
		});
		
		current = $(imgs[0]);
		
		function change(){
			var img = current;
			img.animate({ opacity: 0}, s.speed, function(){
			//img.hide( s.speed, function(){
				var mbi = $(this).attr('mbi')*1;
				imgs.each(function(i){
					var zi = $(this).css('z-index')*1;
					$(this).css('z-index', zi+1);						
				});
				$(this).css({zIndex:0, opacity:1});
				//$(this).css({zIndex:0}).show();
				var next = mbi+1 < cnt ? mbi+1 : 0;
				current = $(imgs[next]);
				setTimeout(change, s.timeout ); 
			});
		}
		
		change();
		
	}	
	
})(jQuery);