function newsGallery()
{
	return {
		
		currentObject: 0,
		previousObject: 0,
		scale: 15, //in percentage
		fadeTime: 200,
		
		bouncing: false,
		
		objectsCount: 0,
		objectsBlockWidth: 0,
		centerOfObjectsBlock: 0,
		
		imageChanging: false,
		lastImage: -1,
		
		existResolution: false,
		existDownload: false,
		existInfos: false,
		
		initialize: function( objectsBlock, objects, generalObjects, left, right )
		{
			this.objectsBlock = $( objectsBlock );
			this.objects = $( objects );
			this.generalObjects = $( generalObjects );
			this.left = $( left );
			this.right = $( right );
			
			this.getCount();
			if( this.objectsCount > 0 )
			{
				this.getWidth();
				this.getCenter();
				this.checkResolutions();
				this.checkDownload();
				this.checkInfos();
				//this.setImageWigthToBlock();
				this.addEvents();

				this.update();
			}
		},
		
		checkResolutions: function()
		{
			var res = $( 'div.resolutions div.resolution_types div.type' );
			if( res.length > 0 )
			{
				this.existResolution = true;
				this.resolutionCount = res.length;
				this.resolutions = res;
			}
		},
		
		checkDownload: function()
		{
			var res = $( 'div.download div.item' );
			if( res.length > 0 )
			{
				this.existDownload = true;
				this.downloadCount = res.length;
				this.downloads = res;
			}
		},
		
		checkInfos: function()
		{
			var infos = $( 'div.images_info div.images_infos span.images_info' );
			if( infos.length > 0 )
			{
				this.existInfos = true;
				this.infosCount = infos.length;
				this.infos = infos;
			}
		},
		
		getCount: function()
		{
			this.objectsCount = this.objects.length;
			$( 'span.pages span.total_pages' ).text( this.objectsCount );
		},
		
		getWidth: function()
		{
			this.objectsBlockWidth = $( this.objects[ this.objectsCount - 1 ] ).position().left + $( this.objects[ this.objectsCount - 1 ] ).width();
		},
		
		getCenter: function()
		{
			this.centerOfObjectsBlock = (parseInt( $( this.objectsBlock ).parent().width() ) / 7) + 7;
		},
		
		update: function()
		{
			if( this.currentObject == 0 )
			{
				$( this.left ).hide();
			}
			else
			{
				$( this.left ).show();
			}
			if( this.currentObject == ( this.objectsCount -1 ) )
			{
				$( this.right ).hide();
			}
			else
			{
				$( this.right ).show();
			}
			this.currentObj = $( this.objects[ this.currentObject ] ).position().left + ( $( this.objects[ this.currentObject ] ).width() / 2 ) - this.centerOfObjectsBlock;
			$( this.objects[ this.previousObject ] ).removeClass( 'current' );
			$( this.objects[ this.currentObject ] ).addClass( 'current' );
      $( this.objectsBlock ).animate( { 'left': -this.currentObj }, 200 );
			this.updateGeneralObject();
			$( 'span.pages span.page_number' ).text( this.currentObject + 1 );
			this.previousObject = this.currentObject;
		},
		
		updateGeneralObject: function()
		{
			ths = this;
			var top = $( ths.generalObjects[ths.currentObject] ).height();
			if( parseInt( top ) == 0 )
			{
				top = 315;
			}
			if( this.existResolution )
			{
				$( this.resolutions[this.previousObject] ).fadeOut( 200 );
			}
			if( this.existDownload )
			{
				$( this.downloads[this.previousObject] ).fadeOut( 200 );
			}
			if( this.existInfos )
			{
				$( this.infos[this.previousObject] ).fadeOut( 200 );
			}
			$( this.generalObjects[this.previousObject] ).fadeOut( 200 );
			$( this.generalObjects[this.currentObject] ).parent().children( 'a.previous' ).animate( { 'top': ( top / 2 ) - 19 }, 200 );
			$( this.generalObjects[this.currentObject] ).parent().children( 'a.next' ).animate( { 'top': ( top / 2 ) - 19 }, 200 );
			$( this.generalObjects[this.previousObject] ).parent().animate( { 'height': top }, 200 );
			$( this.generalObjects[this.currentObject] ).fadeIn( 200 );
			if( this.existResolution )
			{
				$( this.resolutions[this.currentObject] ).fadeIn( 200 );
			}
			if( this.existDownload )
			{
				$( this.downloads[this.currentObject] ).fadeIn( 200 );
			}
			if( this.existInfos )
			{
				$( this.infos[this.currentObject] ).fadeIn( 200 );
			}
			/*$( 'div.images_info span.images_info' ).fadeOut( 50,
				function()
				{
					//$( this ).text( $( ths.generalObjects[ths.currentObject] ).children( 'span.caption' ).text() );
					//$( this ).fadeIn( 150 );
				}
			);*/
		},
		
		setImageWigthToBlock: function()
		{
			$( this.objects ).each(
				function( index, item )
				{
					$( item ).css( 'width', $( item ).find( 'img' ).width() );
					$( item ).css( 'height', $( item ).find( 'img' ).height() );
				}
			);
		},
		
		addEvents: function()
		{
			var ths = this;
			$( this.left ).click(
				function( event )
				{
					ths.previousObject = ths.currentObject;
					ths.currentObject = ths.currentObject - 1;
					if( ths.currentObject < 0 )
					{
						ths.currentObject = 0;
					}
					else
					{
						ths.update();
					}
					return false;
				}
			);
			
			$( this.right ).click(
				function( event )
				{
					ths.previousObject = ths.currentObject;
					ths.currentObject = ths.currentObject + 1;
					if( ths.currentObject == ths.objectsCount )
					{
						ths.currentObject = ths.objectsCount - 1;
					}
					else
					{
						ths.update();
					}
					return false;
				}
			);
			
			$( this.objects ).each(
				function( index, item )
				{
					$( item ).click(
						function( event )
						{
							if( index != ths.currentObject )
							{
								ths.previousObject = ths.currentObject;
								ths.currentObject = index;
								ths.update();
							}
							return false;
						}
					);
					if( this.bouncing )
					{
						$( item ).mouseenter(
							function( event )
							{
								if( !ths.imageChanging || ( ths.lastImage != index ) )
								{
									var width = $( this ).find( 'img' ).width();
									var height = $( this ).find( 'img' ).height();
									$( this ).find( 'img' ).removeAttr( 'height' );
									ths.imageChanging = true;
									ths.lastImage = index;
									next_width = width + ( width * ths.scale / 100 );
									next_height = height + ( height * ths.scale / 100 );
									next_margin_left = ( next_width - width ) / 2;
									next_margin_top = ( next_height - height ) / 2;
									$( this ).find( 'img' ).css( 'width', width );
									//$( this ).find( 'img' ).css( 'height', height );
									$( this ).find( 'img' ).css( 'z-index', 100 );
									$( this ).find( 'img' ).animate(
										{
											'width': next_width,
											//'height': next_height,
											'margin-left': - next_margin_left,
											'margin-top': - next_margin_top
										},
										ths.fadeTime
									);
								}
								return false;
							}
						);
					
						$( item ).mouseleave(
							function( event )
							{
								if( ths.lastImage == index )
								{
									$( this ).find( 'img' ).animate(
										{
											'width': $( this ).css( 'width' ),
											//'height': $( this ).css( 'height' ),
											'margin-left': 0,
											'margin-top': 0
										},
										ths.fadeTime,
										function()
										{
											$( this ).css( 'z-index', 1 );
											ths.imageChanging = false;
										}
									);
								}
								return false;
							}
						);
					}
				}
			);
		}
		
	}
}

