﻿/*
 * LayoutAdjuster.js: Utilities to adjust layouts.
 */

 /*
  * Constructs a new LayoutAdjuster.
  */
function LayoutAdjuster() {
  /*
   * Adjusts the height any number of boxes so that they have the same height
   * (this is used e.g. by the ProgramByDay layout).
   * IMPORTANT: Internet Explorers need this to be done a moment after the page
   *            was loaded ... if you do it too early, the offsetHeights will be 0,
   *            so use setTimeout(..., 1); or OnLoad!
   */
  this.adjustHeight = function(boxIDArray) {
    var boxElemArray = new Array(boxIDArray.length);
    var boxElemsExist = true;
    for (var i = 0; i < boxIDArray.length; i++) {
      boxElemArray[i] = document.getElementById(boxIDArray[i]);
      if (!boxElemArray[i]) {
        boxElemsExist = false;
      } else {
        boxElemArray[i].style.height = '';
      }
    }
    if (boxElemsExist) {
      var neededHeight = 0;
      for (var i = 0; i < boxElemArray.length; i++) {
        neededHeight = Math.max(neededHeight, boxElemArray[i].offsetHeight);
      }
      for (var i = 0; i < boxElemArray.length; i++) {
        boxElemArray[i].style.height = neededHeight + 'px';
      }
    }
  };
  
  /*
   * Makes sure that one element has the same height as another element.
   */
  this.copyHeight = function(from, to) {
    var fromElem = document.getElementById(from);
    var toElem = document.getElementById(to);
    //alert('From: ' + from + ' = ' + fromElem + ', to: ' + to + ' = ' + toElem);
    if (fromElem && toElem) {
      fromElem.style.height = '';
      toElem.style.height = '';

      var height = fromElem.offsetHeight;
      toElem.style.height = height + 'px';
      // IE hack cause IE does it all wrong
      if (toElem.offsetHeight > height) {
        height = height - (toElem.offsetHeight - height);
        toElem.style.height = height + 'px';
      }
    }
  };
  
  /*
   * Resizes elementId to use the maximum available document size
   */
  this.maximumSize = function(elementId) {
      var doc = document;

      var element = doc.getElementById(elementId);
      // make sure this element doesn't keep our size expanded
      element.style.width = '0px';
      element.style.height = '0px';

      var height = Math.max(
              Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight),
              Math.max(doc.body.offsetHeight, doc.documentElement.offsetHeight),
              Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
          );

      var width = Math.max(
              Math.max(doc.body.scrollWidth, doc.documentElement.scrollWidth),
              Math.max(doc.body.offsetWidth, doc.documentElement.offsetWidth),
              Math.max(doc.body.clientWidth, doc.documentElement.clientWidth)
          );
          
      element.style.width = width + 'px';
      element.style.height = height + 'px';
  };
}

var layoutAdjuster = new LayoutAdjuster();

