

// Function to resize images based on user- and default-settings.
(function($) {
	$.fn.extend({
		imageResize: function(options) {
			// Initialize default-variables:
			var defaults = {
				centerImage: 0,				// Defines if a image should be centered inside a fixed image-container. (Possible values: 0 => no (default), 1 => yes).
				maxHeight: 100,				// Defines the maximum-height for the image-resizing-process. (Possible values: n (default => 100).
				maxWidth: 100,				// Defines the maximum-width for the image-resizing-process. (Possible values: n (default => 100).
				upscaleImage: 0				// Defines if a image should be upscaled to fit a fixed image-container. (Possible values: 0 => no (default), 1 => yes).
			}

			// Initialize option-variables so the user can modify the default-settings:
			var options = $.extend(defaults, options);

			// Shorten option-variables-call for further code.
			var o = options;

			// Initialize flexible-variables:
			var flexibles = {
				newHeight: 0,			// Will contain the new height of the current image.
				newWidth: 0,			// Will contain the new width of the current image.
				originalHeight: 0,		// Will contain the original height of the current image.
				originalWidth: 0,		// Will contain the original width of the current image.
				resizeFactor: 0,		// Will contain the resize-factor of the current image.
				topMargin: 0			// Will contain the top-margin of the current image.
			}

			// Shorten flexible-variables-call for further code.
			var f = flexibles;

			// Loop over each matched element.
		 	this.each(function() {
		 		// Run master-function.
				imageResize(this);
			});

			// Master-Function to check which sub-function(s) should be used to resize the image accordingly.
			function imageResize(e) {
				// Reset container-variable.
				f.topMargin = 0;
				// Modify container-variables.
				f.originalHeight = $(e).find("img").height();		// Get original height of the current image.
				f.originalWidth = $(e).find("img").width();		// Get original width of the current image.
				// Check if the image should be upscaled.
				// Check if the original height/width of the image is smaller than the maximum available height/width for the image.
				if ((o.upscaleImage == 0) && (f.originalHeight < o.maxHeight) && (f.originalWidth < o.maxWidth)) {
					// Do not resize the image.
					noResize(e);
				} else {
					// Check if the original height of the image is greater than the original width of the image.
					 if (f.originalHeight > f.originalWidth) {
						 // Resize according to the height of the image.
						resizeViaHeight(e);
					} else {
						// Resize according to the width of the image.
						resizeViaWidth(e);
					}
				}
				// Adjust container-styles and image-styles.
				$(e).css("background-image", "none")
					.find("img").css("display", "block")
			}

			// Sub-Function to adjust image-styles using the calculated values from other sub-functions.
			function adjustImageStyles(e) {
				// Adjust image-styles.
				$(e).find("img")
					.css("margin-top", f.topMargin)
					.height(f.newHeight)
					.width(f.newWidth);
			}

			// Sub-Function to not resize the image.
			function noResize(e) {
				f.newHeight = f.originalHeight;		// Height for the image-resizing-process according to the orignal-height of the image.
				f.newWidth = f.originalWidth;		// Width for the image-resizing-process according to the orignal-width of the image.
				// Position the image properly.
				calculateMargin(e);
			}

			// Sub-Function to properly position the image in fixed image-containers using the calculated values from other sub-functions.
			function calculateMargin(e) {
				if (o.centerImage == 1) {
					// Fill variable.
					f.topMargin = Math.round((o.maxHeight-f.newHeight)/2);		// Calculated and rounded margin to top for the image.
				}
				// Adjust image-styles.
				adjustImageStyles(e);
			}

			// Sub-Function to resize the image according to the height of the image.
			function resizeViaHeight(e) {
				// Fill variables.
				f.newHeight = o.maxHeight;						// Height for the image-resizing-process according to the maximum-height for the image-resizing-process.
				f.resizeFactor = f.originalHeight/f.newHeight;				// Calculated resize-factor for the further image-resizing-process.
				f.newWidth = Math.round(f.originalWidth/f.resizeFactor);		// Calculated and rounded width for the image-resizing-process.
				// Countercheck if the calculated width is greater than maximum-width for the image-resizing-process.
				if (f.newWidth > o.maxWidth) {
					// Resize according to the width of the image.
					resizeViaWidth(e);
				} else {
					// Position the image properly.
					calculateMargin(e);
				}
			}

			// Sub-Function to resize the image according to the width of the image.
			function resizeViaWidth(e) {
				// Fill variables.
				f.newWidth = o.maxWidth;						// Width for the image-resizing-process according to the maximum-width for the image-resizing-process.
				f.resizeFactor = f.originalWidth/f.newWidth;				// Calculated resize-factor for the further image-resizing-process.
				f.newHeight = Math.round(f.originalHeight/f.resizeFactor);		// Calculated and rounded height for the image-resizing-process.
				// Countercheck if the calculated height is greater than maximum-height for the image-resizing-process.
				if (f.newHeight > o.maxHeight) {
					// Resize according to the height of the image.
					resizeViaHeight(e);
				} else {
					// Position the image properly.
					calculateMargin(e);
				}
			}
		}
	});
})(jQuery);

