/*
Function to resize an iframe to its real size so that we get rid of the scroll-bar on the actual iframe.
You can set minimun size to resize it to, bheltne@onsite.no 10.03.2006

Håkon, 20060601. Had problems with this in Explorer. Now it works ok in Explorer, but the iframe is a little too
big in Firefox.
*/

require("/o2www/js/DOMUtil.js");

function resizeIframeById(id, minSize) {
  minSize = minSize || 200;
  if (document.getElementById(id)) {
    setTimeout("_resizeIframe(document.getElementById('" + id + "'));", 50);
  }
}

function _resizeIframe(myIframe) {
  if (myIframe) {
    if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) {
      _autoSizeIE(myIframe.id);
    }
    else {
      _autoSize(myIframe.id);
    }
  }
}

function _autoSizeIE(id) {
  try {
    var iframeRef = document.getElementById(id);
    _autoSize(id);
    var ifr = iframeRef.contentWindow;
    var h   = parseInt(ifr.document.body.scrollHeight);
    if (h > parseInt(iframeRef.style.height)) {
      setTimeout("_autoSizeIE('"+id+"');", 1000);
    }
  }
  catch (e) {
    setTimeout("_autoSizeIE('"+id+"');", 1000);
  }
}

function _autoSize(id) {
  try {
    var iframeRef = document.getElementById(id);
    var ifr = iframeRef.contentWindow;
    var newHeight = parseInt(ifr.document.body.scrollHeight) + parseInt( getTopMargin(ifr.document.body) );
    iframeRef.style.height = newHeight + "px";
  }
  catch (e) {
    // alert('resize error: ' + getExceptionMessage(e)); // XXX Commented out because of inatur ( => 15 apr 2008 )
  }
}

function resizeIframeContinuously(iframe) {
  _resizeIframe(iframe);
  setTimeout("resizeIframeContinuously(document.getElementById('" + iframe.id + "'))", 1000);
}

/*
  Go through child nodes of the given element, depth first, until an element with textual
  content is found. The top margin is then the maximum of the top margins of the searched
  through elements.
*/
function getTopMargin(elm) {
  var topMargin = parseInt( o2GetComputedStyle(elm, "marginTop") );
  for (var i = 0; i < elm.childNodes.length; i++) {
    var result = _getTopMargin(elm, topMargin);
    topMargin = result.topMargin > topMargin ? result.topMargin : topMargin;
    if (result.foundElementWithText) {
      return topMargin;
    }
  }
  return topMargin;
}

function _getTopMargin(elm, currentMargin) {
  for (var i = 0; i < elm.childNodes.length; i++) {
    var _elm = elm.childNodes[i];
    var isTextNode = _elm.nodeType == 3;
    var text = isTextNode ? _elm.data : _elm.innerText || "";
    if (text.match(/[^\s]/)) {
      return {
        foundElementWithText : 1,
        topMargin            : currentMargin
      };
      break;
    }
    if (!isTextNode) {
      var topMargin = parseInt( o2GetComputedStyle(_elm, "marginTop") );
      currentMargin = topMargin > currentMargin ? topMargin : currentMargin;
      result = _getTopMargin(_elm, currentMargin);
      if (result.foundElementWithText) {
        return result;
      }
      currentMargin = result.topMargin;
    }
  }
  return {
    foundElementWithText : 1,
    topMargin            : currentMargin
  }
}
