
/**
 * Creates a new instance of AutoImage.
 * @classDescription Loads an image and updates an image element.
 * @param {String} imageFile The full url to an image.
 * @param {HTMLElement, String} imageElement The element to update its image.
 * @param {Number} maxWidth The maximal width of the image.
 * @param {Number} maxHeight The maximal height of the image.
 * @constructor
 * @copyright Copyright &copy; 2008, krumedia
 */
function AutoImage(imageFile, imageElement, maxWidth, maxHeight)
{
	imageElement = $(imageElement);
	var image = new Image();
	image.onload = function()
	{
		var size = AutoImage.boxSize(new Size(image.width, image.height), new Size(maxWidth, maxHeight));
		try
		{
			imageElement.width = size.width;
			imageElement.height = size.height;
			imageElement.src = image.src;
		}
		catch(e)
		{
		}
	};
	image.src = imageFile;
	// keep reference to not allow the GC to free the object
	this.image = image;
}

/**
 * Boxes a size within another size.
 * @param {Size} inSize The input size to transform.
 * @param {Size} boxSize The maximum size used to transform the input size.
 * @return {Size} A new instance of the transformed input size.
 * @copyright Copyright &copy; 2008, krumedia
 */
AutoImage.boxSize = function(inSize, boxSize)
{
	if (inSize.width / boxSize.width > inSize.height / boxSize.height)
	{
		return new Size(boxSize.width, Math.round(inSize.height * (boxSize.width / inSize.width)));
	}
	else
	{
		return new Size(Math.round(inSize.width * (boxSize.height / inSize.height)), boxSize.height);
	}
};
