(function( $ ){
	var methods = {
		settings: {},
		init : function( options ) {
			this.settings = $.extend( {
				speed: 2000,
				caption_id: 'slideshow-caption',
			}, options),
			jqelement = this;
			
			//append a LI item to the UL list for displaying caption
			this.append('<li id="'+ this.settings.caption_id+'" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

			//Set the opacity of all images to 0
			this.find('li').css({opacity: 0.0});
			
			//Get the first image and display it (set it to full opacity)
			var first = this.find('li:first').css({opacity: 1.0}).addClass('show');
			
			
			var caption_element = $('#' + this.settings.caption_id);
			
			//Get the caption of the first image from REL attribute and display it
			caption_element.find('h3').html(first.find('img').attr('title'));
			caption_element.find('p').html(first.find('img').attr('alt'));

			//Display the caption
			caption_element.css({opacity: 1, bottom:0});
			
			//Call the gallery function to run the slideshow	
			var timer = setInterval(function(){
						jqelement.simpleSlide('gallery');
					},this.settings.speed);
			
			//pause the slideshow on mouse over
			this.hover(
				function () {
					clearInterval(timer);	
				}, 	
				function () {
					timer = setInterval(function(){
						jqelement.simpleSlide('gallery');
					},jqelement.settings.speed);			
				}
			);
			
		  // THIS 
		},
		gallery : function( ) {
		  
			//if no IMGs have the show class, grab the first image
			var current = (this.find('li.show')?  this.find('li.show') : this.find('li:first')),
			jqelement = this;
			
			//trying to avoid speed issue
			if(current.queue('fx').length == 0) {	
			
				//Get next image, if it reached the end of the slideshow, rotate it back to the first image
				var next = ((current.next().length) ? ((current.next().attr('id') == this.settings.caption_id)? this.find('li:first') :current.next()) : this.find('li:first'));
					
				//Get next image caption
				var title = next.find('img').attr('title');	
				var desc = next.find('img').attr('alt');	
			
				//Set the fade in effect for the next image, show class has higher z-index
				next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
				
				var caption_element = $('#'+this.settings.caption_id);
				
				//Hide the caption first, and then set and display the caption
				caption_element.slideToggle(300, function () { 
					caption_element.find('h3').html(title); 
					caption_element.find('p').html(desc); 
					caption_element.slideToggle(500); 
				});		
			
				//Hide the current image
				current.animate({opacity: 0.0}, 1000).removeClass('show');

			}
		}
	};
	  
	$.fn.simpleSlide = function(method) {
		// Method calling logic
		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.tooltip' );
		}   
	};
})( jQuery );

