$(document).ready(tuneLayout);
$(window).load(tuneLayout);

function tuneLayout() {
	var bottom = getBottomEdge($("#mainHull, #sidebar"));
	var navi = $("#naviHull");
	bottom = Math.max(bottom, navi.position().top + navi.outerHeight() + 160) - 40;

	$("#logo2").css("top", bottom);
	$("#LineBottom").css("top", bottom + 75);
	$("#LineBottom, #logo2").show();
}

function getBottomEdge(elements) {
	var bottom = 0;
	$(elements).each(function() {
		bottom = Math.max(bottom, $(this).position().top + $(this).outerHeight());
	});
	return bottom;
}

// fit size of target into maxW x maxH rectangle, keeping the aspect ratio constant 
(function($){
	$.fn.fitRatio = function(maxW, maxH) {
  		var tW = $(this).width();
  		var tH = $(this).height();

		var w = maxW, h = maxH;

		if(tW * maxH >= tH * maxW) {
			h = (maxW * tH + (tW/2 - 1)) / tW; // "+ (tW/2 - 1)" implements rounding
		} else {
			w = (maxH * tW + (tH/2 - 1)) / tH; // "+ (tH/2 - 1)" implements rounding
		}

		return {width: w, height: h};
	};
})(jQuery);

