// need jquery (1.7.1+) to run current page

// trigger resizeIframe function if current page has a parent page
// id: ID of current page's body tag.
//     If body tag has no ID or the supplied ID is blank, it will resize ALL iframes in parent page
//     If the IDs match, it will resize ONLY the matching iframe(s)
// win: window DOM of child page
//     This parameter is used for fail safe when JQUERY cannot get height information from the content
function resizeParentIframe(id, win) {
    if (window.parent != window)
        window.parent.resizeIframe(id, win);
}

// scroll parent page to top if current page has a parent page
function scrollParentToTop() {
    if (window.parent != window)
        window.parent.scrollToTop();
}

// function to get root window
function getParentWindow(currentWindow) {
    if (currentWindow.parent == currentWindow)
        return currentWindow;
    else {
        // Catch possible cross domain access to window.parent if
        // parent is kiosk iframe
        //---------------------------------------------------------------------
        try {
            // AX-7292.10941 Now PA and 1A are in same host so we need to check for Default.aspx page
            if (currentWindow.window.location.pathname === '/PatientAccess/Default.aspx') {
                return currentWindow;
            }
        } catch (e) {
            // Cross domain caught: Chrome/Firefox/Safari
            return currentWindow;
        }
        //---------------------------------------------------------------------
        return getParentWindow(currentWindow.parent);
    }
}

// function to get specific window
function getChildWindow(id) {
    return document.getElementById(id).contentWindow;
}

// private use only
// resizes iframe to its content height
function resizeIframe(id, win) {
    $($("iframe.PAiFrame").get().reverse()).each(function () {                 // 52498835 Only iframes with PAiFrame class to avoid error with other external iframe attached to the page.     
        var currBodyId = $(this).contents().find("body").attr("id");
        if (id == "" || currBodyId == "" || id == currBodyId) {

            // get current scroll position so when page is reset to 0px and resized in this function,
            // the scroll position can remain the same
            var scrollY;
            if (window.pageYOffset != null)
                scrollY = window.pageYOffset;

            // For IE or any browser in standard mode
            else if (window.document.compatMode == "CSS1Compat")
                scrollY = window.document.documentElement.scrollTop;

            // For browsers in Quirks mode
            else
                scrollY = window.document.scrollTop;

            //Reset iframe height to 0 - to make calculation for height consistant
            // Removing this - causes issues on the iPad. Better to leave some extra space at the bottom than have these issues.
            //$(this).height(0);

            //Update iframe height to consistant
            var h = this.contentWindow.document.body.scrollHeight + 60;//$(win.document).height(); // $(this).contents().find("html").height();
            if ($(this).contents().find("#page") && $(this).contents().find("#page").length > 0)
            {
                h = $(window).height() - ($('#headerContainer').height() + $('#mainNavContainer').height() + $('#footer').height()+60);
             
            }

            if (h == 0)
                h = (win.document.documentElement.scrollHeight || win.document.body.scrollHeight) + 60;

            // set height of the iframe to match content
            $(this).height(h); 

            // scroll to initial position before reseting the content height, but don't scroll to very top
            /*
            if (scrollY != 0)
                window.scrollTo(0, scrollY);*/

            return false;
        }
        return false;
    });
}

// private use only
// scrolls to top
function scrollToTop() {
    window.scrollTo(0, 0);
}


//49215983 
function resizeParentIframe(src, win) {
    if (window.parent != window) {
        window.parent.resizeIframe(src, win);
    }
}

//49215983 
function openPopup(src, id, css, func) {
    if (window.parent != window) {
        try {
            window.parent.openIframe(src, id, css, func);
        } catch (e) {
            // Catch possible cross domain access to window.parent if
            // parent is kiosk iframe
            openIframe(src, id, css, func);
        }
    } else {
        openIframe(src, id, css, func);
    }
}

//49215983 Function to open a Window in iFrame
function openIframe(src, id, css, func) {
    if ($("#mask" + id).length > 0 || $("#window" + id).length > 0) {
        // element with the provided id already exists in current tab
        // add timestamp to ID to make it unique
        id += new Date().getTime();
    }

    var maskId = "mask" + id;
    var maskCss = "mask";
    var windowId = css + id;
    var windowCss = css;
    var windowFrameId = "windowFrame" + id;
    var windowFramecss = css + "Frame";
    var buttonId = "closeButton" + id;
    var buttonCss = "closeButton";
    var frameId = "popupFrame" + id;
    var frameCss = css + "PopupFrame";

    //Disable page events
    disablePage();

    //mask
    var maskLayer = createLayer(maskId, maskCss, true);
    maskLayer.style.zIndex = getLayerIndex(maskId);
    resizeLayer(maskId);

    //window place holder
    var windowLayer = createLayer(windowId, windowCss, true);
    var offsetY = $(document).scrollTop();
    windowLayer.style.top = offsetY + parseInt($(windowLayer).css('top')) + "px";
    windowLayer.style.zIndex = getLayerIndex(windowId);

    //window frame
    createContent(windowId, windowFrameId, windowFramecss);

    //iframe
    createIframe(windowFrameId, frameId, frameCss, src);

    //button
    var btn = createCloseButton(windowFrameId, buttonId, buttonCss); //windowFrameId
    if (btn != null) {
        btn.onclick = function () {
            if (func != null)
                func();
            else {
                deleteLayer(maskId);
                deleteLayer(windowId);
                enablePage();
            }
        };
    }

    //Add resize mask layer function to window resize event
    window.onresize = function () {
        resizeLayer(maskId);
    };
}

function closeIframe(id, css) {
    var maskId = "mask" + id;
    var windowId = css + id;
    deleteLayer(maskId);
    deleteLayer(windowId);
    enablePage();
}

// Todo: Refactor this function to be more general, need to recibe (Function for MessageContact)
function callParentIframe(parentFrameId, toTxt, ccTxt, toIds, ccIds, idsEncrypt) {
    if (window.parent != window) {
        window.parent.MessageParentCall(parentFrameId, toTxt, ccTxt, toIds, ccIds, idsEncrypt);
    } else {
        MessageParentCall(parentFrameId, toTxt, ccTxt, toIds, ccIds, idsEncrypt);
    }
}

// Todo: Refactor this function to be more general. (Function for MessageCompose)
function MessageParentCall(parentFrameId, toTxt, ccTxt, toIds, ccIds, idsEncrypt) {
    
    var $f = $("#" + parentFrameId);
    // Check if frame have Content Window (is null when open MessageCompose.aspx in separate window)
    if ($f.contents().find("body").html() != null)
        $f[0].contentWindow.MessageComposeUpdate(toTxt, ccTxt, toIds, ccIds, idsEncrypt);       //52924117
    else
        window.MessageComposeUpdate(toTxt, ccTxt, toIds, ccIds, idsEncrypt);                    //52924117
}

