$(document).ready(prepSifr);
$(document).ready(finalizeLayout);

function prepSifr() {
   if (typeof sIFR == "function") {
      sIFR.replaceElement(named({
         sSelector:".contentarea h1", sFlashSrc:"../gfx/font.swf",
         sColor:"#3e0606", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC",
         nPaddingTop:0, nPaddingBottom:0,
         sFlashVars:"", sWmode : "transparent"
      }));
   };
}

function finalizeLayout() {
   var layoutSections = $('.header, .midsection, .footer');
   var mainCols = $('.navigation, .contentarea');
   var siteHeight = 0;
   var winHeight = $(window).height();

   layoutSections.each(function () {
      siteHeight += this.offsetHeight;
   });

   if (winHeight > siteHeight) {
      layoutSections.eq(1).height(
         winHeight
         - layoutSections.eq(0).height()
         - layoutSections.eq(2).height()
      );
      pullDownColumns(mainCols);
   }
   else {
      equalizeColumns(mainCols);
   }
}

/* proc equalizeColumns
 * applies the same height to an array of dom elements.
 * equalizeColumns assumes the argument is a jQuery element array.
*/
function equalizeColumns(jColumns) {
   var maxHeight = 0;
   jColumns.each(function () {
      maxHeight = Math.max(maxHeight, this.offsetHeight);
   });

   jColumns.each(function () {
      this.style.height = maxHeight
         - getStyleValue(this, 'padding-top', NOUNIT)
         - getStyleValue(this, 'padding-bottom', NOUNIT)
         - getStyleValue(this, 'border-top', NOUNIT)
         - getStyleValue(this, 'border-bottom', NOUNIT)
         + 'px';
   });
}

function pullDownColumns(jColumns) {
   jColumns.each(function () {
      this.style.height = this.parentNode.offsetHeight
         - getStyleValue(this, 'padding-top', NOUNIT)
         - getStyleValue(this, 'padding-bottom', NOUNIT)
         - getStyleValue(this, 'border-top', NOUNIT)
         - getStyleValue(this, 'border-bottom', NOUNIT)
         + 'px';
   });
}
