$( function()
{
	$('.postcontent IMG').load( function()
	{
		if ( this.naturalWidth === undefined ) return;
		if ( !this.naturalWidth ) return;
		if ( this.naturalWidth == $(this).width() ) return;
		
		var Link = $( "<A></A>" );
		Link.attr( "href", $(this).attr( "src" ) );
		
		$(this).replaceWith( Link );
		Link.html( $(this) );
		
		$(this).css( "border", "1px dotted rgba( 0, 0, 0, 0.5 )" );
		$(this).css( "padding", "0px" );
		
		var SmallW = $(this).width();
		var SmallH = $(this).height();
		
		$(this).click( function( e )
		{
			// Middle click, let them open in a new window
			if ( e.which == 2 )
			{
				return true;
			}
			
			$( ".PopupImage" ).remove();
			
			var scrollTop = $(window).scrollTop();
			
			var background = $( "<div></div>" );
			background.css( "background-color", "rgba( 0, 0, 0, 0.5 )" );
			background.css( "position", "fixed" );
			background.css( "width", "100%" );
			background.css( "height", "100%" );
			background.css( "top", "0" );
			background.css( "left", "0" );
			background.hide().fadeIn( 200 );
			
			var image = new Image();
			image.src = $(this).attr( "src" );
			$(image).addClass( "PopupImage" );
			$(image).css( "position", "absolute" );
			$(image).css( "max-width", "100000px" );
			$(image).css( "max-height", "100000px" );
			$(image).css( "top", $(this).offset().top - scrollTop );
			$(image).css( "left", $(this).offset().left );
			$(image).css( "width", SmallW );
			$(image).css( "height", SmallH );
			
			$(image).animate( { width: this.naturalWidth,
								height: this.naturalHeight,
								left: ($(window).width()/2 - this.naturalWidth/2),
								top: ($(window).height()/2 - this.naturalHeight/2) }, 400 );
				
			background.append( image );
			$('BODY').append( background );
			
			$(background).click( function()
			{
				$(this).fadeOut( 200 );
			});
			
			return false;
			
		});

	});
});