(function($) {
	
	var methods = {
		init : function( options ) {

			return this.each(function(){
				
				var speed = options.speed;
				var images = $(this).find('img');
				var currentID = 0;
				var container = $(this);
				
				container.data('play', false);
				
				$(images[currentID]).css({'display':'block'});
				if(images.length > 1) var timeout = setTimeout(function(){ nextImage(); }, speed);

				function nextImage(){
					if(container.data('play')){
						$(images[currentID]).css({'display':'none'});
						currentID++;
						if(currentID >= images.length) currentID = 0;
						$(images[currentID]).css({'display':'block'});
					}
					timeout = setTimeout(function(){ nextImage(); }, speed);
				}

			});

		},
		destroy : function( ) {
			return this.each(function(){
		    	$(this).unbind('.muxflip');
		  	})
		},
		play : function( ) { 
			return this.each(function(){
		    	$(this).data('play', true);
		  	})
		},
		pause : function( ) { 
			return this.each(function(){
		    	$(this).data('play', false);
		  	})
		}
	};

	$.fn.muxflip = 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.muxflip' );
		}    

	};

})(jQuery);

