// heavily modified by GISmatters from script found at: 
//   http://www.dynamicdrive.com/dynamicindex17/iframessi2.htm

// set this to match the id of the <IFRAME> in the parent page
var iframeid = 'mapiframe';

// set these to match the ids of the Cartoweb divs that change size
var contentid = 'conteneur';
var leftbarid = 'conteneur';

// set these to tweak the fit of the <IFRAME> within the parent page
var IEextraHeight = 20;
var FFextraHeight = 50;

function resizeIframe() {
  // try to find the <IFRAME>
  var currentfr = document.getElementById(iframeid)
  if (currentfr) {
    // handle different browsers to get the Cartoweb page and set the height tweak
    if (typeof(currentfr.contentDocument) == 'object') {
      var iframedoc = currentfr.contentDocument;
      var extraHeight = FFextraHeight;
    } else if (typeof(currentfr.Document) == 'object') {
      var iframedoc = currentfr.Document;
      var extraHeight = IEextraHeight;
    }
    // set the <IFRAME> height to the taller of the Cartoweb divs, plus the tweak value
    var leftbardivheight = iframedoc.getElementById(leftbarid).offsetHeight;
    var contentdivheight = iframedoc.getElementById(contentid).offsetHeight;
    currentfr.height = ( leftbardivheight > contentdivheight ? leftbardivheight : contentdivheight ) + extraHeight;
  }
  // reset the event listener
  if (currentfr.addEventListener)
    currentfr.addEventListener("load", resizeIframe, false);
  else if (currentfr.attachEvent) {
    currentfr.detachEvent("onload", resizeIframe); // Bug fix line
    currentfr.attachEvent("onload", resizeIframe);
  }
}

// add an event to fire the resize script when the parent page first loads
if (window.addEventListener)
  window.addEventListener("load", resizeIframe, false);
else if (window.attachEvent)
  window.attachEvent("onload", resizeIframe);
else
  window.onload=resizeIframe;
