﻿/* 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);
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) {
    window.setTimeout("InternalSetIFrameSize(" + bResizeHeight + ", " + bResizeWidth + ")", 10);
}

function InternalSetIFrameSize(bResizeHeight, bResizeWidth) {
    var path = getParameter('iframeurl');
    var iframeId = getParameter('iframeId');

    var 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 (debug) {
            if (bResizeHeight)
                alert("Height: " + height);
             
            if (bResizeWidth)
                alert("Width: " + height);
        }
        
        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';
        }
        
        var url = path + '?iframeId=' + iframeId;
        if (bResizeWidth)
            url += '&width=' + width;

        if (bResizeHeight)
            url += '&height=' + height;

        url += "&script=" + GetMyPath();
        
        iframe.src = url;

        document.body.appendChild(iframe);
    }
}

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");
    
    if (iframeId != null) {
        var iframe = window.top.document.getElementById(iframeId);
        if (iframe != null) {
            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';
            }
        }
    }
}

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;
        }
    }
}