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()
		{

		}
	}
}

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()
		{
			var coordTopHeight = this.galleryTop.height();
			var coordTopWidth = this.galleryTop.width();
			var coordHeight = this.galleryBottom.height();
			if( this.options.bottomWidth != 0 )
			{
				var coordWidth = this.options.bottomWidth;
			}
			else
			{
				var coordWidth = this.galleryBottom.width();
			}
			if ( this.options.position == "vertical" )
			{
				var height = coordHeight;
				this.options.height = height;
				if ( ( coordTopHeight == 0 ) && ( this.options.topImage ) )
				{
					this.options.topHeight = height;
				}
				else
				{
					this.options.topHeight = coordTopHeight;
				}
				this.options.topWidth = coordTopWidth;
				if( this.options.bottomHeight == 'auto' )
				{
					this.options.bottomHeight = coordHeight;
				}
				this.options.bottomWidth = this.options.imageSize.width;
			}
			else
			{
				var width = coordWidth;
				this.options.bottomWidth = width;
				this.options.topHeight = width * this.options.aspectRatio;
				this.options.topWidth = coordTopWidth;
				if( this.options.bottomHeight == 'auto' )
				{
					this.options.bottomHeight = this.options.imageSize.height;
				}
				this.options.height = this.options.topHeight + this.options.bottomHeight;
			}

		},
		
		setSize: function()
		{
			if ( this.options.position == "vertical" )
			{
				$( this.galleryBottom).css( 'width', this.options.bottomWidth );
				$( this.galleryTop ).css( 'height', this.options.topHeight );
			}
			else
			{
				//$( this.galleryTop ).css( 'height', this.options.topHeight );
				//$( this.galleryBottom ).css( 'height', this.options.bottomHeight );
				$( this.galleryTop ).animate( { 'height': this.options.topHeight }, 1000 );
				$( this.galleryBottom ).animate( { 'height': this.options.bottomHeight }, 1000 );
			}
		},
		
		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 )
		{
			if( this.options.dontRepeatImages )
			{
				var shift = 0;
			}
			else
			{
				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();
			if( this.options.specialBlockLikeImage )
			{
				var count = this.options.specialBlockCount;
			}
			else
			{
				var count = this.options.imageCount;
			}
			for ( var i = 0; i < ( count ); i++ )
			{
				var img = this.getImg( 'small', 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 ( 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 )
			{
				var img = 0;
				if ( size == "large" )
				{
					img = this.images[ (id % this.options.galleryImageCount) * 2 + 1 ];
					//img = $( img ).removeAttr( 'height' );
					//img = $( img ).attr( 'class', 'image-item' );
					return img;
				}
				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( 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 ); 
					}
				}
				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 ); 
					}
				}
				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 ); 
					}
				}
				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 ); 
					}
				}
				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( 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( 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( 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( currentX + step, temp.a );
					el.setX( Math.round( nextX ) );
				} );
			}
		}
	
	}
}

function imageGalleryOutputSystem()
{
	return {
		
		initialize: function( fadingTopImage, galleryTop )
		{
			this.fadingTopImage = fadingTopImage;
			this.galleryTop = galleryTop;
		},
		
		outputOnBlock: function( typeOfShow, image, specialBlock, width, height )
		{
			this.galleryTop.html('');
			
			this.center = $( '<div></div>' ).attr( 'class', 'nxc-block-center' );
			
			$( 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 );
			$( this.center ).append( image );
			
			if ( specialBlock )
			{
				$( this.center ).append( specialBlock );
				$( specialBlock ).show();
			}
			
			if( this.fadingTopImage != 0 )
			{
				$( this.center ).fadeIn( this.fadingTopImage );
			}
			else
			{
				$( this.center ).show();
			}
		},
				
		outputOnLightBox: function()
		{
			
		}
		
	}
}

function imageGalleryKernel()
{
	return {
		//setting parametrs
		imageSize				: { 'width': 90, 'height': 60 }, //width, height
		imageMargin				: 2,
		aspectRatio				: 0.8,
		mouseClickTime			: 150,
		topImage				: true,
		defaultTopImage			: true,
		fadingTopImage			: 800,
		shadow					: false,
		interval				: 2,
		autoScrollOn			: true,
		autoScroll				: 2,
		staticAutoScroll		: 0,
		existSpecialBlock		: 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 )
		specialBlockLikeImage	: false,
		possibleClicking		: true,
		possibleScrollButton	: true,
		specialScrollButton		: false,
		possibleScrollWheel		: true,
		
		bottomHeight			: 'auto',
		cufonForSpecialBlock	: false,
		cufonFunction			: false,
		movingOn				: 4,
		movingIterationCount	: 20,
		movingIterationTime		: 100,
		dontRepeatImages		: false,
		autoScrolling			: { 'status' : false, 'time' : 10000, 'to' : 'right' },
		fadeNonActiveImages		: { 'status' : false, 'fadeTime' : 300, 'fadeOpacity' : 0.3 },
		
		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,
		
		
		initialize: function( images, specialBlock, top, bottom, controlLeft, controlRight )
		{
			this.images = images;
			this.specialBlock = specialBlock;
			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;
			}
		},
		
		run: function()
		{
			this.output = new imageGalleryOutputSystem();
			this.output.initialize( this.fadingTopImage, this.galleryTop );
			this.proc = new imageGalleryProcessor();
			this.proc.initialize( this, this.loadedImages, this.loadedSpecialBlock, this.galleryTop, this.galleryBottom );
			this.proc.calculateSize();
			this.proc.setSize();
			
			if ( this.dontRepeatImages )
			{
				this.proc.calculateImageCount();
				if ( !this.specialBlockLikeImage )
				{
					if( ( this.imageCount - 2 ) > this.galleryImageCount )
					{
						this.imageCount = this.galleryImageCount;
						this.possibleClicking = false;
						this.possibleScrollButton = false;
						this.possibleScrollWheel = false;
					}
				}
				else
				{
					if( ( this.imageCount - 2 ) > this.specialBlockCount )
					{
						this.imageCount = this.specialBlockCount;
						this.possibleClicking = false;
						this.possibleScrollButton = false;
						this.possibleScrollWheel = false;
					}
				}
			}
			else
			{
				
			}
			
			this.els = this.proc.createElements();
			this.proc.showElements( this.els );
			if( this.topImage )
			{
				$( this.els[ this.currentImage ].getObj() ).fadeTo( this.fadeTime, 1, function(){} );
				this.output.outputOnBlock( 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 );
			};
			setInterval( rotation, 1 );
			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 )
						{
							$( temp.els[ temp.currentImage ].getObj() ).fadeTo( temp.fadeNonActiveImages.fadeTime, temp.fadeNonActiveImages.fadeOpacity, function(){} );
						}
						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.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(){} );
						}
					}
				}
				else
				{
					if( !temp.mouseEnter )
					{
						if( !temp.movingStarted )
						{
							if( temp.topImage == true )
							{
								$( temp.els[ temp.currentImage ].getObj() ).fadeTo( temp.fadeNonActiveImages.fadeTime, temp.fadeNonActiveImages.fadeOpacity, function(){} );
							}
							if ( temp.autoScrolling.to == 'left' )
							{
								temp.currentImage--;
								if( temp.currentImage < 0 )
								{
									temp.currentImage = count - 1;
								}
								temp.movingOn = Math.abs( temp.movingOn );
							}
							else
							{
								//console.log( temp.currentImage );
								temp.currentImage++;
								//console.log( temp.currentImage );
								//console.log( count );
								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.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 ( this.autoScrolling.status )
			{
				setInterval( scrolling, this.autoScrolling.time );
			}
		},
		
		loadingImages: function()
		{
			this.galleryImageCount = this.images.length;
			var c = 0;
			var allImages = new Array( this.images.length * 2 );
			$.each( this.images, function( index, value )
			{
				allImages[ c++ ] = value[0];
				allImages[ c++ ] = value[1];
			} );
			var myImages = new Array( c + 1 );
			var loadedCount = 0;
			var temp = this;
			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.autoScroll = temp.staticAutoScroll;
					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 )
							{
								$( subTemp.els[ subTemp.currentImage ].getObj() ).fadeTo( subTemp.fadeNonActiveImages.fadeTime, subTemp.fadeNonActiveImages.fadeOpacity, function(){} );
								subTemp.currentImage = $( this ).attr( 'position' );
								subTemp.output.outputOnBlock( subTemp.typeOfShow, subTemp.proc.getImg( 'large', $( this ).attr( 'position' ) ), subTemp.proc.getSpecialBlock( $( this ).attr( 'position' ) ) , subTemp.topWidth, subTemp.topHeight );
							}
						}
					}
				);
				
				if( subTemp.fadeNonActiveImages.status )
				{
					$( item.getObj() ).mouseenter(
						function()
						{
							$( this ).fadeTo( subTemp.fadeNonActiveImages.fadeTime, 1, function(){} );
						}
					);
					
					$( item.getObj() ).mouseleave(
						function()
						{
							if ( $( this ).attr( 'position' ) != subTemp.currentImage  )
							{
								$( this ).fadeTo( subTemp.fadeNonActiveImages.fadeTime, subTemp.fadeNonActiveImages.fadeOpacity, function(){} );
							}
						}
					);
				}

			} );
			
			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;
				}
			);
		}
	}
}

