/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 * 
 * Project: OpenSubsystems
 * 
 * $Id: application.js,v 1.3 2007/01/07 06:14:08 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 application layout to correctly resize the elements 
 * on the page based on the size of the window.
 *
 * @version $Id: application.js,v 1.3 2007/01/07 06:14:08 bastafidli Exp $
 * @author Miro Halas
 * @code.reviewer Miro Halas
 * @code.reviewed 1.24 2005/05/09 22:45:57 jlegeny
 */

// window.alert("application.js loaded");

var arrUpperAreaOriginalHeights = new Array(2); 
    arrUpperAreaOriginalHeights[0] = 0; // variable defining height of the upper area
    arrUpperAreaOriginalHeights[1] = 0; // variable defining original offset top of the upper iframe
var iUpperAreaMaxHeight = 0;      // variable defining counted max. height of the upper area

var bAllowMaxUpperArea = true;  // flag defining if there will be allowed to maximize
                                // upper area. If this will be not allowed, we cannot change
                                // flag 'bMaximizeUpperArea' to the 150TRUE value.
                                // This flag is also used for DEFINING INITIAL VALUE.
var bMaximizeUpperArea = bAllowMaxUpperArea; // flag defining if the upper area will be 
                                             // maximized (value = true) or restored (value = false)

/*
 * This method should be hooked up on body.onload to correctly set up 
 * the initial sizes of the upper application area and bottom application 
 * area to fill up the whole content area.
 *
 * @param strContentId - ID of the area holding the list and preview
 * @param iContentAreaHeight - height of content area in case it was resized,
 *                             if it is 0, it will be determined dynamically
 * @param strUpperAreaId - ID of the upper application area 
 * @param strBottomAreaId - ID of the bottom application area 
 * @param iMinBottomAreaHeight - minimal height of the bottom application area
 * @param strPadding - padding which needs to be accounted for since I don't
 *                     know how to compute it. This should be padding to make
 *                     the application looks good when both upper and bottom
 *                     area are displayed
 * @return integer - height of the the upper area. This value can be used
 *                   to make the content to fill the upper area
 */ 
function resizeApplicationLayout(
   strContentId,
   iContentAreaHeight,
   strUpperAreaId,
   strBottomAreaId,
   iMinBottomAreaHeight,
   strPadding
)
{
//   window.alert("strContentId=" + strContentId
//                + " iContentAreaHeight=" + iContentAreaHeight
//                + " strUpperAreaId=" + strUpperAreaId
//                + " strBottomAreaId=" + strBottomAreaId
//                + " iMinBottomAreaHeight=" + iMinBottomAreaHeight
//                + " strPadding=" + strPadding);

   var eContentArea; // the container containing the whole content area
   var eUpperArea;
   var eBottomArea;   
   var iBodyExtraHeight; // extra height of the table around the body
   var iPadding;
   
   // Figure out the width and height of the surrounding area
   eContentArea = document.getElementById(strContentId);
   if (eContentArea == null)
   {
      window.alert("Cannot find content area container " + strContentId);
   }

   eUpperArea = document.getElementById(strUpperAreaId);
   if (eUpperArea == null)
   {
      window.alert("Cannot find upper area element with id " + strUpperAreaId);
   }

   eBottomArea = document.getElementById(strBottomAreaId);
   if (eBottomArea == null)
   {
      window.alert("Cannot find bottom area element with id " + strBottomAreaId);
   }
   // Count the height of the various elements which the content consists of 
   // so we know how much space is left for the preview area
   if ((typeof strPadding != "undefined") && (strPadding != null) && (strPadding != ""))
   {
      // TODO: Improve: application.css has 5px padding on list and 5px on preview,
      // which I don't know how to account for so I added this feature to allow
      // to specify it programatically
      iPadding = parseInt(strPadding);
      if (bMaximizeUpperArea)
      {
         // If we are maximizing upper area we do not have space between upper 
         // and bottom area and therefore we can count only 2/3rds of paddings
         iPadding = iPadding * 2 / 3;
      }
   }
   else
   {
      iPadding = 0;
   }
   iBodyExtraHeight = iPadding;

   if (iContentAreaHeight == 0)
   {
      // Dynamically determine size of the content area
      iContentAreaHeight = eContentArea.offsetHeight;
      
      if (browser.isNS)
      {
         iContentAreaHeight = iContentAreaHeight - iPadding;
      }
   }

   // initialize variable for storing original size of upper area
   if (arrUpperAreaOriginalHeights[0] == 0)
   {
      arrUpperAreaOriginalHeights[0] = eUpperArea.offsetHeight;
   }
   if (bMaximizeUpperArea)
   {
      // if the preview dialog should be hidden, stretch list to the whole page
      iUpperAreaMaxHeight = iContentAreaHeight - iPadding; 
      eUpperArea.style.height = iUpperAreaMaxHeight;
   }
   else
   {
      eUpperArea.style.height = arrUpperAreaOriginalHeights[0];
   }

   iBodyExtraHeight += eUpperArea.offsetHeight;

   // Set the size of the preview to fill the whole container except
   // the space for the list, etc.
   var eBottomAreaElement = eBottomArea.firstChild;
   // TODO: Improve: These two styles are defined in patterns tab.css
   while ((eBottomAreaElement != null)
         && (eBottomAreaElement.className != 'clsTabPreviewActive')
         && (eBottomAreaElement.className != 'clsTabPreviewActiveLoaded'))
   {
      eBottomAreaElement = eBottomAreaElement.nextSibling;
   }
   
   if (iMinBottomAreaHeight > iContentAreaHeight - iBodyExtraHeight)
   {   
      if (eBottomAreaElement != null)
      {
         eBottomAreaElement.style.height = iMinBottomAreaHeight;
         // The page is longer that the content area and we need to let user scroll
         eBottomArea.style.height = iMinBottomAreaHeight;
         eContentArea.style.overflow = 'auto';
      }
      else
      {
         // If bottom area element doesn't exist set up eBottomArea = 0 and
         // disable scrollbars. Here cannot be used display="none" for eBottomArea
         // object because it will be hidden forever an nobody shows its again.
         eBottomArea.style.height = 0;
      }
   }
   else
   {
      if (eBottomAreaElement != null)
      {
         eBottomAreaElement.style.height = iContentAreaHeight - iBodyExtraHeight;
      }
      eBottomArea.style.height = iContentAreaHeight - iBodyExtraHeight;
      // Everything fits, we don't need to scroll otherwise IE always displays 
      eContentArea.style.overflow = 'hidden';
   }

   return eUpperArea.offsetHeight;
}

/*
 * Change flag singaling if upper area has to be maximalized or not. This flag
 * is depended on flag bAllowMaxUpperArea and also this will be checked here. This
 * check will be only on this place.
 *
 * @param bMaxUpperArea - flag singaling if upper area has to be maximalized or not
 */
function setMaximizeUpperArea(
   bMaxUpperArea
)
{
   if (bAllowMaxUpperArea)
   {
      bMaximizeUpperArea = bMaxUpperArea;
   }
}

/**
 * Remove all existing messages.
 */
function clearMessages()
{
   var eMsgParentElement = document.getElementById("appmessages");
   
   if (eMsgParentElement != null)
   {
      var eInfoMsgs = document.getElementById("appmessagesinfo");
      var eErrorMsgs = document.getElementById("appmessageserror");
   
      if (eInfoMsgs != null)
      {
         var eMsgListElement = eInfoMsgs.firstChild;
         while (eMsgListElement != null)
         {
            eMsgListElement.parentNode.removeChild(eMsgListElement);
            eMsgListElement = eInfoMsgs.firstChild;
         }
      }
      else
      {
         window.alert("Cannot find message area with id appmessagesinfo");
      }
      
      if (eErrorMsgs != null)
      {
         var eMsgListElement = eErrorMsgs.firstChild;
         while (eMsgListElement != null)
         {
            eMsgListElement.parentNode.removeChild(eMsgListElement);
            eMsgListElement = eErrorMsgs.firstChild;
         }
      }
      else
      {
         window.alert("Cannot find message area with id appmessageserror");
      }
      eMsgParentElement.className = "clsMessagesEmpty";

      // We just changed size of the page we need to resize it. Application
      // layout is derived from basic website layour so called the top level
      // function controlling the sizing of the web page
      onResizeBasicWrapper();
   }
   else
   {
      window.alert("Cannot find message area with id appmessages");
   }
}

/**
 * Add message to the message area.
 *
 * @param arMessages - array of messages
 * @param arMessageTypes - type of messages "error", "info"
 */
function addMessages(
   arMessages,
   arMessageTypes
)
{
   var eMsgParentElement = document.getElementById("appmessages");
   
   if (eMsgParentElement != null)
   {
      if ((arMessages != null) && (arMessages.length > 0))
      {
         eMsgParentElement.className = "clsMessages";
         
         var eInfoMsgs = document.getElementById("appmessagesinfo");
         var eErrorMsgs = document.getElementById("appmessageserror");
      
         if (eInfoMsgs == null)
         {
            window.alert("Cannot find message area with id appmessagesinfo");
         }
         if (eErrorMsgs == null)
         {
            window.alert("Cannot find message area with id appmessageserror");
         }
         
         var eMessageElement;
         
         for (iCount = 0; iCount < arMessages.length; iCount++)
         {
            eMessageElement = document.createElement("DIV");
            eMessageElement.innerHTML = arMessages[iCount];
            if (arMessageTypes[iCount] == "error")
            {
               eErrorMsgs.appendChild(eMessageElement);
            }
            else if (arMessageTypes[iCount] == "info")
            {
               eInfoMsgs.appendChild(eMessageElement);
            }
            else
            {
               window.alert("Unknown message type " + arMessageTypes[iCount] 
                            + " at index " + iCount);
            }
         }
         onResizeBasicWrapper();
      }
   }
   else
   {
      window.alert("Cannot find message area with id appmessages");
   }
}

