//     SCALE IMAGES TO FIT IN A FIXED CONTAINER SIZE
//     ---------------------------------------------

//     By Chris Hill-Scott 2009, except where noted.

//     Code is public domain, reuse as desired.

//     Requires jQuery

//     * Kept specific for speed

//     ---------------------------------------------

jQuery.fn.imageScale = function() {

	var scale = .75, // controls the ammount of matte around the image
		maxX = parseInt($(window).width() * scale), // parseInt() makes things look better in FF
		maxY = parseInt($(window).height() * scale);

	return this.each(
		function() {

			var $img = $(this),
				nativeX = $img.attr("data-width"),
				nativeY = $img.attr("data-height"),
				newHeight = maxX/(nativeX/nativeY);

			if (nativeX > maxX) { // image needs resizing one way or the other
					$img.css(
						{
							//"width": "auto",
							//"height": maxY
							"width": maxX,
							"height": "auto"
						}
					);
				
			} else { // image is natively smaller than viewport
				$img.css(
					{
						"width": "auto",
						"height": "auto"
					}
				);
			}

		}
	);

}
