/****************************************************************

  Traction Software, Inc. Confidential and Proprietary Information

  Copyright (c) 1996-2007 Traction Software, Inc.
  All rights reserved.

****************************************************************/

// PLEASE DO NOT DELETE THIS LINE -- make copyright depends on it.

function getVariableLeftOcean() {
  return Util.hasStylesheet("/html/css/ocean/layouts/variable.css");  
}

function getVariableLeftMexico() {
  return Util.hasStylesheet("/html/css/mexico/layout-variable.css");  
}

function getMaxRigthWidth() {
  if (Util.hasStylesheet("/html/css/ocean/layouts/fixedsmall.css")) return 160;
  if (Util.hasStylesheet("/html/css/ocean/layouts/fixedmedium.css")) return 183;
  if (Util.hasStylesheet("/html/css/ocean/layouts/fixedlarge.css")) return 210;
  // variable.css
  return 180;
}

var inside_fixlayout = false;

function fixlayout() {
  if (!inside_fixlayout) {
    inside_fixlayout = true;

    var body   = document.getElementById("body");

    var left   = document.getElementById("left");
    var middle = document.getElementById("middle");
    var right  = document.getElementById("right");
    var main   = document.getElementById("main");

    var middlet = document.getElementById("middletop");
    var middlec = document.getElementById("middlecontent");
    var middleb = document.getElementById("middlebottom");
  
    var lwidth = left.offsetWidth;
    var rwidth = right.offsetWidth;

    var isVariableOcean = getVariableLeftOcean();
    var isVariableMexico = getVariableLeftMexico();

    if (isVariableOcean || isVariableMexico) {
      // don't compress, only expand
      var leftMinWidth = isVariableOcean ? 116 : 175;
      if (lwidth < leftMinWidth) {
	lwidth = leftMinWidth;
      }
      
      // adjust the width
      middle.style.marginLeft  = lwidth + "px";
      left.style.width         = lwidth + "px";
    }

    // in some browsers the content will be allowed to grow beyond the
    // maximum size and the size reported will be the new large size.
    // if this happens, push it off tot he right so that it doesn't
    // cover the middle column
    var maxRight = getMaxRigthWidth();
    if (rwidth > maxRight) {
      // need to push it off the screen
      right.style.right = (maxRight - rwidth) + "px";
    }

    var viewportBounds = Util.getViewportBounds();

    var leftBounds = Util.getElementBounds(left);
    var middleBounds = Util.getElementBounds(middle);
    var rightBounds = Util.getElementBounds(right);
    var docBounds = Util.getDocumentBounds();

    var middlecBounds = Util.getElementBounds(middlec);
    var middleScroll = middle.scrollWidth;

    // ----------------------------------------------------------------------
    // stretch the view to create a horizontal scrollbar [ajm 21.Jun.2006]

    // typically this accounts for a vertical scrollbar and the fact
    // that firefox doesn't give us a good viewport width.  we already
    // account for it in Util.getViewportBounds, but it's not perfect
    var excessWidthAllowed = 5; 

    // don't change these unless you change all browsers
    var extraPadding = 2;
    var extraMiddle = 10;

//     alert("middleBounds="+middleBounds.w+" middleScroll="+middle.scrollWidth+"("+middlec.scrollWidth+")");
//     alert("docwidth="+docBounds.w+" columns="+(leftBounds.w+middleBounds.w+rightBounds.w)+" viewport="+viewportBounds.w);
//     alert((leftBounds.w + middleBounds.w + rightBounds.w)+" > "+(viewportBounds.w + excessWidthAllowed));

//     if (middleScroll < middleBounds.w) {
//       alert('0');
//       // sometimes happens with overflow: hidden in firefox.  also
//       // happens with overflow: scroll/auto.
//     }
    if (middleScroll > middleBounds.w) {
//       alert('1');
      middle.style.width = px(middleScroll + extraMiddle);
      middleScroll = middle.scrollWidth;
      right.style.left = px(leftBounds.w + middleScroll + extraPadding);
      
      // don't do this anymore [ajm 11.Jan.2008]
//       body.style.width = px(leftBounds.w + middleScroll + rightBounds.w + extraPadding); // don't know why we need 2*extraPadding
    }
    // maybe IE, because scrollWidth == width always in IE
    else if (docBounds.w > viewportBounds.w + excessWidthAllowed) {
//       alert('2');

      // we no longer do any of these [ajm 11.Jan.2008]
//       middle.style.width = px(middleBounds.w + extraMiddle);
//       right.style.left = px(middleBounds.x + middleBounds.w + extraMiddle + extraPadding + extraPadding); // don't know why we need 2*extraPadding
//       body.style.width = px(docBounds.w + extraMiddle + extraPadding + extraPadding);
    }
    // maybe the right column is overlapping the middle because of min-width or something
    else if (rightBounds.x + extraPadding < leftBounds.w + middleBounds.w) {
//        alert('3');
//       right.style.left = px(leftBounds.w + middleBounds.w);
//       body.style.width = px(leftBounds.w + middleBounds.w + rightBounds.w);
      body.style.minWidth = px(leftBounds.w + middleScroll + rightBounds.w + extraPadding);
    }

    // ----------------------------------------------------------------------

    var middlebBounds = Util.getElementBounds(middleb);
    var middlecBounds = Util.getElementBounds(middlec);

    // fix the heights [ajm 28.Feb.2005]
    try {
      var idealheight = 
	viewportBounds.h         // total available area
	- middlecBounds.y        // distance from top
	- middlebBounds.h        // height of footer
	- 110                    // padding-bottom of middlecontent 
	- 3;                     // fudge

      if (middlecBounds.h < idealheight) {
	middlec.style.height = idealheight + "px";
	var newTopicArticleButton = document.getElementById("newtopicarticle");
	if (newTopicArticleButton) {
	  newTopicArticleButton.style.position = "static";
	}
      }

      //     var leftmiddle = document.getElementById("leftmiddle");

      //     var leftheight = outer_height(left);
      //     var middleheight = outer_height(middle);

      //     var lmheight = (leftheight > middleheight) ? leftheight : middleheight;
      //     leftmiddle.style.height = lmheight + "px";

    } catch (e) {}
 
    inside_fixlayout = false;
  }
}

function body_onload() {
  fixlayout();
}
