// Make sure the first element in the body will have a top margin of 0 pixels.
(function()
{
	for(var i = 0, elems = $("#body > *"); i < elems.length; i++)
	{
		if(elems[i].offsetWidth > 0)
		{
			$(elems[i]).setCSS("margin-top", "0px");
			break;
		}
	}
	
	function revealBody() {
		var wrapHeight = parseInt($("#body-wrap")[0].offsetHeight, 10);
		var bodyHeight = parseInt($("#body")[0].offsetHeight, 10);
		
		// Set the opacity of the body according to the amount that is visible.
		var opacity = Math.min(Math.round(100 * wrapHeight / bodyHeight), 100);
		$("#body-wrap").setCSS("opacity", opacity / 100);
		if($("#body-wrap")[0].filters)
			$("#body-wrap").setCSS("filter", "progid:DXImageTransform.Microsoft.Alpha(Opacity=" + opacity + ")");
		
		// If the wrapper isn't at the desired height, continue to increase the
		// height.
		if(wrapHeight != bodyHeight) {
			var delta = Math.max(Math.min((bodyHeight - wrapHeight) / 7, 40), 1);
			wrapHeight += delta;
			$("#body-wrap").setCSS("height", wrapHeight + "px");
			setTimeout(revealBody, 20);
		}
		// If the wrapper is at the desired height, prevent the overflow style from
		// clipping expandable content.
		else {
			$("#body-wrap").setCSS("overflow", "visible");
			
			// Make sure the filter doesn't make the text look unnatractive.
			if($("#body-wrap")[0].filters)
				$("#body-wrap").setCSS("filter", "");

			// Create the first two random colors and then start the fader.
			runBGFader.start = new Color("#FFF");
			runBGFader.end = new Color("#000");
			runBGFader.percent = 0;
			runBGFader();
		}
	}

	// Don't make the body-wrap visible until now just in case something can be
	// seen on the first line of pixels.
	$("#body-wrap").setCSS("visibility", "visible");
	
	// Show the body by fading it in.
	revealBody();
})();