/* jquery.imagefit 
 *
 * Version 0.3 Scoramba 
 	based on Version 0.2 by Oliver Boermans <http://www.ollicle.com/eg/jquery/imagefit/>
 *
 * Extends jQuery <http://jquery.com>
 *
 */
 

(function($) {
	$.fn.imagefit = function(options) {
		var fit = {
			all : function(imgs){
				imgs.each(function(){
					fit.one(this);
					})
				},
			one : function(img){
				$(img)
					.width('100%').each(function()
					{
						$(this).height(Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);
					})
				}
		};
		
		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).resize(function(){
					fit.all(imgs);
				});
			});
		return this;
	};
	
		$.fn.thumbfit = function(options) {
		var fit = {
			all : function(imgs){
				imgs.each(function(){
					fit.one(this);
					})
				},
			one : function(img){
				$(img)
					.width('100%').each(function()
					{
						$(this).height(Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);
					})
				}
		};
		
		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")
							//.css('left', "200px")		//.css('max-height', "100px")
						
					
						//.css('top', ($(this).children().attr('startheight') - $(this).height())/2);
						$(this).parent().parent().css('max-height', "143px")
					//	.css('margin-top', $(this).width() +"px")
						//$(this).parent().parent().css('margin-top', "100px")
						
					//$(this).parent().parent().css('margin-left', (200 - $(this).parent().parent().height())/2 +"px")
					//$(this).parent().parent().css('margin-top', (200 - $(this).parent().parent().width())/2 +"px")
					fit.one(this);
					
					//$(this).children().css('left', (100 - $(this).width())/2);
					//$(this).children().css('top', (100 - $(this).height())/2);
				});
				// Re-adjust when window width is changed
				$(window).resize(function(){
					fit.all(imgs);
				});
			});
		return this;
	};
	
})(jQuery);