
/***********************************************
* 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 = ["myframe"]

//Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide = "yes"

var isIE = navigator.userAgent.indexOf('MSIE') != -1;
var isFirefox = navigator.userAgent.indexOf('Firefox') != -1;
var isChrome = navigator.userAgent.indexOf('Chrome') != -1;
var isSafari = navigator.userAgent.indexOf('Safari') != -1;  // true anche in Chrome

// alert('navigator.userAgent: ' + navigator.userAgent);
// alert('navigator.appVersion: ' + navigator.appVersion);

var ExtraHeight = 20;

function resizeCaller() {
    var dyniframe = new Array()
    for (i = 0; i < iframeids.length; i++) {
        if (document.getElementById) {  // tutti
            document.getElementById(iframeids[i]).style.display = "block";        
            // alert('document.getElementById');
            firstLoad(iframeids[i]);   // iframeids[i] = "myframe" 
            // alert('iframeids[i]: ' + iframeids[i]);            
        }
        //reveal iframe for lower end browsers? (see var above):
        if ((document.all || document.getElementById) && iframehide == "no") {   // nessuno
            // alert('(document.all || document.getElementById) && iframehide == "no"');
            var tempobj = document.all ? document.all[iframeids[i]] : document.getElementById(iframeids[i]);
            tempobj.style.display = "block";
        }
    }
}

function resizeIframe(frameid) {
    var currentfr = document.getElementById(frameid);
    if (currentfr && !window.opera) {  // tutti
        // alert('currentfr && !window.opera');
        currentfr.style.display = "block";
        if (isSafari) {
            currentfr.height = currentfr.contentDocument.body.offsetHeight + ExtraHeight;
            //alert('currentfr.contentDocument.body.offsetHeight: ' + currentfr.contentDocument.body.offsetHeight);
            //alert('currentfr.contentWindow.document.body.offsetHeight: ' + currentfr.contentWindow.document.body.offsetHeight);
            //alert('currentfr.contentWindow.document.body.scrollHeight: ' + currentfr.contentWindow.document.body.scrollHeight);
            //alert('currentfr.contentDocument.body.scrollHeight: ' + currentfr.contentDocument.body.scrollHeight);
        }
        else
            currentfr.height = currentfr.contentWindow.document.body.scrollHeight + ExtraHeight;
        addListener(currentfr);
    }
}

function firstLoad(frameid) {
    var currentfr = document.getElementById(frameid);
    currentfr.height = currentfr.contentWindow.document.body.scrollHeight + ExtraHeight;
    addListener(currentfr);
}

function addListener(currentfr) {
    if (currentfr.addEventListener) {  // safari 3.0.2, chrome 3.0, firefox 3.0.1
        currentfr.addEventListener("load", readjustIframe, false);
    }
    else if (currentfr.attachEvent) { // Explorer 7.0, Explorer 8.0
        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);  // iframeroot.id = 'myframe' con tutti i browser
}

function loadintoIframe(iframeid, url) {
    if (document.getElementById)
        document.getElementById(iframeid).src = url
}

if (window.addEventListener) {   // firefox 3.0.1, safari 3.0.2, chrome 3.0
    // alert('window.addEventListener');
    window.addEventListener("load", resizeCaller, false);
}
else if (window.attachEvent) {  // Explorer 7.0, Explorer 8.0
    window.attachEvent("onload", resizeCaller)
    // alert('window.attachEvent');  
}
else {
    window.onload = resizeCaller;
    // alert('né window.addEventListener né window.attachEvent');
}

//alert('document.getElementById(\'myframe\').content Document: ' + document.getElementById('myframe').contentDocument);  // undefined solo in IE7
//alert('document.getElementById(\'myframe\').contentWindow.document: ' + document.getElementById('myframe').contentWindow.document);

function changeHeight(iframe) {
    try {
        var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
        if (innerDoc.body.offsetHeight) //ns6 syntax
        {
            iframe.height = innerDoc.body.offsetHeight + 32; //Extra height FireFox
        }
        else if (iframe.Document && iframe.Document.body.scrollHeight) //ie5+ syntax
        {
            iframe.height = iframe.Document.body.scrollHeight;
        }
    }
    catch (err) {
        alert(err.message);
    }
}

