//
// Fairways layout script
//

// Number of options in the menu
var g_option_count = 6;

/**
 * Layout the page by moving elements into place
 */
function Layout()
{
	// Valid element?
	if( document.body )
	{
		// Reveal the page?
		document.body.className = "";
	}

	//
	// Center the page
	//
	
	// Find the page element
	var page = document.getElementById( "page" );
	
	// Valid element?
	if( page )
	{
		// Use absolute positioning
		page.style.position = "absolute";
			
		// Left position required to centre page
		var left = ( document.body.clientWidth - page.clientWidth ) / 2;
		
		// Check that this is positive
		left = Math.max( left, 0 );

		// Centre horizontally
		page.style.left = left + "px";
	}

	// Find the top
	var top = document.getElementById( "top" );

	// Valid element?
	if( top )
	{
		// Find the 'bottom' of the top element
		var tops_bottom = top.style.top + top.offsetHeight;

		// Add padding to create the top of the menu
		var menu_top = parseInt( tops_bottom ) + 7;

		// Get the options
		var options = Array();
	
		// Success flag
		var success = true;
	
		// Loop over the options and get the elements
		for( var index = 0 ; index < g_option_count ; ++index )
		{
			// Get the element
			options[index] = document.getElementById( "option" + index );
	
			// Invalid?
			if( !options[index] )
			{
				// Failed to find all the options
				success = false;
			}
		}
	
		// Successfully found all the elements?
		if( success )
		{
			// The accumulated width of the options
			var total_width = 0;
			
			// Loop over the options
			for( var index = 0 ; index < g_option_count ; ++index )
			{
				// Set absolute positioning
				options[index].style.position = "absolute";
	
				// Set the top
				options[index].style.top = menu_top + "px";
	
				// Add the width
				total_width += options[index].offsetWidth;
			}
	
			// Calculate the padding
			var padding = (726 - total_width) / (g_option_count - 1);
	
			// X position
			var x = 17;
	
			// Loop over the elements
			for( var index = 0 ; index < g_option_count ; ++index )
			{
				// Set the left of this element
				options[index].style.left = x + "px";
	
				// Move x along
				x += options[index].offsetWidth + padding;
			}		
		}
	}

	// Find the middle element
	var middle = document.getElementById( "middle" );
	
	// Found middle?
	if( middle )
	{
		// Set the left of the middle to indent correctly
		middle.style.left = "7px";

		// Find the menu
		var menu = document.getElementById( "option0" );

		// Valid element?
		if( menu )
		{
			// Use absolute positioning
			middle.style.position = "absolute";			

			// Bottom
			var menu_height =  + menu.offsetHeight + 7;

			// Limit to 30 px
			menu_height = Math.min( menu_height, 27 );

			// Set the top of the middle to be under the menu
			middle.style.top = menu.offsetTop + menu_height + "px";
		}	

		// Find the middle foreground element
		var middle_foreground = document.getElementById( "middle_foreground" );

		// Valid element?
		if( middle_foreground )
		{
			// Find the content
			var content = document.getElementById( "content" );

			// Find the image
			var content_image = document.getElementById( "content_image" );

			// Valid content element?
			if( content && content_image )
			{
				// Get the height of the content
				var content_height = content.offsetTop + content.offsetHeight + 10;

				// Get the height of the image
				var content_image_height = content_image.offsetTop + content_image.offsetHeight + 10;

				// Get the maximum
				var height = Math.max( content_height, content_image_height );

				// Ensure a minimum size to the middle foreground
				height = Math.max( height, 325 );

				// Set the height of the middle foregound to hold the content
				middle_foreground.style.height = height + "px";
			}

			// Set the bottom of the middle section to be below the foreground
			middle.style.height = middle_foreground.offsetTop + middle_foreground.offsetHeight + 2 + "px";
		}

		// Find the highlight
		var highlight = document.getElementById( "highlight" );
	
		// Valid element
		if( highlight )
		{
			// Set height
			highlight.style.height = "30px";

			// Find the anchor
			var anchor = document.getElementById( "highlight_anchor" );

			// Valid?
			if( anchor )
			{
				// Set width
				highlight.style.width = anchor.offsetWidth + "px";
			}
		}

		// Find the bottom
		var bottom = document.getElementById( "bottom" );

		// Find the bottom foreground
		var bottom_foreground = document.getElementById( "bottom_foreground" );

		// Find the contact details
		var contact_details = document.getElementById( "contact_details" );
		
		// Found bottom?
		if( bottom && bottom_foreground && contact_details )
		{
			// Absolute position
			bottom.style.position = "absolute";

			// Set the left of the bottom to indent correctly
			bottom.style.left = "7px";

			// Put the bottom after the middle
			bottom.style.top = middle.offsetTop + middle.offsetHeight + 7 + "px";

			// Get the height of the 
			var contact_height = contact_details.offsetHeight;

			// Maximum height
			var max_height = Math.max( 48, contact_height );

			// Set the height of the foreground
			bottom_foreground.style.height = max_height;

			// Set the position of the details
			contact_details.style.top = ((max_height - contact_height) / 2) + "px";

			// Set the height of the bottom 
			bottom.style.height =  max_height + 4 + "px";

			// Find the copyright notice
			var copyright_notice = document.getElementById( "copyright_notice" );
			
			// Found bottom?
			if( copyright_notice )
			{
				// Absolute position
				copyright_notice.style.position = "absolute";
	
				// Put the copyright_notice after the bottom
				copyright_notice.style.top = bottom.offsetTop + bottom.offsetHeight + 7 + "px";
	
				// Place in the middle
				copyright_notice.style.left = (760 - copyright_notice.offsetWidth)/2 + "px"
			}
		}
	}
}

/**
 * On load handler
 */
window.onload = function()
{
	Layout();
}

/**
 * On resize of window handler
 */
window.onresize = function()
{
	Layout();
}
