function scaleAllImagesDown($images) {
	$images.each(function () {
		var $img = $(this);
		var $div = $img.parents("div:first");
		scaleImgDownToDiv($img, $div);
		$img.css("visibility","visible");
		$img.load(function () {
			var $image = $(this);
			var $div = $image.parents("div:first");
			scaleImgDownToDiv($image, $div);
			$image.css("visibility","visible");
		});
	});
}


function scaletoHeight($div, height) {
	var w = $div.width();
	var h = $div.height();
	$div.css({height : height+"px", width : (w * height / h) + "px"});
}

function scaletoWidth($div, width) {
	var w = $div.width();
	var h = $div.height();
	$div.css({height : (h * width / w) + "px", width : width + "px"});
}

function scaleImgDownToDiv($img, $div) {
	var width = $div.width();
	var height = $div.height();
	var imgWidth = $img.innerWidth();
	var imgHeight = $img.innerHeight();

	//only scale if image is too large
	if (imgWidth>width || imgHeight>height) {
		if (imgWidth/imgHeight<width/height) {
			scaletoHeight($img, height);
		} else {
			scaletoWidth($img, width);
		}
	}
	moveImage($img, width, height);
}


function moveImage($img, width, height) {
	var imgHeight = $img.height();
	var imgWidth = $img.width();
	var imgTop = (height - imgHeight) / 2;
	var imgLeft = (width - imgWidth) / 2;
	$img.css({"position": "absolute", top: imgTop + "px", left: imgLeft + "px", "visibility": "visible"});

}


