jQuery(document).ready(function(){
	var img = jQuery('.index-image');
	
	jQuery(window).resize(function(){
		var maxImageWidth = 1615;
		var maxImageHeight = 1122;
		var minImageWidth = 970;
		var minImageHeight = 674;
		var imageRatio = maxImageWidth/maxImageHeight;
		
		var viewportWidth = jQuery(window).width();
		var viewportHeight = jQuery(window).height();
		
		if (viewportWidth / viewportHeight >= imageRatio) {
			var resizeRatio = maxImageWidth / viewportWidth;
			var left = 0;
			var top = 1;
		} else {
			var resizeRatio = maxImageHeight / viewportHeight;
			var left = 1;
			var top = 0;
		}
		
		newImageWidth = parseInt(maxImageWidth / resizeRatio);
		newImageHeight = parseInt(maxImageHeight / resizeRatio);
		
		if ((newImageWidth < minImageWidth) || (newImageHeight < minImageHeight)) {
			newImageWidth = minImageWidth;
			newImageHeight = minImageHeight;
			top = 1;
			left = 1;
		}
		
		left = 0;
		
		if (left) {
			left = parseInt((newImageWidth - viewportWidth)/(-2));
		}

		if (top) {
			top = parseInt((newImageHeight - viewportHeight)/(-4));
		}
		
		jQuery(img).css({
			'width': newImageWidth+'px',
			'height': newImageHeight+'px',
			'top': top+'px',
			'left': left+'px'
		});
	});
	
	jQuery(window).resize();
});
