var LighboxCreator = ( function() {
	return function( realImageContainer ) {
		var currentItem = null;
		var realImg = null;
		var lighBoxImages = $( '<div style="display: none"></div>' );
		var readyImages = null;
		var description = null;

		this.createImage = function( n ) {
			currentItem = $( this );
			realImg = currentItem.find( realImageContainer ).html();
			description = currentItem.find( '.imgDescription' ).html();

			lighBoxImages.append( $(
				'<a href="' + realImg + '" title="' + description + '">' +
					'<img src="' + realImg + '" alt="" />' +
				'</a>'
			));
		};

		this.getImages = function() { 
			if( readyImages === null ) {
				readyImages = $( lighBoxImages ).find( 'a' );
			}
			return readyImages;
		};

		this.generateLightBox = function( prevPressedHandler, nextPressedHandler ) {
			this.getImages().lightBox({
				imageLoading:		'/images/lightbox/lightbox-ico-loading.gif',
				imageBtnClose:		'/images/lightbox/lightbox-btn-close.gif',
				imageBtnPrev:		'/images/lightbox/lightbox-btn-prev.gif',
				imageBtnNext:		'/images/lightbox/lightbox-btn-next.gif',
				imageBlank:			'/images/lightbox/lightbox-blank.gif',
				fixedNavigation:	true,
		    	onPrevPress:		prevPressedHandler || function(){},
				onNextPress:		nextPressedHandler || function(){}
			});
		};
	}
})();


( function( $ ) {
	$.fn.stepGallery = function( settings ) {
		for( var elt in settings ) {
			if( settings[elt] == null ) {
				throw new Error( "Не установлен '" + elt + "'" );
			}
		}

		var gallerySettings = $.extend({
			galleryid: null,
			beltclass: null,
			panelclass: null,
			viewPort: null,
			realImgInfoContainer: '.lightBoxImg',
			titleContainer: '.imgTitle',
			descriptionContainer: '.imgDescription',
			stepPrevAndSetImage: null,
			stepNextAndSetImage: null,
			galleryDefaultbuttons: null,
			dehighlightActive: function(){},
			highlightActive: function(){},
			speed: 500,
			wraparound: false
		}, settings || {} );

		var gallery = {
			thumbsItems:	null,
			readyImages:	null,
			viewPort:		null,
			activeImage:	0,

			itemClick: function( target ) {
				var index = gallery.thumbsItems.index( $( target ).parent() );
				if( index == gallery.activeImage ) {
					return;
				}

				//$( gallery.readyImages.get( index ) ).trigger( 'click' );
				//gallery.viewPort.html( gallery.readyImages.[index] );

				gallery.displayInViewPort( index );

				gallerySettings.dehighlightActive.call( this, $( gallery.thumbsItems.get( gallery.activeImage ) ).find( 'img' ) );
				gallery.activeImage = index;
				gallerySettings.highlightActive.call( this, target );

				distortFooter();
			},

			displayInViewPort: function( index ) {
				var img = $( 
					'<img src="' + 
					$( gallery.thumbsItems.get( index ) ).find( '.realImg' ).html() +
					'" alt ="" />' );

				img.click( function( event ){
					$( gallery.readyImages.get( index ) ).trigger( 'click' );
				});

				gallery.viewPort.html( img );
			},

			setImage: function( options ) {
				stepcarousel.stepTo( gallerySettings.galleryid, options.activeImage + 1 );
				gallery.itemClick( $( gallery.thumbsItems.get( options.activeImage ) ).find( 'img' ) );
			},

			makeStepAndSetImage: function( event ) {
				var index = gallery.activeImage + event.data.direction;
				if( index < 0 || index > gallery.readyImages.length - 1 ) {
					return;
				}

				gallery.setImage( { activeImage: index } );
			}
		};

		var lighboxCreator = new LighboxCreator( gallerySettings.realImgInfoContainer );

		$( document ).ready( function() {
			gallery.thumbsItems = $( '#' + gallerySettings.galleryid + ' .' + gallerySettings.panelclass );

			gallery.thumbsItems.each( lighboxCreator.createImage );
			gallery.readyImages = lighboxCreator.getImages();
			gallery.viewPort = $( gallerySettings.viewPort );
			//gallery.viewPort.html( gallery.readyImages.get( 0 ) );
			gallery.displayInViewPort( 0 );

			lighboxCreator.generateLightBox( gallery.setImage, gallery.setImage );

			if( gallerySettings.stepPrevAndSetImage !== null && gallerySettings.stepNextAndSetImage !== null ) {
				$( gallerySettings.stepPrevAndSetImage ).bind( 'click', { direction: -1 }, gallery.makeStepAndSetImage );
				$( gallerySettings.stepNextAndSetImage ).bind( 'click', { direction:  1 }, gallery.makeStepAndSetImage );
			}

			gallerySettings.highlightActive.call( this, $( gallery.thumbsItems.get( gallery.activeImage ) ).find( 'img' ) );

			distortFooter();
		});

		this.buildBar = function() {
			stepcarousel.setup({
				galleryid: gallerySettings.galleryid,
				beltclass: gallerySettings.beltclass,
				panelclass: gallerySettings.panelclass,
				panelbehavior: {
					speed: gallerySettings.speed,
					wraparound: gallerySettings.wraparound,
					persist: false
				},
				defaultbuttons: {
					enable: false
				},
	
				onpanelclick: gallery.itemClick,
				contenttype: ['inline'],
				defaultbuttons: gallerySettings.galleryDefaultbuttons
			});
		}

		return this;
	};
})( jQuery );