( function( $ ){
	$.extend( $.prototype, {
		gallery: function( settings ){
			settings = $.extend( {
				elms: [],
				interval: 3000,
				fade: 1000,
				lastElm: "<a href=\"http://jiyugaoka.furari.to/inshoku/cafe/%E6%A5%B5%E4%B8%8A%E3%81%AE%E3%81%8A%E8%8C%B6%E3%82%92%E6%A5%BD%E3%81%97%E3%82%80%E3%81%AA%E3%82%89%E3%80%80lupicia\" title=\"極上のお茶を楽しむなら　LUPICIA\"><img src=\"http://jiyugaoka.furari.to/wp-content/uploads/2008/10/e794bbe5838f-0052.jpg\" width=\"1024\" height=\"768\" class=\"attachment-default\" alt=\"\" /></a>",
				rotateLimit: 0
			}, settings );
			if( !settings.elms.length ) return;
			var p = this, i = p.children( 'a:first' ).attr( 'src' ) == $(settings.elms[0]).attr('src')? 1: 0;
			var hi = function(){
				clearInterval( slide );
				$( this ).append( '<span>' + $( this ).attr( 'title' ) + '</span>' );
				$( this ).children( 'span' ).css( 'top', ( $( this ).height() - $( this ).children( 'span' ).height() ) / 2 -  $( this ).children( 'span' ).css( 'border-top-width').match( /[0-9]+/ ) ).animate( { width: $( this ).width() } );
			}
			var ho = function(){
				clearInterval( slide );
				$( this ).children( 'span' ).css('left', '0' ).animate( { width: 0 }, function(){
					$( this ).remove();
				} );
				if( limit <= settings.rotateLimit )
					slide = setInterval( fn , settings.interval );
			}
			$( this[0].firstChild ).hover( hi, ho );
			var limit = 0;
			var fn = function(){
				p.append( $( settings.elms[i] ).css( 'display','none' ).hover( hi, ho ) );
				p.children( 'a:last' )
					.css( 'left', Math.floor( ( p.width() - p.children( 'a:last' ).width() ) / 2 ) + 'px' )
					.css( 'top', Math.floor( ( p.height() - p.children( 'a:last' ).height() ) / 2 ) + 'px' )
					.fadeIn( settings.fade );
				p.children( 'a:first' ).fadeOut( settings.fade, function(){
					$( this ).remove();
				} );
				i = i >= settings.elms.length - 1? 0: i + 1;
				if( i == 0 ) limit++;
				if( limit > settings.rotateLimit ){
					clearInterval( slide );
					var to = setTimeout( function(){
						p.append( $( settings.lastElm ).css( 'display','none' ).hover( hi, ho ) );
						p.children( 'a:last' )
							.css( 'left', Math.floor( ( p.width() - p.children( 'a:last' ).width() ) / 2 ) + 'px' )
							.css( 'top', Math.floor( ( p.height() - p.children( 'a:last' ).height() ) / 2 ) + 'px' )
							.fadeIn( settings.fade );
						p.children( 'a:first' ).fadeOut( settings.fade, function(){
							$( this ).remove();
						} );
					}, settings.interval );
				}
			};
			var slide = setInterval( fn , settings.interval );
		}
	} );
} )( jQuery );
