// --------------------------------------
// KNOWLEDGE MAP CONTENT LAYOUT FUNCTIONS
// --------------------------------------
// Copyright (c) 2004-2007 The Salamander Organization Ltd.  All Rights Reserved.
//
// THIS WORK IS SUBJECT TO U.K. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
//
// DISCLAIMER:
//
// The Salamander Organization Ltd. (hereto referred as "Salamander") makes no representations or warranties
// with respect to the contents or use of this code, and specifically disclaims any express or implied
// warranties of merchantability or fitness for any particular purpose.  Further, Salamander reserves the right
// to revise this publication and to make changes to its content, at any time, without obligation to notify any
// person or entity of such revisions or changes.
//
// Further, Salamander makes no representations or warranties with respect to any software, and specifically
// disclaims any express or implied warranties of merchantability or fitness for any particular purpose.  Salamander
// reserves the right to make changes to any and all parts of the software, at any time, without obligation to notify
// any person or entity of such changes.
//
// Inclusion of the above notice does not necessarily imply publication.

// ########################
// ### SYSTEM VARIABLES ###

var extraBottomOffset = 0;  // should be zero for most cases

var footerHeight = 0;
var gotFooterHeight = false;


// ################################
// ### CONTENT LAYOUT FUNCTIONS ###

function SetContentHeights()
{	
	SetMainContentHeight();
	SetNavigationBarHeight();
}

// set the height of the main content DIV tag
function SetMainContentHeight()
{
	var mainContentSection = document.getElementById("mainContentSection");

	var copyrightBlock = document.getElementById("copyrightInfo");
	var copyrightOffset = 0;

	if (mainContentSection)
	{
		var totalTopOffset = ElementTopOffset(mainContentSection, 0) + mainContentSection.offsetTop;
		
		if (copyrightBlock)
		{
			// check that the copyright block is visible
			if (GetStyleValue(copyrightBlock, "display") == "block")
			{
				// if the copyright block is present on the page, how high is it?
				copyrightOffset = copyrightBlock.offsetHeight;

				var copyrightTopMargin = parseInt(StripChars(GetStyleValue(copyrightBlock, "marginTop"), "units"));

				// also add the top margin
				if (copyrightTopMargin > 0)
				{
					copyrightOffset = copyrightOffset + copyrightTopMargin;
				}
			}
		}
		
		mainContentSection.style.height = document.body.clientHeight - totalTopOffset - GetFooterHeight() - extraBottomOffset - copyrightOffset;
		mainContentSection.style.display = 'block';
	}
}

// set the height of the main content DIV tag
function SetNavigationBarHeight()
{
	var navigationBarSection = document.getElementById("navigationBarSection");
	var separatorBarSection = document.getElementById("separatorBar");
	var navBarHeight;
	
	var poweredbyLogoBlock = document.getElementById("poweredbyLogo");
	var logoOffset = 0;

	if (navigationBarSection)
	{
		var totalTopOffset = ElementTopOffset(navigationBarSection, 0) + navigationBarSection.offsetTop;
		
		if (poweredbyLogoBlock)
		{
			// check powered by block is visible
			if (GetStyleValue(poweredbyLogoBlock, "display") == "block")
			{
				logoOffset = poweredbyLogoBlock.offsetHeight;

				var logoTopMargin = parseInt(StripChars(GetStyleValue(poweredbyLogoBlock, "marginTop"), "units"));

				// also add the top margin
				if (logoTopMargin > 0)
				{
					logoOffset = logoOffset + logoTopMargin;
				}
			}
		}
		
		navBarHeight = document.body.clientHeight - totalTopOffset - GetFooterHeight() - extraBottomOffset;
		navigationBarSection.style.height = navBarHeight - logoOffset;
		navigationBarSection.style.display = 'block';
	}
	
	if (separatorBarSection)
	{
		separatorBarSection.style.height = navBarHeight;
	}
}

// find the displayed height of the page footer
// -- only processed once per page, subsequent calls return the same value
function GetFooterHeight()
{
	if (!gotFooterHeight)
	{
		var pageFooter = document.getElementById("pageFooter");

		if (pageFooter)
		{
			var footerDisplay = GetStyleValue(pageFooter, "display");
			
			// check the footer is actually displaying
			if (footerDisplay == "block")
			{
				footerHeight = pageFooter.offsetHeight;
				
				var footerTopMargin = parseInt(StripChars(GetStyleValue(pageFooter, "marginTop"), "units"));
				
				// also add the top margin for the footer
				if (footerTopMargin > 0)
				{
					footerHeight = footerHeight + footerTopMargin;
				}
			}
		}
		
		gotFooterHeight = true;
	}
	
	return footerHeight;
}

// find how far the top of the main content DIV tag is from the top of the content window
function ElementTopOffset(pageElement, knownOffset)
{
	if (pageElement.offsetParent == null) return knownOffset;
	else return ElementTopOffset(pageElement.offsetParent, knownOffset + pageElement.offsetTop);
}

// find how far the left of the main content DIV tag is from the left of the content window
function ElementLeftOffset(pageElement, knownOffset)
{
	if (pageElement.offsetParent == null) return knownOffset;
	else return ElementLeftOffset(pageElement.offsetParent, knownOffset + pageElement.offsetLeft);
}

// reveal an initially hidden page
// a "still loading" work around so that the navigation bar is initially displayed but doesn't "blink" on the page load
function RevealPage()
{
	var pagePanel = document.getElementById("mainPagePanel");
	var pleaseWaitPanel = document.getElementById("pleaseWaitPanel");
	
	if (pagePanel)
	{
		ClearPleaseWaitTimer();
		HidePleaseWaitPanel();
	
		// display the page
		pagePanel.style.display = 'block';
	}
	
	SetContentHeights();
}
