/* jquery.imagefit 
 *
 * Version 0.2 by Oliver Boermans <http://www.ollicle.com/eg/jquery/imagefit/>
 *
 * Extends jQuery <http://jquery.com>
 *
 * Extended by SH <http://sven513.de>
 */
(function($) {
	$.fn.imagefit = function(options) {
	
		// hide unscaled img initially
		//$('#background').hide();
		
		var fit = {
			all : function(imgs){
				imgs.each(function(){
					fit.one(this);
					})
				},
			one : function(img){
				
				var frame = $('#full_content');
				var calc_height = $(img).attr('startheight')*(frame.width()/$(img).attr('startwidth'));
								
				if ( frame.height() < frame.width()  && frame.height() < calc_height ){
					$(img).width('100%').each(function(){
						$(this).height(Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);			
					})
				} else {
					$(img).height('100%').each(function(){
						$(this).width(Math.round(
							$(this).attr('startwidth')*($(this).height()/$(this).attr('startheight')))
						);
					})
				}
				//$('#full_content').removeClass('loading');
				$('#background').show();
			}
		};
		
		this.each(function(){
				var container = this;
				
				// store list of contained images (excluding those in tables)
				var imgs = $('img', container).not($("table img"));
				
				// store initial dimensions on each image 
				imgs.each(function(){
					$(this).attr('startwidth', $(this).width())
						.attr('startheight', $(this).height())
						.css('max-width', $(this).attr('startwidth')+"px");
				
					fit.one(this);
				});
				// Re-adjust when window width is changed
				$(window).bind('resize', function(){
					fit.all(imgs);
				});
			});
		return this;
	};
})(jQuery);

