// BEGIN WEBND COPYRIGHT
// (c) 2000-2008 WebND Technologies PTY LTD (http://webnd.com.au/)
// Released under the GNU Affero GPL v3.  Seeing doc/COPYING for details.
// END WEBND COPYRIGHT


function resizeChildFrame(childframe) {
	var theFrameToResize = document.getElementById(childframe);
	// contentDocument method to detect and set size
	// (Navigator, Safari, Netscape, Mozilla, Firefox)
	if (theFrameToResize.contentDocument && theFrameToResize.contentDocument.body) {
		// Reset the width of the iframe
		//if (theFrameToResize.width != "100%") {
		//	theFrameToResize.style.width=1;
		//}
		theFrameToResize.style.height=1;
		// Set the width and height of the iframe
		if (theFrameToResize.width != "100%") {
			theFrameToResize.style.width=theFrameToResize.contentDocument.documentElement.scrollWidth + "px";
		}
		theFrameToResize.style.height=theFrameToResize.contentDocument.body.offsetHeight + "px";
	}
	// Document method to detect and set size
	// (Windows IE)
	if (theFrameToResize.Document && theFrameToResize.Document.body){
		// Reset the width of the iframe
		//if (!theFrameToResize.width != "100%") {
		//	theFrameToResize.style.width=1;
		//}
		theFrameToResize.style.height=1 + "px";
		if (theFrameToResize.width != "100%") {
			// IE detects scrollWidth as the width of the iframe until it is wider than the content
			// so we need to increase the width of the iframe until it is wider than the content
			while (theFrameToResize.style.width <= theFrameToResize.Document.body.scrollWidth) { // 
				theFrameToResize.style.width = (theFrameToResize.style.width + 1) + "px";
			}
			// Set the width of the iframe
			theFrameToResize.style.width = (theFrameToResize.Document.body.scrollWidth) + "px";
		}
		// IE detects scrollHeight as the height of the iframe until it is higher than the content
		// so we need to increase the height of the iframe until it is wider than the content
		theFrameToResize.style.height = 1 + "px";
		while (theFrameToResize.style.height <= theFrameToResize.Document.body.scrollHeight) {
			theFrameToResize.style.height = (theFrameToResize.style.height + 1) + "px";
		}
		// Set the height of the iframe
		theFrameToResize.style.height = theFrameToResize.Document.body.scrollHeight + "px";
	}
	makeItResize();
}

function replaceIframeWithContentRestartCountdownTimers(inTimerid) {
  var timerid = inTimerid;
  if (timerid) {
    var elem = document.getElementById(timerid);
    var duration = elem.expires || elem.remaining_time;
    countdown(timerid,duration);
  }
}

function replaceIframeWithContent(inID, inContent, inTimerid) {
  var id = inID;
  var frame = document.getElementById(id);
  var timerid = inTimerid;
  if (!frame) {
    replaceIframeWithContentRestartCountdownTimers(timerid);
    return;
  }
  var content = inContent;
  if (!content) {
    replaceIframeWithContentRestartCountdownTimers(timerid);
    return;
  }

  var forms = content.getElementsByTagName('form');
  //Special cases we allow in templates but which break DOM manipulation.
  if (forms.length > 0) {
    for (var i = 0; i < forms.length; i++) {
      if (forms[i].parentNode.nodeName == 'TR' || forms[i].parentNode.nodeName == 'TABLE') {
	replaceIframeWithContentRestartCountdownTimers(timerid);
        return;
      }
    }
  }
  var span = document.createElement('span');
  var contentHTML = content.innerHTML;
  span.innerHTML = contentHTML;
  frame.parentNode.replaceChild(span, frame);
  replaceIframeWithContentRestartCountdownTimers(timerid);
}

function canReplaceIframeWithContent () {
  if(document.getElementById &&
     document.getElementsByTagName &&
     document.body.parentNode &&
     document.body.nodeName &&
     document.body.replaceChild &&
     document.body.innerHTML) {
    return true;
  }
  return false;
}

function makeItResize() {
	// Pass the name of this window (frame) to it's parent to resize the frame it is in
	if (window.name && window.parent.resizeChildFrame) window.parent.resizeChildFrame(window.name);
}

function replaceOrResize () {

  var timerid = "";

  if (window.name && window.parent &&
      window.parent.canReplaceIframeWithContent && 
      window.parent.replaceIframeWithContent){
    if (window.parent.canReplaceIframeWithContent() == true) {
      var spans = document.getElementsByTagName('span');
      for (var i = 0; i < spans.length; i++) {
	if (spans[i].timer) {
          clearTimeout(spans[i].timer);
          timerid = '"' + spans[i].getAttribute('id') + '"';
          continue;
        }
      }

      var callback = 'window.parent.replaceIframeWithContent(window.name,document.body';
      if (timerid) {
        callback = callback + ',' + timerid;
      }
      callback = callback + ')';
      window.setTimeout(callback,1500);
    return;
    }
  }
  makeItResize();
}
