var zoom_box	= null;
function ZoomBox() {
	var t	= function ZoomBox() {
		this.onImageLoaded	= function( jImg ) {
			jImg.css( { display:	'none' } );

			this.area.append( jImg );
			var to_w	= jImg.width();
			var to_h	= jImg.height();

			if ( 0 == to_w || 0 == to_h ) {
				this.area.empty();
				return false;
			}

			var jClose	= jQuery( '<img src="/images/door_out.png" alt="" />' ).css( {
				display:	'none',
				position:	'absolute',
				cursor:		'pointer',
				backgroundColor:	'#fff',
				padding:	2,
				top:		to_h - 20,
				left:		to_w - 20
			} );
			jClose.bind( 'click', { ref: this }, function( e ) {
				e.preventDefault();
				e.data.ref.close();
			} );
			this.area.append( jClose );

			var win_h	= jQuery(window).height();
			var win_w	= jQuery(window).width();

			var	redim	= false;
			if ( to_h > win_h ) {
				var p		= this.margin * 2;
				var new_h	= win_h - p;

				var ratio	= to_h * 100 / new_h;
				to_h		= new_h;
				to_w		= parseInt( ( to_w / ratio ) * 100, 10 );

				redim		= true;
			} else if ( to_w > win_w ) {
				var p		= this.margin * 2;
				var new_w	= win_w - p;

				var ratio	= to_w * 100 / new_w;
				to_w		= new_w;
				to_h		= parseInt( ( to_h / ratio ) * 100, 10 );

				redim		= true;
			}
			if ( redim ) {
				jImg.css( {
					width:		to_w,
					height:		to_h
				} );
				jClose.css( {
					top:		to_h - 20,
					left:		to_w - 20
				} );
			}

			if ( this.area.width() == to_w && this.area.height() == to_h ) {
				jImg.fadeIn();
				jClose.fadeIn();
			} else {
				var ref	= this;
				this.area.animate( {
					width:	to_w,
					height:	to_h,
					top:	( ( win_h / 2 ) - ( to_h / 2 ) + jQuery(document).scrollTop() ),
					left:	( win_w / 2 ) - ( to_w / 2 )
				}, 1000, function() {
					jImg.fadeIn();
					jClose.fadeIn();
				} );
			}
		}
		this.onImageError	= function( jImg ) {
			alert( 'Cannot load ' + jImg.attr( 'src' ) );
		}
		this.setMargin		= function( px ) {
			this.margin	= px;
			return this;
		}
		this.setSource		= function( src ) {
			this.src	= src;
			return this;
		}

		this.center			= function() {
			var animate		= arguments.length > 0 ? arguments[ 0 ] : false;
			var win			= jQuery(window);
			var	opts		= {
				left:		( win.width() / 2 ) - ( this.area.width() / 2 ),
				top:		( ( win.height() / 2 ) - ( this.area.height() / 2 ) + jQuery(document).scrollTop() )
			};
			if ( animate ) {
				this.area.animate( opts, animate );
			} else {
				this.area.css( opts );
			}
			return this;
		}
		
		this.close			= function() {
			//	@todo	les anims scrolls et center continue après avoir cliquez ici
			this.container.fadeOut().next().fadeOut();
		}

		this.bind			= function() {
			if ( null == this.src ) {
				alert( 'Source not defined' );
				return false;
			}
			var d			= jQuery(document);
			if ( null != jQuery( 'div.zoom_box' ).html() ) {
				this.container	= jQuery( 'div.zoom_box' );
				this.area.empty();
			} else {
				var container	= jQuery( '<div class="zoom_box_container"></div>' ).css( {
					textAlign:			'center',
					width: '100%'
				} );
				jQuery( 'body' ).append( container );

				this.container	= jQuery( '<div class="zoom_box"></div>' ).css( {
					display:			'none',
					cursor:				'pointer',
					backgroundColor:	'#383839',
					opacity:			0.8,
					width:				'100%',
					height:				d.height(),
					position:			'absolute',
					top:				0,
					left:				0,
					zIndex:				100
				} );
				this.container.bind( 'click', { ref: this }, function( e ) {
					e.preventDefault();
					e.data.ref.close();
				} );
				container.append( this.container );

				this.area		= jQuery( '<div class="zoom_box_area"></div>' ).css( {
					backgroundColor:	'#ffffff',
					width:				100,
					height:				100,
					position:			'absolute',
					zIndex:				101
				} );
				container.append( this.area );

				jQuery(window).bind( 'resize', { ref: this }, function( e ) {
					e.data.ref.center( 200 );
				} );
				jQuery(document).bind( 'scroll', { ref: this }, function( e ) {
					e.data.ref.center( 200 );
					e.data.ref.container.css( { width: '+=' + jQuery(document).scrollLeft() + 'px' } );
				} );

				this.center();
			}
			new ImagePreloader( this.src, this ).start();
			this.container.fadeIn().next().fadeIn();
		}

		this.container	= null;
		this.area		= null;
		this.src		= null;
		this.margin	= 0;
	}

	//	Singleton ^^
	if ( null == zoom_box ) {
		zoom_box = new t();
	}

	return zoom_box;
}