/**
  frame.js
  Resizable iframe. Call from parent of iframe.

  Copyright (c) 1999-2007 New Fire
 */
var frFitWidth;
var frFitHeight;

var frMinWidth;
var frMinHeight;

function frResize()
{
  frResize2Fit();
  frResize2Min();
  frResize2Max();
}

/**
  Resize frame to fit the browser window.
 */
function frResize2Fit()
{
  var theIframe = document.getElementById("AutoFitFrame");
  if (theIframe
    || (theIframe = document.getElementById("frameMarker")) != null)
  {
    document.body.style.overflow = "hidden";
    theIframe.scrolling = 'auto';

    frGetFitSizes(theIframe);

    theIframe.style.width = frFitWidth+"px";
    theIframe.style.height = frFitHeight+"px";
  } 
}
   
/**
  Resize frame to fit the content.
  If frame has a dimension, width and/or height; do not change that dimension.
 */
function frResize2Min()
{
  var theIframe = document.getElementById("AutoMinFrame");
  if (theIframe)
  {
//alert("frResize2Min: "+theIframe.style.width+", "+theIframe.style.height);
//alert("frResize2Min: "+theIframe.width+", "+theIframe.height);

    theIframe.scrolling = 'no';

    if (!theIframe.width)
    {
      theIframe.style.width = "1px";
    }

    if (!theIframe.height)
    {
      theIframe.style.height = "1px";
    }

    frGetMinSizes(theIframe);

    if (!theIframe.width)
    {
      theIframe.style.width = frMinWidth+"px";
    }

    if (!theIframe.height)
    {
      theIframe.style.height = frMinHeight+"px";
    }

    theIframe.style.visibility = 'visible';

    // Resize if iframe reloads by navigation
    if (self.attachEvent) // IE
    {
      theIframe.attachEvent("onload", function(){self.frResize()});
    }
    else
    {
      theIframe.onload=function(){self.frResize()};
    }
  }
}

/**
  Resize frame to fit the browser window. 
  If the content takes more space, enlarge frame to hold content.
 */
function frResize2Max()
{
  var theIframe = document.getElementById("AutoMaxFrame");
  if (theIframe)
  {
    // scroll may block right side, simpler just to show scrolls 
    //document.body.style.overflow = "scroll";
    document.body.style.overflow = "auto";
    theIframe.scrolling = 'no';

    // to fit size first
    frGetFitSizes(theIframe);

    if (!theIframe.width)
    {
      theIframe.style.width = frFitWidth+"px";
    }
    theIframe.style.height = frFitHeight+"px";
//    theIframe.style.height = (frFitHeight-10)+"px";

    // to min size if bigger
    // IE cannot shrink when resize to smaller
    frGetMinSizes(theIframe );

    var width = (frMinWidth > frFitWidth)? frMinWidth: frFitWidth;
    var height = (frMinHeight > frFitHeight)? frMinHeight: frFitHeight;

//alert("frResize2Max: fit("+frFitWidth+", "+frFitHeight+"); min("+frMinWidth+", "+frMinHeight+")");

    if (!theIframe.width)
    {
      theIframe.style.width = width+"px";
    }
    theIframe.style.height = height+"px";

//alert("frResize2Max: fit("+frFitWidth+", "+frFitHeight+"); min("+frMinWidth+", "+frMinHeight+")");

    theIframe.style.visibility = 'visible';

    // Resize if iframe reloads by navigation
    if (self.attachEvent) // IE
    {
      theIframe.attachEvent("onload", function(){self.frResize()});
    }
    else
    {
      theIframe.onload=function(){self.frResize()};
    }
  }
}

/**
  FIX: height seems to be slightly more
    need to remove margin.
 */
function frGetFitSizes(theIframe)
{
  if (document.body.clientWidth)  // IE & FF
  {
    width = document.body.clientWidth;
    height = document.body.clientHeight;
  }
  else
  if (window.innerWidth) // not sure who uses this
  {
    width = window.innerWidth;
    height = window.innerHeight;
  }

  var e = theIframe;
  var left = e.offsetLeft;
  var top = e.offsetTop;
  //var right = e.offsetRight;
  //var bottom = e.offsetBottom;

  while (e.offsetParent)
  {
    e = e.offsetParent;
    left += e.offsetLeft;
    top += e.offsetTop;
    //right += e.offsetRight;
    //bottom += e.offsetBottom;
  }

  frFitWidth = width - left;// - document.body.content.left;// - right;
  frFitHeight = height - top;// - document.body.content.top;// - bottom;
}

/**
  shrink it first, otherwise it would shrink to fit.
  it also smooths the display
 */
function frGetMinSizes(theIframe)
{
  // 4.5: IE cannot have 0px before using scrollwidth
  //theIframe.style.width = "1px";
  //theIframe.style.height = "1px";

  frMinWidth = theIframe.contentWindow.document.body.scrollWidth;
  frMinHeight = theIframe.contentWindow.document.body.scrollHeight;
}

if (self.attachEvent) // IE
{
  self.attachEvent("onresize", function(){self.frResize();});
  self.attachEvent("onload", function(){self.frResize();});
}
else
{
  self.onresize=function(){self.frResize()};
  self.onload=function(){self.frResize();};
}


