jQuery.fn.eslideshow = function()
{
	this.each( function()
	{
		//get current object
		var t = jQuery( this );
		
		//setup the items
		var items = [];
		var current_item = -1;
		var pause = false;
		var counter = 4000;
			
		var timer;
				
		//setup the class
		t.addClass( 'eslideshow' );
		
		t.mouseover( function() { pause = true; clearTimeout( timer ); } ).mouseout( function() { pause = false; timer = setTimeout( "this.showImage()", counter ); } );
		
		//setup caption and nav
		var t_nav = $( '<div class="eslideshow_nav"></div>' );
		var t_caption = $( '<div class="eslideshow_caption"></div>' );
		var t_caption_content = $( '<div class="eslideshow_caption_content"></div>' );
		t_caption.append( t_caption_content );
		
		//get content
		var t_content = $( '> *', t );
		
		var i = 0;
		
		$( t_content ).each( function() {
		
			var c = $( this );
			c.addClass( 'eslideshow_item' );
			
			//get content
			var c_i = $( 'img', c ).clone( true );
			
			$( 'img', c ).replaceWith( '' );
			
			var c_c = c.html();
			
			c.html( '' ).append( c_i );
			
			c_item = $( '<div class="eslideshow_caption_item">' + c_c + '</div>' );
			c_item.css( 'opacity', '0' );
			
			t_caption_content.append( c_item );
			
			var f = i;
			
			c_button = $( '<a href="#" class="eslideshow_nav_button">' + ( i + 1 ) + '</a>' ).click( function() {
				forceShowImage( f, c, c_item, this );
				return false;
			} );
			
			t_nav.append( c_button );
			
			var item = [ c, c_item, c_button ];
			
			items.push( item );
			
			i++;			
		} );
		
		//append to slideshow
		t.append( t_nav ).append( t_caption );

		showImage = function( first_time ) {
			
		   if( $.browser.msie && first_time ) {
			$( '.eslideshow_item' ).css( 'opacity', '0' );
		   }
			
		   if( ! pause )
		   {
			   var active = current_item;
			 
			   //use this to pull the images in the order they appear in the markup
			   var next =  current_item + 1;
			   if( next >= items.length ) next = 0;
			   
			   if( active >= 0 )
			   { 
			    items[active][0].animate({opacity: 0.0}, 1000 );
			    items[active][1].animate({opacity: 0.0}, 1000 );
			    items[active][2].removeClass( 'eslideshow_nav_button_active' );
			   }
			   items[next][0].animate({opacity: 1.0}, 1000 );
			   items[next][1].animate({opacity: 1.0}, 1000 );
			   items[next][2].addClass( 'eslideshow_nav_button_active' );
			   			  		  
			   current_item = next;
			   if( items.length == 1 ) return;
			   timer = setTimeout( "this.showImage()", counter );
			}
		}
		
		forceShowImage = function( n, img, item, button ) {
			
			if( items.length == 1 ) return;
			
			clearTimeout( timer );
			
			counter = 4000;
			if( n == current_item ) return;
			var active = current_item;
			var next = n;
			current_item = next;
			
			if( active >= 0 )
			{ 
			 items[active][0].animate({opacity: 0.0}, 1000 );
			 items[active][1].animate({opacity: 0.0}, 1000 );
			 items[active][2].removeClass( 'eslideshow_nav_button_active' );
			}
			items[next][0].animate({opacity: 1.0}, 1000 );
			items[next][1].animate({opacity: 1.0}, 1000 );
			items[next][2].addClass( 'eslideshow_nav_button_active' );
			
		}
		
		showImage( true );
						
	});
}

