/* Author: 

*/

var imageInfo;

$(window).resize(positionImage);
$(window).load(positionImage);

function positionImage() {
	var margin = 40;
	
	var img = $('#right-column img');
	
	if (!imageInfo) {
		imageInfo = {
			height: img.height(),
			width: img.width()
		};
	}
	
	var rightColumn = $('#right-column');
	
	var rightColumnInfo = {
		height: rightColumn.height(),
		width: rightColumn.width() - 300,
		top: rightColumn.position().top,
		left: rightColumn.position().left + 300
	};
	
	var maxHeight = rightColumnInfo.height - (margin * 2);
	var maxWidth = rightColumnInfo.width - (margin * 2);
	
	var imageRatio = imageInfo.width / imageInfo.height;
	var spaceRatio = maxWidth / maxHeight;
	
	var newWidth, newHeight;
	
	if (imageRatio > spaceRatio) {
		// The image is wider than the space, so limit by width.
		newWidth = maxWidth;
		newHeight = newWidth / imageRatio;
	} else {
		// The image is taller than the space, so limit by height.
		newHeight = maxHeight;
		newWidth = newHeight * imageRatio;
	}
	
	$('#right-column img').css({
		position: 'absolute',
		left: rightColumnInfo.left + (rightColumnInfo.width / 2) - (newWidth / 2),
		top: (rightColumnInfo.height / 2) - (newHeight / 2),
		width: newWidth,
		height: newHeight
	});
}
