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

// Function to resize an iframe to its real size so that we get rid of the scroll-bar on the actual iframe.
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 currentHeight = parseInt( o2GetComputedStyle(iframeRef, "height") );
    var newHeight     = parseInt( ifr.document.body.scrollHeight          );
    if (currentHeight >= newHeight) {
      newHeight = ifr.o2GetComputedStyle(ifr.document.body, "height") + getTopSpacing(ifr, ifr.document.body.parentNode);
    }
    iframeRef.style.height = newHeight + "px";
    ifr.document.body.parentNode.style.height = 0;
  }
  catch (e) {
    if (window.console) {
      console.log('Iframe resize error: ' + getExceptionMessage(e));
    }
  }
}

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. The top padding is the sum of the top paddings of the elements.
  And the top spacing is the sum of these two sums.
*/
function getTopSpacing(frame, elm) {
  var totalTopSpacing = 0;
  var types = ["marginTop", "paddingTop"];
  for (var i = 0; i < types.length; i++) {
    var type = types[i];
    var topSpacing = parseInt( frame.o2GetComputedStyle(elm, type) );
    for (var j = 0; j < elm.childNodes.length; j++) {
      var result = _getTopSpacing(frame, elm, topSpacing, type);
      topSpacing = _getSpacing(topSpacing, result.topSpacing, type);
      if (result.foundElementWithText) {
        totalTopSpacing += topSpacing;
        j = elm.childNodes.length; // Break out of the inner loop
      }
    }
  }
  return totalTopSpacing;
}

function _getTopSpacing(frame, elm, currentSpacing, type) {
  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,
        topSpacing           : currentSpacing
      };
      break;
    }
    if (!isTextNode) {
      var topSpacing = parseInt( frame.o2GetComputedStyle(_elm, type) );
      currentSpacing = _getSpacing(currentSpacing, topSpacing, type);
      result = _getTopSpacing(frame, _elm, currentSpacing, type);
      if (result.foundElementWithText) {
        return result;
      }
      currentSpacing = result.topSpacing;
    }
  }
  return {
    foundElementWithText : 1,
    topSpacing           : currentSpacing
  }
}

function _getSpacing(spacing, newSpacing, type) {
  if (type === "topMargin") {
    spacing = newSpacing > spacing ? newSpacing : spacing; // Vertical margins collapse
  }
  else {
    spacing += newSpacing;
  }
  return spacing;
}

