﻿/* Created 2009-01-29 by Jonas Johansson, Capitex AB
Version: 1.0

För att denna ska fungera så måste följande uppfyllas:
- Sidan som innehåller den Iframe som ska vara dynamisk MÅSTE till i TOP fönstret.
- De obligatoriska parametrarna måste anges (se nedan).
- SetIFrameSize måste köras på innehållssidan (se nedan)
- Samtliga sidor (huvudsidan, innehållssidan måste ha en giltig referens till DynamicIFrame.js)

Obligatoriska parametrar som anges i URL:en
iframeurl: URL:en till resizeIframe.htm som ska ligga på samma domän som den sidan som har iframen som ska resizas.
iframeId: ID på Iframen som ska vara dynamisk.

Funktionen SetIFrameSize måste köras på sidan som är innehållet.
SetIFrameSize(bResizeHeight, bResizeWidth, debug);
Möjliga värden för debug är:
'true', 'remote', 'local'

Ex:
- För vanlig postback: SetIFrameSize(true, false);
- För AJAX ASP.NET: ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "DynamicIFrame", "SetIFrameSize(true, false);", True)

Ex:
Denna Iframe ligger i en sida på domänen www.newsec.se och anropar en lista som ligger på newsecv2.capitex.se.
resizeIframe.htm ligger på samma domän (iframeurl=http://www.newsec.se/resizeIframe.htm) som sidan.
<iframe id="ctl00_MainBodyOuter_MainBody_MainBodyRegion_capitexIframe" width="684" scrolling="no" src="http://newsecv2.capitex.se/Lista.aspx?t=CMBoLgh&amp;iframeId=ctl00_MainBodyOuter_MainBody_MainBodyRegion_capitexIframe&amp;iframeurl=http://www.newsec.se/resizeIframe.htm"></iframe>

Lista.aspx kör:
ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "DynamicIFrame", "SetIFrameSize(true, false);", True)
*/

function ResizeIFrame(iframeId, height, width) {
    if (iframeId != null) {
        var iframe = document.getElementById(iframeId);
        if (iframe != null) {
            if (height != null && height != "")
                iframe.setAttribute("height", height + 'px');

            if (width != null && width != "")
                iframe.setAttribute("width", width + 'px');
        }
    }
}

function SetIFrameSize(bResizeHeight, bResizeWidth, debug) {
    window.setTimeout("InternalSetIFrameSize(" + bResizeHeight + ", " + bResizeWidth + ", '" + debug + "')", 10);
}

var lastHeight = -1;
var lastWidth = -1;
var timesChecked = 0;
function InternalSetIFrameSize(bResizeHeight, bResizeWidth, debug) {
    var path = getParameter('iframeurl');
    var iframeId = getParameter('iframeId');

    if (path == null)
        alert('The parameter "iframeurl" is missing och incorrect. It\'s case sensitive!');

    if (iframeId == null)
        alert('The parameter "iframeId" is missing och incorrect. It\'s case sensitive!');

    if (debug == null)
        debug = getParameter("debug");

    if (path != "" && iframeId != "") {
        var height = Math.max(document.body.offsetHeight, document.body.scrollHeight);
        var width = Math.max(document.body.offsetWidth, document.body.scrollWidth);

        if ((bResizeHeight && height != lastHeight) || (bResizeWidth && width != lastWidth)) {

            if (debug == "true" || debug == "local")
                alert("LOCAL DEBUGGING:\nHeight: " + height + "\nWidth: " + width);

            var iframe = document.getElementById('DynamicIframeResizer');
            if (iframe == null) {
                iframe = document.createElement("iframe");
                iframe.id = "DynamicIframeResizer";
                iframe.style.visibility = 'hidden';
                iframe.style.position = 'absolute';
                iframe.style.width = '1px';
                iframe.style.height = '1px';
                iframe.style.top = '0px';
                iframe.style.left = '0px';
                document.body.appendChild(iframe);
            }

            var url = unescape(path) + '?iframeId=' + iframeId;
            if (bResizeWidth)
                url += '&width=' + width;

            if (bResizeHeight)
                url += '&height=' + height;

            url += "&script=" + GetMyPath();

            if (debug)
                url += "&debug=" + debug;

            if (debug == "true" || debug == "local")
                alert("LOCAL DEBUGGING:\niframeURL: " + url);

            iframe.src = url;

            lastHeight = height;
            lastWidth = width;
        }

        if (timesChecked++ < 10)
            window.setTimeout("InternalSetIFrameSize(" + bResizeHeight + ", " + bResizeWidth + ", '" + debug + "')", 500);

    }
}

function getParameter(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function CallTopResizeIFrame() {
    var iframeId = getParameter("iframeId");
    var height = getParameter("height");
    var width = getParameter("width");
    var debug = getParameter("debug");


    if (iframeId != null) {
        try {
            var iframe = window.top.document.getElementById(iframeId);
            if (iframe != null) {
                if (debug == "true" || debug == "remote")
                    alert('REMOTE DEBUGGING:\nCurrent:\n   Height: ' + iframe.style.height + '\n   Width: ' + iframe.style.width + '\n\nNew:\n   Height: ' + height + "\n" + '   Width: ' + width);

                if (height != null && height != "") {
                    iframe.setAttribute("height", height + 'px');
                    iframe.style.height = height + 'px';
                }

                if (width != null && width != "") {
                    iframe.setAttribute("width", width + 'px');
                    iframe.style.width = width + 'px';
                }
            }
            else {
                if (debug == "true" || debug == "remote")
                    alert('REMOTE DEBUGGING: Unable to find iframe with Id: ' + iframeId);
            }
        }
        catch (e) {
            if (debug == "true" || debug == "remote")
                alert('REMOTE DEBUGGING: Error:\n' + e.description);
        }
    }
    else {
        if (debug == "true" || debug == "remote")
            alert('iframeId is missing!');
    }
}

function GetMyPath() {
    var scriptTags = document.getElementsByTagName("script");
    for (var i = 0; i < scriptTags.length; i++) {
        var src = scriptTags[i].getAttribute('src');
        if (src.toLowerCase().indexOf("dynamiciframe.js")) {

            var protocol = document.location.protocol;
            var host = document.location.host;
            var path = document.location.pathname;

            var srcEndsWithSlash = src.charAt(0) == "/" || src.charAt(0) == "\\";

            if (path.length > 0) {
                var endIndex = path.lastIndexOf("\\");
                var endIndexSlash = path.lastIndexOf("/");

                if (endIndex > -1 && endIndex > endIndexSlash) {
                    if (!srcEndsWithSlash)
                        endIndex += 1;

                    path = path.substring(0, endIndex);
                }
                else if (endIndexSlash > -1) {
                    if (!srcEndsWithSlash)
                        endIndexSlash += 1;

                    path = path.substring(0, endIndexSlash);
                }
            }

            if (src.charAt(0) == "/" || src.charAt(0) == "\\") {
                return protocol + "//" + host + src;
            }
            else {
                return protocol + "//" + host + path + src;
            }


            i = scriptTags.length;
        }
    }
}
