function imageGalleryElements()
{
	var position		= 0;
	var	x				= 0;
	var	y				= 0;
	var	width			= 0;
	var	height			= 0;
	var	margin			= 0;
	var	alpha			= 0;
	var	visibility		= false;
	var	image			= 0;
	var	obj				= 0;
	var	event			= 0;
	
	return {
		getPosition: function()
		{
			return position;
		},
		
		setPosition: function( vars )
		{
			position = vars;
		},
		
		getX: function()
		{
			return x;
		},
		
		setX: function( vars )
		{
			x = vars;
		},
		
		getY: function()
		{
			return y;
		},
		
		setY: function( vars )
		{
			y = vars;
		},
		
		getWidth: function()
		{
			return width;
		},
		
		setWidth: function( vars )
		{
			width = vars;
		},
		
		getHeight: function()
		{
			return height;
		},
		
		setHeight: function( vars )
		{
			height = vars;
		},
		
		getMargin: function()
		{
			return margin;
		},
		
		setMargin: function( vars )
		{
			margin = vars;
		},
		
		getAlpha: function()
		{
			return alpha;
		},
		
		setAlpha: function( vars )
		{
			alpha = vars;
		},
		
		getImage: function()
		{
			return image;
		},
		
		setImage: function( vars )
		{
			image = vars;
		},
		
		getObj: function()
		{
			return obj;
		},
		
		setObj: function( vars )
		{
			obj = vars;
		},
		
		getObjLeft: function()
		{
			return $( obj ).css( 'left' );
		},
		
		getObjTop: function()
		{
			return $( obj ).css( 'top' );
		},
		
		setObjLeft: function( vars )
		{
			$( obj ).css( 'left', vars );
		},
		
		setObjTop: function( vars )
		{
			$( obj ).css( 'top', vars );
		},
		
		setEventToObj: function()
		{

		},
		
		updateImage: function()
		{
			$( this.getObj() ).html( '' );
			$( this.getObj() ).append( this.getImage() );
		},
		
		updatePosition: function()
		{
			$( this.getObj() ).attr( 'position', this.getPosition() );
		}
	}
}

function imageGalleryProcessor()
{
	return {
	
		initialize: function( options, images, specialBlock, top, bottom )
		{
			this.options = options;
			this.images = images;
			this.specialBlock = specialBlock;
			this.galleryTop = top;
			this.galleryBottom = bottom;
			this.galleryStart = false;
		},
		
		calculateSize: function()
		{
			if( this.options.topHeight == 0 )
			{
				this.options.topHeight = this.galleryTop.height();
			}
			if( this.options.topWidth == 0 )
			{
				this.options.topWidth = this.galleryTop.width();
			}
			if( this.options.bottomHeight == 0 )
			{
				this.options.bottomHeight = this.galleryBottom.height();
			}
			if( this.options.bottomWidth == 0 )
			{
				this.options.bottomWidth = this.galleryBottom.width();
			}
		},
		
		setSize: function()
		{
			//$( this.galleryTop ).animate( { 'height': this.options.topHeight }, 1000 );
			//$( this.galleryTop ).animate( { 'width': this.options.topWidth }, 1000 );
			//$( this.galleryBottom ).animate( { 'height': this.options.bottomHeight }, 1000 );
			//$( this.galleryBottom ).animate( { 'width': this.options.bottomWidth }, 1000 );
			
			$( this.galleryTop ).css( 'height', this.options.topHeight  );
			$( this.galleryTop ).css( 'width', this.options.topWidth );
			$( this.galleryBottom ).css( 'height', this.options.bottomHeight );
			$( this.galleryBottom ).css( 'width', this.options.bottomWidth );
		},
		
		calculateImageCount: function()
		{
			if ( this.options.position == "vertical" )
			{
				var height = this.options.bottomHeight;
				var imageHeight = this.options.imageSize.height;
				var margin = 2 * this.options.imageMargin;
				var count = Math.round( height / ( imageHeight + margin ) );
				this.options.imageCount = count + 2;
			}
			else
			{
				var width = this.options.bottomWidth;
				var imageWidth = this.options.imageSize.width;
				var margin = 2 * this.options.imageMargin;
				var count = Math.round( width / ( imageWidth + margin ) );
				this.options.imageCount = count + 2;
			}
		},
		
		setFirstPosition: function( el )
		{
			var shift = 0;
			if( this.options.position == "vertical" )
			{
				var imageHeight = this.options.imageSize.height;
				var margin = 2 * this.options.imageMargin;
				var elPos = el.getPosition();
				pos = ( imageHeight + margin ) * ( elPos - shift );
				el.setY( pos );
			}
			else
			{
				var imageWidth = this.options.imageSize.width;
				var margin = 2 * this.options.imageMargin;
				var elPos = el.getPosition();
				pos = ( imageWidth + margin ) * ( elPos - shift );
				el.setX( pos );
			}
		},
		
		createElements: function()
		{
			var els = new Array();
			var count = this.options.imageCount;
			for ( var i = 0; i < count; i++ )
			{
				var img = this.getImg( 'secondSmall', i );
				els[i] = new  imageGalleryElements();
				els[i].setPosition( i );
				els[i].setX( 0 );
				els[i].setY( 0 );
				els[i].setWidth( this.options.imageSize.width );
				els[i].setHeight( this.options.imageSize.height );
				els[i].setMargin( this.options.imageMargin );
				if( this.options.fadeNonActiveImages.status )
				{
					els[i].setAlpha( this.options.fadeNonActiveImages.fadeOpacity );
				}
				else
				{
					els[i].setAlpha( 1 );
				}
				els[i].setImage( $( img ).clone() );
				this.setFirstPosition( els[i] );
			}
			return els;
		},
		
		getUrlByID: function( size, id )
		{
			if( this.options.secondThumbnail )
			{
				if ( size == "large" )
				{
					return $( this.images[ (id % this.options.galleryImageCount) * 3 + 1 ] ).attr( 'src' );
				}
				else if( size == "secondSmall" )
				{
					return $( this.images[ (id % this.options.galleryImageCount ) * 3 + 2 ] ).attr( 'src' );
				}
				else
				{
					return $( this.images[ (id % this.options.galleryImageCount ) * 3 ] ).attr( 'src' );
				}
			}
			else
			{
				if ( size == "large" )
				{
					return $( this.images[ (id % this.options.galleryImageCount) * 2 + 1 ] ).attr( 'src' );
				}
				else
				{
					return $( this.images[ (id % this.options.galleryImageCount ) * 2 ] ).attr( 'src' );
				}
			}
		},
		
		getImg: function( size, id )
		{
			if( this.options.specialBlockLikeImage )
			{
				return this.getSpecialBlock( id );
			}
			if( this.images )
			{
				if( this.options.secondThumbnail )
				{
					var img = 0;
					if ( size == "large" )
					{
						if( id < 0 )
						{
							img = this.images[ (this.options.galleryImageCount - (Math.abs(id) % this.options.galleryImageCount)) * 3 + 1 ];
						}
						else
						{
							img = this.images[ (id % this.options.galleryImageCount) * 3 + 1 ];
						}
						return img;
					}
					else if( size == "secondSmall" )
					{
						if( id < 0 )
						{
							img = $( this.images[ (this.options.galleryImageCount - (Math.abs(id) % this.options.galleryImageCount)) * 3 + 2 ] ).removeAttr( 'width' );
						}
						else
						{
							img = $( this.images[ (id % this.options.galleryImageCount) * 3 + 2 ] ).removeAttr( 'width' );
						}
						img = $( img ).removeAttr( 'height' );
						img = $( img ).attr( 'class', 'image-item' );
						return img;
					}
					else
					{
						if( id < 0 )
						{
							img = $( this.images[ (this.options.galleryImageCount - (Math.abs(id) % this.options.galleryImageCount)) * 3 ] ).removeAttr( 'width' );
						}
						else
						{
							img = $( this.images[ (id % this.options.galleryImageCount) * 3 ] ).removeAttr( 'width' );
						}
						img = $( img ).removeAttr( 'height' );
						img = $( img ).attr( 'class', 'image-item' );
						return img;
					}
				}
				else
				{
					var img = 0;
					if ( size == "large" )
					{
						if( id < 0 )
						{
							img = this.images[ (this.options.galleryImageCount - (Math.abs(id) % this.options.galleryImageCount)) * 2 + 1 ];
						}
						else
						{
							img = this.images[ (id % this.options.galleryImageCount) * 2 + 1 ];
						}
						return img;
					}
					else
					{
						if( id < 0 )
						{
							img = $( this.images[ (this.options.galleryImageCount - (Math.abs(id) % this.options.galleryImageCount)) * 2 ] ).removeAttr( 'width' );
						}
						else
						{
							img = $( this.images[ (id % this.options.galleryImageCount) * 2 ] ).removeAttr( 'width' );
						}
						img = $( img ).removeAttr( 'height' );
						img = $( img ).attr( 'class', 'image-item' );
						return img;
					}
				}
			}
			else
			{
				return false;
			}
		},
		
		getSpecialBlock: function( id )
		{
			if ( this.options.existSpecialBlock )
			{
				var block = $( this.specialBlock[ id % this.options.specialBlockCount ] ).clone();
				return block;
			}
			else
			{
				return false;
			}
		},
		
		showElements: function( els )
		{
			var temp = this;
			$.each( els, function( index, el ) 
			{
				var pic = $( '<div></div>' )
					.attr( 'class', 'item' )
					.attr( 'id', 'item' + index )
					.attr( 'position', el.getPosition() )
					.css( 'width', el.getWidth() )
					.css( 'height', el.getHeight() )
					.css( 'top', el.getY() )
					.css( 'left', el.getX() )
					.css( 'display', 'none' );
				
				$( pic ).fadeTo( 100, el.getAlpha(), function(){} );
				var img = el.getImage();
				
				$( pic ).append( img );
				$( temp.galleryBottom ).append( pic );
				$( pic ).fadeIn(500);
				el.setObj( pic );
				el.setEventToObj();
				if ( temp.options.cufonForSpecialBlock )
				{
					temp.options.cufonFunction( '#item' + index );
				}
			} );
		},
		
		moveElements: function( els )
		{
			if ( this.options.position == "vertical" )
			{
				$.each( els, function( index, el )
				{
					el.setObjTop( el.getY() );
				} );
			}
			else
			{
				$.each( els, function( index, el )
				{
					el.setObjLeft( el.getX() );
				} );
			}
		},
		
		checkAdditionalImage: function( el, x, a )
		{
			if ( this.options.position == "vertical" )
			{
				var nextY = x;
				if ( this.a > 0 )
				{
					if ( x > ( this.options.bottomHeight + ( this.options.imageSize.height / 2 ) ) )
					{
						var nextY = x - this.options.imageCount * ( this.options.imageSize.height + this.options.imageMargin * 2 );
						var newPosition = el.getPosition() - this.options.imageCount;
						if( newPosition < 0 )
						{
							newPosition = newPosition + this.options.galleryImageCount;
						}
						if( newPosition >= this.options.galleryImageCount )
						{
							newPosition = newPosition - this.options.galleryImageCount;
						}
						el.setPosition( newPosition );
						if( ( el.getAlpha() != 1 ) && ( el.getPosition() != this.options.currentImage ) )
						{
							$( el.getObj() ).fadeTo( 0, this.options.fadeNonActiveImages.fadeOpacity, function(){} );
						}
						if( newPosition == this.options.currentImage )
						{
							el.setAlpha( 1 );
							$( el.getObj() ).fadeTo( 100, el.getAlpha(), function(){} );
						}
						el.setImage( this.getImg( 'small', el.getPosition() ).clone() );
						el.updateImage();
						el.updatePosition();
					}
				}
				else
				{
					if ( x < ( this.options.imageSize.height * ( -1.5 ) - this.options.imageMargin ) )
					{
						var nextY = x + this.options.imageCount * ( this.options.imageSize.height + this.options.imageMargin * 2 );
						var newPosition = el.getPosition() + this.options.imageCount;
						if( newPosition < 0 )
						{
							newPosition = newPosition + this.options.galleryImageCount;
						}
						if( newPosition >= this.options.galleryImageCount )
						{
							newPosition = newPosition - this.options.galleryImageCount;
						}
						el.setPosition( newPosition );
						if( ( el.getAlpha() != 1 ) && ( el.getPosition() != this.options.currentImage ) )
						{
							$( el.getObj() ).fadeTo( 0, this.options.fadeNonActiveImages.fadeOpacity, function(){} );
						}
						if( newPosition == this.options.currentImage )
						{
							el.setAlpha( 1 );
							$( el.getObj() ).fadeTo( 100, el.getAlpha(), function(){} );
						}
						el.setImage( this.getImg( 'small', el.getPosition() ).clone() );
						el.updateImage();
						el.updatePosition();
					}
				}
				return nextY;
			}
			else
			{
				var nextX = x;
				if ( this.a > 0 )
				{
					if ( x > ( this.options.bottomWidth + ( this.options.imageSize.width / 2 ) ) )
					{
						
						var nextX = x - this.options.imageCount * ( this.options.imageSize.width + this.options.imageMargin * 2 );
						var newPosition =  el.getPosition() - this.options.imageCount;
						if( newPosition < 0 )
						{
							newPosition =  newPosition + this.options.galleryImageCount;
						}
						if( newPosition >= this.options.galleryImageCount )
						{
							newPosition = newPosition - this.options.galleryImageCount;
						}
						el.setPosition( newPosition );
						if( ( el.getAlpha() != 1 ) && ( el.getPosition() != this.options.currentImage ) )
						{
							$( el.getObj() ).fadeTo( 0, this.options.fadeNonActiveImages.fadeOpacity, function(){} );
						}
						if( newPosition == this.options.currentImage )
						{
							el.setAlpha( 1 );
							$( el.getObj() ).fadeTo( 100, el.getAlpha(), function(){} );
							el.setImage( this.getImg( 'small', el.getPosition() ).clone() );
						}
						else
						{
							if( this.options.secondThumbnail )
							{
								el.setImage( this.getImg( 'secondSmall', el.getPosition() ).clone() );
							}
							else
							{
								el.setImage( this.getImg( 'small', el.getPosition() ).clone() );
							}
						}
						el.updateImage();
						el.updatePosition();
					}
				}
				else
				{
					if ( x < ( this.options.imageSize.width * ( -1.5 ) - this.options.imageMargin ) )
					{
						var nextX = x + this.options.imageCount * ( this.options.imageSize.width + this.options.imageMargin * 2 );
						var newPosition =  el.getPosition() + this.options.imageCount;
						if( newPosition < 0 )
						{
							newPosition =  newPosition + this.options.galleryImageCount;
						}
						if( newPosition >= this.options.galleryImageCount )
						{
							newPosition =  newPosition - this.options.galleryImageCount;
						}
						el.setPosition( newPosition );
						if( ( el.getAlpha() != 1 ) && ( el.getPosition() != this.options.currentImage ) )
						{
							$( el.getObj() ).fadeTo( 0, this.options.fadeNonActiveImages.fadeOpacity, function(){} );
						}
						if( newPosition == this.options.currentImage )
						{
							el.setAlpha( 1 );
							$( el.getObj() ).fadeTo( 100, el.getAlpha(), function(){} );
							el.setImage( this.getImg( 'small', el.getPosition() ).clone() );
						}
						else
						{
							if( this.options.secondThumbnail )
							{
								el.setImage( this.getImg( 'secondSmall', el.getPosition() ).clone() );
							}
							else
							{
								el.setImage( this.getImg( 'small', el.getPosition() ).clone() );
							}
						}
						el.updateImage();
						el.updatePosition();
					}
				}
				return nextX;
			}
		},
		
		calculateMovingImage: function( els )
		{
			if( this.options.movingStarted )
			{
				this.a = this.options.movingOn;
				this.options.moveTo = this.options.moveTo - this.step;
				if( Math.abs( this.options.moveTo ) < Math.abs( this.step ) )
				{
					this.step = this.options.moveTo;
					this.options.moveTo = 0;
					this.options.movingStarted = false;
				}
				var temp = this;
				if ( this.options.position == "vertical" )
				{
					$.each( els, function( index, el )
					{
						var currentY = el.getY();
						var nextY = temp.checkAdditionalImage( el, currentY + temp.step, temp.options.movingOn );
						el.setY( Math.round( nextY ) );
					} );

				}
				else
				{
					$.each(els, function( index, el )
					{
						var currentX = el.getX();
						var nextX = temp.checkAdditionalImage( el, currentX + temp.step, temp.options.movingOn );
						el.setX( Math.round( nextX ) );
					} );
				}
			}
			else
			{
				this.options.movingStarted = true;
				if ( this.options.position == "vertical" )
				{
					this.options.moveTo = this.options.movingOn * ( this.options.imageSize.height + this.options.imageMargin * 2 );
				}
				else
				{
					this.options.moveTo = this.options.movingOn * ( this.options.imageSize.width + this.options.imageMargin * 2 );
				}
				this.step = Math.round( this.options.moveTo / this.options.movingIterationCount );
				this.options.moveTo += this.step;
			}
		},
		
		calculateIteration: function( els, a )
		{
			this.a = a;
			if ( !this.galleryStart )
			{
				this.startTime = new Date().getTime();
				this.galleryStart = true;
			}
			this.currentTime = new Date().getTime();
			var shiftSpeed = ( this.currentTime - this.startTime ) / this.options.speed;
			this.startTime = this.currentTime;
			if ( !this.options.autoScrollOn )
			{
				var moveTo = this.options.staticAutoScroll;
				if ( this.options.mouseEnter )
				{
					moveTo = 0;
				}
				var diff = ( this.options.autoScroll + 1000 ) - ( moveTo + 1000 );
				this.options.autoScroll = this.options.autoScroll - ( ( diff * ( ( this.options.movePercentage ) / 100 ) ) * shiftSpeed ) ;
				if ( ( this.options.autoScroll <= ( moveTo + 0.1 ) ) && ( this.options.autoScroll >= ( moveTo - 0.1 ) ) )
				{
					this.options.autoScroll = moveTo;
					this.options.autoScrollOn = true;
				}
			}
			
			var temp = this;
			if ( this.options.position == "vertical" )
			{
				$.each( els, function( index, el )
				{
					var step = temp.a * temp.options.interval * shiftSpeed;
					var currentY = el.getY();
					var nextY = temp.checkAdditionalImage( el, currentY + step, temp.a );
					el.setY( Math.round( nextY ) );
				} );

			}
			else
			{
				$.each(els, function( index, el )
				{
					var step = temp.a * temp.options.interval * shiftSpeed;
					var currentX = el.getX();
					var nextX = temp.checkAdditionalImage( el, currentX + step, temp.a );
					el.setX( Math.round( nextX ) );
				} );
			}
		},
		
		hideLastImages: function( els )
		{
			var temp = this;
			if( this.options.fadeNonActiveImages.status )
			{
				$.each( els, 
					function(index, item)
					{
						if( $( item.getObj() ).attr( 'position' ) == temp.options.currentImage )
						{
							$( item.getObj() ).fadeTo( temp.options.fadeNonActiveImages.fadeTime, temp.options.fadeNonActiveImages.fadeOpacity, function(){} );
						}
					}
				);
			}
			if( this.options.secondThumbnail )
			{
				$.each( els, 
					function(index, item)
					{
						if( $( item.getObj() ).attr( 'position' ) == temp.options.currentImage )
						{
							item.setImage( temp.getImg( 'secondSmall', item.getPosition() ).clone() );
							item.updateImage();
						}
					}
				);
			}
		},
		
		showNewImages: function( els )
		{
			var temp = this;
			if( this.options.fadeNonActiveImages.status )
			{
				$.each( els, 
					function(index, item)
					{
						if( $( item.getObj() ).attr( 'position' ) == temp.options.currentImage )
						{
							$( item.getObj() ).fadeTo( 0, 1, function(){} );
						}
					}
				);
			}
			if( this.options.secondThumbnail )
			{
				$.each( els, 
					function(index, item)
					{
						if( $( item.getObj() ).attr( 'position' ) == temp.options.currentImage )
						{
							item.setImage( temp.getImg( 'small', item.getPosition() ).clone() );
							item.updateImage();
						}
					}
				);
			}
		}
	}
}

function imageGalleryOutputSystem()
{
	return {
		
		initialize: function( href, descriptionStatus, description, specialBlockPlace, fadingTopImage, topImageHref, galleryTop )
		{
			this.href = href;
			this.descriptionStatus = descriptionStatus;
			this.specialBlockPlace = specialBlockPlace;
			this.description = description;
			this.fadingTopImage = fadingTopImage;
			this.topImageHref = topImageHref;
			this.galleryTop = galleryTop;
			this.last = -1;
		},
		
		outputOnBlock: function( currentImage, typeOfShow, image, specialBlock, width, height )
		{
			this.galleryTop.html('');
			
			this.center = $( '<div></div>' ).attr( 'class', 'nxc-block-center' );
			//this.buttons = $( '<a class="btn-previous" href="#">&nbsp;</a><a class="btn-next" href="#">&nbsp;</a>' );
			
			//$( this.galleryTop ).append( this.buttons );
			$( this.galleryTop ).append( this.center );
			
			if( shadow ) {
				var shadow = $( '<div></div>' ).attr( 'class', 'nxc-block-bg-wrap' );
				$( this.center ).append( shadow );
				$.each( ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'], function( index, dir ) {
					$( shadow ).append( $( '<div></div>' ).attr( 'class', 'nxc-block-bg nxc-block-bg-' + dir ) );
				} );
			}
			
			var imageWidth = 0;
			var imageHeight = 0;
			var imageTop = 0;
			var imageLeft = 0;
			var imageSize = { 'width': $( image ).attr( 'width' ), 'height': $( image ).attr( 'height' ) }
			if ( typeOfShow == '1' )
			{
				imageWidth = imageSize.width;
				imageHeight = imageSize.height;
				imageTop = ( height - imageHeight ) / 2;
				imageLeft = ( width - imageWidth ) / 2;
			}
			if ( typeOfShow == '2' )
			{
				imageWidth = width;
				imageHeight = height;
				imageTop = ( height - imageHeight ) / 2;
				imageLeft = ( width - imageWidth ) / 2;
			}
			if ( typeOfShow == '3' )
			{
				if ( imageSize.width > width )
				{
					var shift  = width / imageSize.width;
					imageWidth = width;
					imageHeight = imageSize.height * shift;
				}
				imageTop = ( height - imageHeight ) / 2;
				imageLeft = ( width - imageWidth ) / 2;
			}
			if ( typeOfShow == '4' )
			{
				if ( imageSize.height > height )
				{
					var shift  = height / imageSize.height;
					imageHeight = height;
					imageWidth = imageSize.width * shift;
				}
				imageTop = ( height - imageHeight ) / 2;
				imageLeft = ( width - imageWidth ) / 2;
			}
			if ( typeOfShow == '5' )
			{
				var shiftHeight = imageSize.height / height;
				var shiftWidth = imageSize.width / width;
				if ( ( shiftHeight > 1 ) && ( shiftWidth <= 1 ) )
				{
					imageHeight = height;
					imageWidth = imageSize.width / shiftHeight;
				}
				if ( ( shiftHeight <= 1 ) && ( shiftWidth > 1 ) )
				{
					imageWidth = width;
					imageHeight = imageSize.height / shiftWidth;
				}
				if ( ( shiftHeight > 1 ) && ( shiftWidth > 1 ) )
				{
					if ( shiftHeight > shiftWidth )
					{
						imageHeight = height;
						imageWidth = imageSize.width / shiftHeight;
					}
					else
					{
						imageWidth = width;
						imageHeight = imageSize.height / shiftWidth;
					}
				}
				if ( ( shiftHeight <= 1 ) && ( shiftWidth <= 1 ) )
				{
					imageWidth = imageSize.width;
					imageHeight = imageSize.height;
				}
				imageTop = ( height - imageHeight ) / 2;
				imageLeft = ( width - imageWidth ) / 2;
			}
			
			$( this.center ).css( 'position', 'absolute' );
			$( this.center ).css( 'width', imageWidth );
			$( this.center ).css( 'height', imageHeight );
			$( this.center ).css( 'top', imageTop );
			$( this.center ).css( 'left', imageLeft );
			$( this.center ).css( 'display', 'none' );
			
			$( image ).css( 'width', imageWidth );
			$( image ).css( 'height', imageHeight );
			
			if( this.topImageHref )
			{
				$( '<a target="_blank"></a>' ).attr( 'href', this.href[currentImage] ).append( image ).appendTo( $( this.center ) );
			}
			else
			{
				$( this.center ).append( image );
			}
			
			
			if( specialBlock )
			{
				$( this.center ).append( specialBlock );
				$( specialBlock ).show();
			}
			
			if( this.descriptionStatus )
			{
				$( this.specialBlockPlace ).text( $( this.description[ currentImage ] ).text() );
			}
			
			if( this.fadingTopImage != 0 )
			{
				$( this.center ).fadeIn( this.fadingTopImage );
			}
			else
			{
				$( this.center ).show();
			}
		},
				
		outputOnLightBox: function()
		{
			
		},
		
		outputStatus: function( currentImage, count )
		{
			if( this.last != currentImage )
			{
				var currentPage = $( 'div.pages span.currentpage' );
				var countPages = $( 'div.pages span.countpages' );
				$( currentPage ).text( currentImage );
				$( countPages ).text( count );
				this.last = currentImage;
			}
		}
	}
}

function imageGalleryKernel()
{
	return {
		//setting parametrs
		imageSize				: { 'width': 90, 'height': 60 }, //width, height
		imageMargin				: 2,
		aspectRatio				: 0.8,
		mouseClickTime			: 150,
		
		topImage				: true,
		topImageHref			: false,
		defaultTopImage			: true,
		fadingTopImage			: 800,
		shadow					: false,
		
		interval				: 2,
		autoScrollOn			: true,
		autoScroll				: 2,
		staticAutoScroll		: 0,
		
		existSpecialBlock		: false,
		specialBlockLikeImage	: false,
		cufonForSpecialBlock	: false,
		cufonFunction			: false,
		
		secondThumbnail			: false,
		
		speed					: 20,
		movePercentage			: 5,
		typeOfShow				: 5, // it can be= 1, 2, 3, 4, 5
		position				: 'horizontal', //it can be horizontal or vertical. default value is horizontal. ( If you want to vertical then you need to set height in the block where will be images )
		possibleClicking		: true,
		possibleScrollButton	: true,
		specialScrollButton		: false,
		possibleScrollWheel		: true,
		
		bottomHeight			: 'auto',
		movingOn				: 4,
		movingIterationCount	: 20,
		movingIterationTime		: 100,
		dontRepeatImages		: false,
		autoScrolling			: { 'status' : false, 'time' : 10000, 'to' : 'right' },
		fadeNonActiveImages		: { 'status' : false, 'fadeTime' : 300, 'fadeOpacity' : 0.3 },
		
		existDescription		: false,
		
		movingWhenMouseEnter	: true,
		
		//other script parametrs
		height					: 0,
		topHeight				: 0,
		topWidth				: 0,
		bottomWidth				: 0,
		imageCount				: 0,
		galleryImageCount		: 0,
		specialBlockCount		: 0,
		lastMousePosition 		: { 'x': 0, 'y': 0 }, // x, y
		galleryClicked			: false,
		mouseEnter				: false,
		currentImage			: 0,
		movingStarted			: false,
		ln						: 0,
		innertion				: 2,
		galleryIsReady 			: false,
		scrolling				: true,
		
		
		initialize: function( images, description, specialBlock, href, specialBlockPlace, top, bottom, controlLeft, controlRight )
		{
			
			this.images = images;
			this.description = description;
			this.specialBlock = specialBlock;
			this.href = href;
			this.specialBlockPlace = $( specialBlockPlace);
			this.galleryTop = $( top );
			this.galleryBottom = $( bottom );
			this.controlLeft = $( controlLeft );
			this.controlRight = $( controlRight );
			
			if ( !this.specialBlockLikeImage )
			{
				this.loadingImages();
			}
			else
			{
				this.loadedImages = false;
			}
			
			if ( this.existSpecialBlock )
			{
				this.loadingSpecialBlock();
				if( this.specialBlockLikeImage )
				{
					this.run();
				}
			}
			else
			{
				this.loadedSpecialBlock = false;
			}
		},
		
		destroyGallery: function()
		{
			window.clearInterval( this.galleryInterval );
		},
		
		run: function()
		{
			
			this.output = new imageGalleryOutputSystem();
			this.output.initialize( this.href, this.existDescription, this.description, this.specialBlockPlace, this.fadingTopImage, this.topImageHref, this.galleryTop );
			this.proc = new imageGalleryProcessor();
			this.proc.initialize( this, this.loadedImages, this.loadedSpecialBlock, this.galleryTop, this.galleryBottom );
			this.proc.calculateSize();
			
			this.proc.setSize();
			
			this.proc.calculateImageCount();
			if ( this.dontRepeatImages )
			{
				if ( !this.specialBlockLikeImage )
				{
					if( ( this.imageCount - 2 ) > this.galleryImageCount )
					{
						this.imageCount = this.galleryImageCount;
						this.possibleClicking = false;
						this.possibleScrollButton = false;
						this.possibleScrollWheel = false;
						this.scrolling = false;
					}
				}
				else
				{
					if( ( this.imageCount - 2 ) > this.specialBlockCount )
					{
						this.imageCount = this.specialBlockCount;
						this.possibleClicking = false;
						this.possibleScrollButton = false;
						this.possibleScrollWheel = false;
						this.scrolling = false;
					}
				}
			}
			this.els = this.proc.createElements();
			this.proc.showElements( this.els );
			if( this.topImage )
			{
				$( this.els[ this.currentImage ].getObj() ).fadeTo( this.fadeTime, 1, function(){} );
				if( this.secondThumbnail )
				{
					this.els[ this.currentImage ].setImage( this.proc.getImg( 'small', this.currentImage ).clone() );
					this.els[ this.currentImage ].updateImage();
				}
				this.output.outputOnBlock( this.currentImage, this.typeOfShow, this.proc.getImg( 'large', this.currentImage ), this.proc.getSpecialBlock( this.currentImage ) , this.topWidth, this.topHeight );
			}
			this.setEvents();
			var temp = this;
			var rotation = function()
			{
				if( temp.movingStarted )
				{
					temp.proc.calculateMovingImage( temp.els );
				}
				else
				{
					temp.proc.calculateIteration( temp.els, temp.autoScroll );
				}
				temp.proc.moveElements( temp.els );
				temp.output.outputStatus( parseInt(temp.currentImage) + 1, temp.galleryImageCount );
			};
			this.galleryInterval = window.setInterval( rotation, 10 );
			if ( this.specialBlockLikeImage )
			{
				var count = this.specialBlockCount;
			}
			else
			{
				var count = this.galleryImageCount;
			}
			var scrolling = function()
			{
				
				if ( temp.movingWhenMouseEnter )
				{
					if( !temp.movingStarted )
					{
						if( temp.topImage == true )
						{
							if( temp.fadeNonActiveImages.status )
							{
								$( temp.els[ temp.currentImage ].getObj() ).fadeTo( temp.fadeNonActiveImages.fadeTime, temp.fadeNonActiveImages.fadeOpacity, function(){} );
							}
							if( temp.secondThumbnail )
							{
								temp.els[ temp.currentImage ].setImage( temp.proc.getImg( 'secondSmall', temp.currentImage ).clone() );
								temp.els[ temp.currentImage ].updateImage();
							}
						}
						if ( temp.autoScrolling.to == 'left' )
						{
							temp.currentImage--;
							if( temp.currentImage < 0 )
							{
								temp.currentImage = count - 1;
							}
							temp.movingOn = Math.abs( temp.movingOn );
						}
						else
						{
							temp.currentImage++;
							if( temp.currentImage == count )
							{
								temp.currentImage = 0;
							}
							temp.movingOn = - Math.abs( temp.movingOn );
						}
						temp.proc.calculateMovingImage( temp.els );
						if( temp.topImage == true )
						{
							temp.output.outputOnBlock( temp.currentImage, temp.typeOfShow, temp.proc.getImg( 'large', temp.currentImage ), temp.proc.getSpecialBlock( temp.currentImage ) , temp.topWidth, temp.topHeight );
							$( temp.els[ temp.currentImage ].getObj() ).fadeTo( temp.fadeTime, 1, function(){} );
							if( temp.secondThumbnail )
							{
								temp.els[ temp.currentImage ].setImage( temp.proc.getImg( 'secondSmall', temp.currentImage ).clone() );
								temp.els[ temp.currentImage ].updateImage();
							}
						}
					}
				}
				else
				{
					if( !temp.mouseEnter )
					{
						if( !temp.movingStarted )
						{
							if( temp.topImage == true )
							{
								temp.proc.hideLastImages( temp.els );
							}
							if ( temp.autoScrolling.to == 'left' )
							{
								temp.currentImage--;
								if( temp.currentImage < 0 )
								{
									temp.currentImage = count - 1;
								}
								temp.movingOn = Math.abs( temp.movingOn );
							}
							else
							{
								temp.currentImage++;
								if( temp.currentImage == count )
								{
									temp.currentImage = 0;
								}
								
								temp.movingOn = - Math.abs( temp.movingOn );
							}
							temp.proc.calculateMovingImage( temp.els );
							if( temp.topImage == true )
							{
								temp.output.outputOnBlock( temp.currentImage, temp.typeOfShow, temp.proc.getImg( 'large', temp.currentImage ), temp.proc.getSpecialBlock( temp.currentImage ) , temp.topWidth, temp.topHeight );
								temp.proc.showNewImages( temp.els );
							}
						}
					}
				}
			};
			if ( this.autoScrolling.status )
			{
				setInterval( scrolling, this.autoScrolling.time );
			}
			this.galleryIsReady = true;
		},
		
		rotateImage: function(index, direction)
		{
			if( this.fadeNonActiveImages.status )
			{
				this.proc.hideLastImages( this.els );
				this.currentImage = index;
				this.proc.showNewImages( this.els );
			}
			if( this.secondThumbnail )
			{
				
			}
			if ( this.scrolling )
			{
				this.movingOn =  - direction * Math.abs( this.movingOn );
				this.proc.calculateMovingImage( this.els );
			}
			this.output.outputOnBlock( this.currentImage, this.typeOfShow, this.proc.getImg( 'large', this.currentImage ), this.proc.getSpecialBlock( this.currentImage ) , this.topWidth, this.topHeight );
		},
		
		loadingImages: function()
		{
			this.galleryImageCount = this.images.length;
			var c = 0;
			if( this.secondThumbnail )
			{
				var allImages = new Array( this.images.length * 3);
			}
			else
			{
				var allImages = new Array( this.images.length * 2);
			}
			var temp = this;
			$.each( this.images, function( index, value )
			{
				allImages[ c++ ] = value[0];
				allImages[ c++ ] = value[1];
				if( temp.secondThumbnail )
				{
					allImages[ c++ ] = value[2];
				}
			} );
			var myImages = new Array( c + 1 );
			var loadedCount = 0;
			
			for ( i = 0; i <= c; i++ )
			{
				myImages[i] = new Image();
				$( myImages[i] ).load( 
					function()
					{
						loadedCount++;
						if ( loadedCount == c )
						{
							if( $( '#nxc-gallery-loader' ).lenght > 0 )
							{
								$( '#nxc-gallery-loader' ).remove();
							}
							temp.loadedImages = myImages;
							temp.run();
						}
					}
				).attr( 'src', allImages[i] );
			}
		},
		
		loadingSpecialBlock: function()
		{
			if ( this.specialBlock )
			{
				this.specialBlockCount = $( this.specialBlock ).length;
				this.imageCount = this.specialBlockCount;
				var allSpecialBlock = new Array();
				$.each( $( this.specialBlock ), function( index, el ) 
				{
					allSpecialBlock[index] = $( el ).clone();
					$( el ).remove();
				} );
			}
			else
			{
				this.loadedSpecialBlock = false;
			}
			this.loadedSpecialBlock = allSpecialBlock;
		},
		
		setEvents: function()
		{
			var temp = this;
			$( this.galleryBottom ).mouseenter( 
				function(e)
				{
					temp.autoScroll = 0;
					temp.mouseEnter = true;
					temp.autoScrollOn = false;
				}
			);
			$( this.galleryBottom ).mouseleave( 
				function(e)
				{
					temp.autoScrollOn = false;
					temp.mouseEnter = false;
					temp.galleryClicked = false;
				}
			);
			
			if( this.possibleClicking )
			{
				$( this.galleryBottom ).mousedown(
					function(e)
					{
						e.preventDefault();
						temp.innertion = 0;
						temp.galleryClicked = true;
						temp.lastMousePosition = { 'x': e.pageX, 'y': e.pageY };
					}
				);
				
				$( this.galleryBottom ).mouseup(
					function(e)
					{
						e.preventDefault();
						temp.galleryClicked = false;
					}
				);
			}
			
			if ( this.possibleScrollWheel )
			{
				$( this.galleryBottom ).mousewheel(
					function( e, d )
					{
						e.preventDefault();
						temp.autoScrollOn = false;
						temp.autoScroll += d;
					}
				);
			}
			$.each( this.els, function( index, item )
			{
				var subTemp = temp;
				$( item.getObj() ).mousedown(
					function(e)
					{
						subTemp.startTime = new Date().getTime();
					}
				);
				
				$( item.getObj() ).mouseup(
					function(e)
					{
						subTemp.currentTime = new Date().getTime();
						if ( ( subTemp.currentTime - subTemp.startTime ) < subTemp.mouseClickTime )
						{
							if( subTemp.topImage == true )
							{
								if( subTemp.currentImage != $( this ).attr( 'position' ) )
								{
									//$( this ).fadeTo( subTemp.fadeNonActiveImages.fadeTime, subTemp.fadeNonActiveImages.fadeOpacity, function(){} );
									subTemp.proc.hideLastImages( subTemp.els );
									subTemp.currentImage = $( this ).attr( 'position' );
									
									subTemp.output.outputOnBlock( subTemp.currentImage, subTemp.typeOfShow, subTemp.proc.getImg( 'large', $( this ).attr( 'position' ) ), subTemp.proc.getSpecialBlock( $( this ).attr( 'position' ) ) , subTemp.topWidth, subTemp.topHeight );
								}
							}
						}
					}
				);
				
				if( subTemp.fadeNonActiveImages.status || subTemp.secondThumbnail )
				{
					$( item.getObj() ).mouseenter(
						function()
						{
							if( subTemp.fadeNonActiveImages.status )
							{
								$( this ).fadeTo( subTemp.fadeNonActiveImages.fadeTime, 1, function(){} );
							}
							if( subTemp.secondThumbnail )
							{
								$( this ).html( subTemp.proc.getImg( 'small', $( this ).attr( 'position' ) ).clone() );
							}
						}
					);
					
					$( item.getObj() ).mouseleave(
						function()
						{
							if ( $( this ).attr( 'position' ) != subTemp.currentImage  )
							{
								if( subTemp.fadeNonActiveImages.status )
								{
									$( this ).fadeTo( subTemp.fadeNonActiveImages.fadeTime, subTemp.fadeNonActiveImages.fadeOpacity, function(){} );
								}
								if( subTemp.secondThumbnail )
								{
									$( this ).html( subTemp.proc.getImg( 'secondSmall', $( this ).attr( 'position' ) ).clone() );
								}
							}
						}
					);
				}

			} );
			
			if( this.possibleScrollButton )
			{
				$( this.controlLeft ).mouseenter(
					function(e)
					{
						temp.autoScrollOn = true;
						temp.autoScroll = 2;
					}
				);
				
				$( this.controlRight ).mouseenter(
					function(e)
					{
						temp.autoScrollOn = true;
						temp.autoScroll = -2;
					}
				);
				
				$( this.controlLeft ).mouseleave(
					function(e)
					{
						temp.autoScrollOn = false;
					}
				);
				
				$( this.controlRight ).mouseleave(
					function(e)
					{
						temp.autoScrollOn = false;
					}
				);
				
				$( this.controlLeft ).click(
					function(e)
					{
						e.preventDefault();
					}
				);
				
				$( this.controlRight ).click(
					function(e)
					{
						e.preventDefault();
					}
				);
				
			}
			else
			{
				if( this.specialScrollButton )
				{
					$( this.controlLeft ).click(
						function(e)
						{
							e.preventDefault();
							temp.movingOn = Math.abs( temp.movingOn );
							temp.proc.calculateMovingImage( temp.els );
						}
					);
					$( this.controlRight ).click(
						function(e)
						{
							e.preventDefault();
							temp.movingOn = - Math.abs( temp.movingOn );
							temp.proc.calculateMovingImage( temp.els );
						}
					);
				}
			}
			
			$( this.galleryBottom ).mousemove(
				function(e)
				{
					e.preventDefault();
					var currentMousePosition = { 'x': e.pageX, 'y': e.pageY };
					if ( temp.position == "vertical" )
					{
						var difference = ( currentMousePosition.y - temp.lastMousePosition.y ) / 20;
					}
					else
					{
						var difference = ( currentMousePosition.x - temp.lastMousePosition.x ) / 20;
					}
					if ( temp.galleryClicked )
					{
						temp.innertion += difference;
						temp.autoScroll = temp.innertion;
						temp.autoScrollOn = false;
					}
					temp.lastMousePosition = currentMousePosition;
				}
			);
		}
	}
}

