/* jquery.maxSize 
 *
 * Version 0.1 by Stefan Wärting <http://sourceblogg.se/>
 *
 * Extends jQuery <http://jquery.com>
 *
 */
(function($) {
	$.fn.maxSize = function(options) {
		this.each(function() {
			if(options.keepAspectRatio == undefined) {
				options.keepAspectRatio =  true;
			}
			if(options.maxWidth == undefined && options.maxWidth == undefined && options.maxSize != undefined) {
				options.maxWidth = options.maxSize;
				options.maxheight = options.maxSize;
			}
			
	        if (options.maxWidth != undefined && $(this).width() > options.maxWidth)
	        {
	            var newWidth = options.maxWidth;
	            if(options.keepAspectRatio) {
	            	var newHeight = $(this).height() / ( $(this).width() / options.maxWidth );
	            }
	            else {
	            	var newHeight = $(this).height();
	            }
	            
	            $(this).height(newHeight).width(newWidth);
	        }
	        
	        if (options.maxheight != undefined && $(this).height() > options.maxHeight)
	        {
	            var newHeight = options.maxHeight;
	            if(options.keepAspectRatio) {
	            	var newWidth = $(this).width() / ( $(this).height() / options.maxHeight );
	            }
	            else {
	            	var newWidth = $(this).width();
	            }
	    
	            $(this).height(newHeight).width(newWidth);
	        }
			
		});
		return this;
	};
})(jQuery);