/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 * 
 * Project: OpenSubsystems
 * 
 * $Id: basicwebsite.js,v 1.2 2007/01/07 06:14:24 bastafidli Exp $
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/**
 * JavaScript used by basic website layout to correctly resize the elements 
 * on the page based on the size of the window.
 *
 * @version $Id: basicwebsite.js,v 1.2 2007/01/07 06:14:24 bastafidli Exp $
 * @author Miro Halas
 * @code.reviewer Miro Halas
 * @code.reviewed 1.8 2005/03/29 06:50:00 bastafidli
 */

// window.alert("basicwebsite.js loaded");

/**
 * Make sure that this window is a top level window. 
 * If this page is embedded in the frame, it will replace the parent.
 *
 * @param bMakeMeTop - if true then this page will replace parent
 *                     if embedded in the frame
 */               
function makeMeTop(
   bMakeMeTop
)
{
   if ((typeof bMakeMeTop != "undefined") && (bMakeMeTop != null) && (bMakeMeTop))
   {
      if (self != top)
      {
         top.location.href = document.location.href;
         return false;
      }
   }
}

/**
 * Resize given container to fill the whole client area of the browser
 *
 * @param strContainerId - ID of the container representing content area, 
 *                         which should be expanded
 * @param bScrollBarsVisible - if false then for IE the scrollbars will be 
 *                             turned off since they are displayed even if
 *                             the page scroll itself. It has no effect for 
 *                             Gecko based browsers
 * @return iHeight - new height of the content area
 */               
function resizeContentArea(
   strContainerId,
   bScrollBarsVisible
)
{
   var eBody;
   var eContainer;
   var iClientHeight = 0;
                    
   eContainer = document.getElementById(strContainerId);
   if (eContainer == null)
   {
      window.alert("Cannot find container " + strContainerId);
   }
   
   eBody = document.getElementsByTagName("BODY")[0];
   if (eBody == null)
   {
      window.alert("Cannot find BODY element.");
   }

   // IE doesn't know how to hide the scrollarbars if the overflow is auto
   if ((bScrollBarsVisible == false) && (browser.isIE))
   {
      eBody.style.overflow = 'hidden';
   }   
   
   iClientHeight = eBody.clientHeight;

   // For Mozilla and Firefox must be retrieved client height by another way 
   if (browser.isNS)
   {
      iClientHeight = window.innerHeight;
   }

   eContainer.style.height = iClientHeight;

   // window.alert("content area is " + iClientHeight);

   return iClientHeight;
}

/**
 * This method should be hooked up on body.onload to correctly set up 
 * the initial sizes of the content area body which holds the whole content
 * of the page.
 *
 * @param strContentAreaId - ID of the area holding the body content 
 *                           (container for strBodyId)
 * @param iContentAreaHeight - desired height of the content area, we need to get 
 *                             this, since for example if the table has previous
 *                             size bigger than the content area, the content 
 *                             area will be stretched and we cannot find what
 *                             size we want the table to be
 * @param strHeaderId - ID of the area representing header
 * @param strBodyId - ID of the area representing the body content 
 *                    (container for strBodyContentId)
 * @param strBodyContentId - ID of the area representing the content of the body 
 *                           (contained in strBodyId)
 * @param strFooterId - ID of the area representing footer
 * @return iBodyContentHeight - height of the  content (strBodyContentId)
 */ 
function resizeContentBody(
   strContentAreaId,
   iContentAreaHeight,
   strHeaderId,
   strBodyId,
   strBodyContentId,
   strFooterId
)
{
   var eHeader;
   var eBody;
   var eContent;
   var eFooter;   
   var iBodyExtraHeight; // extra height of the table around the body
                         // due to header and footer
   var iContentExtraHeight; // extra height around content due to padding and margins
   var iBodyHeight;
   var iContentHeight;
   
   if (strHeaderId != null)
   {
      eHeader = document.getElementById(strHeaderId);
      if (eHeader == null)
      {
         window.alert("Cannot find element with id " + strHeaderId);
      }
   }
   else
   {
      eHeader = null;
   }
   
   eBody = document.getElementById(strBodyId);
   if (eBody == null)
   {
      window.alert("Cannot find element with id " + strBodyId);
   }
   
   eContent = document.getElementById(strBodyContentId);
   if (eContent == null)
   {
      window.alert("Cannot find element with id " + strBodyContentId);
   }

   if (strFooterId != null)
   {
      eFooter = document.getElementById(strFooterId);
      if (eFooter == null)
      {
         window.alert("Cannot find element with id " + strFooterId);
      }
   }
   else
   {
      eFooter = null;
   }
   
   // Count the height of the various elements which the content consists of 
   // so we know how much space is left for the content area
   iBodyExtraHeight  = 0;
   if (eHeader != null)
   {
      iBodyExtraHeight += eHeader.offsetHeight;
      // window.alert("header is " + eHeader.offsetHeight);
   }
   if (eFooter != null)
   {
      iBodyExtraHeight += eFooter.offsetHeight;
      // window.alert("footer is " + eFooter.offsetHeight);
   }

   // Set the size of the content body to fill the whole container except
   // the space for the headers, footer, etc.
   // We should subtract for Netscape also width of borders but there is
   // no way to find it out, therefore the body cannot have any top and bottom 
   // border (and if it does, we need to subtract it here)

   if (iContentAreaHeight < (iBodyExtraHeight + 100))
   {
      // window.alert("adjusting iContentAreaHeight from " + iContentAreaHeight);
      iContentAreaHeight = iBodyExtraHeight + 100;
   }
   // window.alert("iContentAreaHeight is " + iContentAreaHeight);
   
   iBodyHeight = iContentAreaHeight - iBodyExtraHeight;
   iContentExtraHeight = eBody.offsetHeight - eContent.offsetHeight;
   if (iContentExtraHeight < 0)
   {
      iContentExtraHeight = 0;
   }
   if (browser.isNS)
   {
      // On Mozilla, the way the border collapse forces us to make the height
      // smaller by the since of the borders. The padding has to be on the 
      // container element and not on the container element
      iBodyHeight = iBodyHeight - iContentExtraHeight;
   }
   eBody.style.height = iBodyHeight;
   // window.alert("adjusting body height to " + iBodyHeight);
   if (browser.isNS)
   {
      // We have already adjusted for the iContentExtraHeight
      iContentHeight = iBodyHeight;
   }
   else
   {
      iContentHeight = iBodyHeight - iContentExtraHeight;
   }
   eContent.style.height = iContentHeight
   // window.alert("adjusting content height to " + iContentHeight);

   // Return the real value we have changed the height to depending on what browser 
   // are we on
   return iContentHeight;
}

