/***********************************************
* IFrame SSI script II- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/

// Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
// Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids=[];

// The names of functions we must call whenever we resize the iFrame for whatever reason.
var dynamicIframeCallbacks=[];

// Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide="yes"

var hasFirefox = navigator.userAgent.indexOf("Firefox");
var getFFVersion = (hasFirefox > -1 ? parseFloat(navigator.userAgent.substring(hasFirefox).split("/")[1]) : -1);

var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

// Extra height in px to add to iframe in FireFox 1.0+ browsers and Chrome
// due to errors in calculating the frame height in at least some situations.
var frameExtraHeight = (getFFVersion >= 0.1 || isChrome ? 80 : 0);

function registerFrameResizeCallback (callbackFunction)
  {
    dynamicIframeCallbacks.push(callbackFunction);
  }


// Calculate the height of the content of the passed frame.
// Some browsers work with one of these methods, some work with the other,
// and some support both methods, but only one returns the correct value.
// So, we have to try both and find the one that gives us the longer
// frame size (it may be wrong, but it is better to fail longer than
// to fail shorter and hide part of the frame).
function calculateFrameSize (theFrame)
  {
    var frameHeightMethodOne = -1;
    var frameHeightMethodTwo = -1;
    var frameHeightSpecial = -1;
    var frameHeight = -1;
    var baseURI;
    try
      {
        if (   theFrame.contentDocument
            && theFrame.contentDocument.body
            && theFrame.contentDocument.body.offsetHeight) //ns6 syntax
          {
            frameHeightMethodOne = theFrame.contentDocument.body.offsetHeight;
            baseURI = theFrame.contentDocument.body.baseURI;

            // Special case for IE, which doesn't have the baseURI
            if (   !baseURI
                && theFrame.contentDocument.body.ownerDocument
                && theFrame.contentDocument.body.ownerDocument.URLUnencoded)
              {
                baseURI = theFrame.contentDocument.body.ownerDocument.URLUnencoded;
              }
          }

        if (   theFrame.Document
            && theFrame.Document.body.scrollHeight) //ie5+ syntax
          {
            frameHeightMethodTwo = theFrame.Document.body.scrollHeight;
            baseURI = theFrame.Document.body.baseURI;

            // Special case for IE, which doesn't have the baseURI
            if (   !baseURI
                && theFrame.Document.body.ownerDocument
                && theFrame.Document.body.ownerDocument.URLUnencoded)
              {
                baseURI = theFrame.Document.body.ownerDocument.URLUnencoded;
              }
          }


        // Special case for ShipAddresses.aspx to make sure there
        // is enough room for the popup address dialog (which doesn't
        // appear to effect the browser calculation of height.
        if (baseURI && /ShipAddresses.aspx/.test(baseURI))
          {
            frameHeightSpecial = 700;
            //console.log("frameHeightSpecial = " + frameHeightSpecial);
          }

        //console.log("frameHeightMethodOne = " + frameHeightMethodOne);
        //console.log("frameHeightMethodTwo = " + frameHeightMethodTwo);
        //console.log("frameHeightSpecial = " + frameHeightSpecial);

        frameHeight = Math.max(frameHeightMethodOne, frameHeightMethodTwo, frameHeightSpecial)+frameExtraHeight;
      }
    catch (error)
      {
        /* The iFrame is not loaded yet, or we don't have permission. */
      }

    return(frameHeight);
  }

function followframe (frameId)
  {
    iframeids.push(frameId);

    var currentfr = document.getElementById(frameId),
        lastHeight = -1;

    setInterval(function()
                  {
                    var frameHeight = calculateFrameSize(currentfr);

                    // There are situations where calculateFrameSize fails miserably (like when the
                    // browser is hosting some other application like Acrobat Reader).  In that case
                    // we need to just use the last height we had so that the frame doesn't at least
                    // get any smaller.
                    if (frameHeight == -1)
                      {
                        frameHeight = lastHeight;
                      }

                    //console.log("hello from timed frame check, frame size = " + frameHeight + " last height = " + lastHeight + " - " + (new Date().getSeconds()));

                    if (frameHeight != lastHeight)
                      {
                        if (lastHeight > -1)
                          resizeIframe(frameId)
                        lastHeight = frameHeight;
                      }
                  }, 200);
  }

function resizeCaller ()
  {
    var dyniframe=new Array();
    for (i=0; i < iframeids.length; i++)
      {
        if (document.getElementById)
          {
            resizeIframe(iframeids[i]);
          }

        // reveal iframe for lower end browsers? (see var above):
        if (   (document.all || document.getElementById)
            && iframehide=="no")
          {
            var tempobj = (document.all ? document.all[iframeids[i]]
                                        : document.getElementById(iframeids[i]));
            tempobj.style.display = "block";
          }
      }
  }

function notifyParent ()
  {
    for (i=0; i < dynamicIframeCallbacks.length; i++)
      {
        dynamicIframeCallbacks[i]();
      }
  }

function resizeIframe (frameid)
  {
    //console.log("hello from resizeIframe");

    var currentfr = document.getElementById(frameid);
    if (currentfr && !window.opera)
      {
        currentfr.style.display = "block"
        try
          {
            var frameHeight = calculateFrameSize(currentfr);
            if (frameHeight > -1)
              {
                currentfr.height = frameHeight;
                notifyParent();
              }
          }
        catch (err)
          {
            // Nothing to do, let's try to still set the event listener.
          }

        if (currentfr.addEventListener)
          currentfr.addEventListener("load", readjustIframe, false);
        else if (currentfr.attachEvent)
          {
            currentfr.detachEvent("onload", readjustIframe); // Bug fix line
            currentfr.attachEvent("onload", readjustIframe);
          }
      }
  }

function readjustIframe (loadevt)
  {
    var crossevt = (window.event) ? event : loadevt;
    var iframeroot = (crossevt.currentTarget) ? crossevt.currentTarget : crossevt.srcElement;
    if (iframeroot)
      resizeIframe(iframeroot.id);
  }

function loadintoIframe (iframeid, url)
  {
    if (document.getElementById)
      document.getElementById(iframeid).src = url;
  }

if (window.addEventListener)
  window.addEventListener("load", resizeCaller, false);
else if (window.attachEvent)
  window.attachEvent("onload", resizeCaller);
else
  window.onload = resizeCaller;

