var __nfmm_Debug = false;

var __nfmw_LeftStart = 0;
var __nfmw_TopStart = 0;

var __nfmw_PopupWidth = 100;
var __nfmw_PopupHeight = 100;

var __nfmw_Popup = "5DCCF5B1_FEBB_4161_92AC_D164F876407E"
var __nfmw_PopupShadowOffsetTop = 3;
var __nfmw_PopupShadowOffsetLeft = 3;

var __nfmw_Key_BG = "NFModalWindowBackground_";
var __nfmw_Key_PP = "NFModalWindowPopup_";
var __nfmw_Key_SH = "NFModalWindowShadow_";

function __NFMW_ResetPopup(sCustomId) {

    if (!sCustomId) {
        sCustomId = __nfmw_Popup;
    }

    var o_NFMM_BG = document.getElementById(__nfmw_Key_BG + sCustomId);
    var o_NFMM_PP = document.getElementById(__nfmw_Key_PP + sCustomId);
    var o_NFMM_SH = document.getElementById(__nfmw_Key_SH + sCustomId);

    var i_ScrollTop = 0;
    var i_ImpressionistRuleAttempt = 50;
    var i_ClientHeight = 0;
    var i_ClientWidth = 0;
    var i_FullHeight = 0;

    if (BrowserDetect.browser == "Firefox") {
        i_ScrollTop = document.body.scrollTop;
        i_ClientHeight = window.innerHeight;
        i_ClientWidth = window.innerWidth;
        i_FullHeight = document.body.offsetHeight;
    }
    else if (BrowserDetect.browser == "GoogleChrome") {
        i_ScrollTop = document.body.scrollTop;
        i_ClientHeight = window.innerHeight;
        i_ClientWidth = window.innerWidth;
        i_FullHeight = document.body.offsetHeight;
    }
    else if (BrowserDetect.browser == "Safari") {
        i_ScrollTop = document.body.scrollTop;
        i_ClientHeight = window.innerHeight;
        i_ClientWidth = window.innerWidth;
        i_FullHeight = document.body.offsetHeight;
    }
    else if (BrowserDetect.browser == "Explorer") {
        i_ScrollTop = document.body.scrollTop;
        i_ClientHeight = document.body.offsetHeight;
        i_ClientWidth = document.body.offsetWidth;
        i_FullHeight = document.body.scrollHeight;
    }
    else {
        if (document.getElementById("NFCheckout")) { //BASKET WORK AROUND
            i_ScrollTop = document.body.scrollTop;
            i_ClientHeight = document.body.offsetHeight;
            i_ClientWidth = document.body.offsetWidth;
            i_FullHeight = document.body.clientHeight;
        }
        else //REST OF WEBSITE
        {
            i_ScrollTop = document.documentElement.scrollTop;
            i_ClientHeight = document.documentElement.offsetHeight;
            i_ClientWidth = document.documentElement.offsetWidth;
            i_FullHeight = document.body.clientHeight;
        }
    }

    if (__nfmm_Debug) {
        __nfmw_PopupHeight = 600;
    }


    i_ImpressionistRuleAttempt = (i_ClientHeight / 100) * 10;

    __nfmw_LeftStart = (i_ClientWidth / 2) - (__nfmw_PopupWidth / 2);
    __nfmw_TopStart = (i_ClientHeight / 2) - (__nfmw_PopupHeight / 2);
    if (__nfmw_TopStart > (i_ImpressionistRuleAttempt * 1.5)) {
        __nfmw_TopStart = __nfmw_TopStart - i_ImpressionistRuleAttempt;
    }

    //o_NFMM_BG.style.top = i_ScrollTop;
    o_NFMM_BG.style.height = "" + (i_FullHeight) + "px";

    o_NFMM_PP.style.top = "" + (i_ScrollTop + __nfmw_TopStart) + "px";
    o_NFMM_PP.style.left = "" + (document.documentElement.scrollLeft + __nfmw_LeftStart) + "px";

    if (__nfmm_Debug) {
        o_NFMM_PP.innerHTML = "<span style=\"font-size: 8pt;\">"
                                + "i_ScrollTop: " + i_ScrollTop
                                + "<br />__nfmw_TopStart: " + __nfmw_TopStart
                                + "<br />__nfmw_LeftStart: " + __nfmw_LeftStart
                                + "<br />__nfmw_PopupWidth: " + __nfmw_PopupWidth
                                + "<br />__nfmw_PopupHeight: " + __nfmw_PopupHeight
                                + "<br />i_ImpressionistRuleAttempt: " + i_ImpressionistRuleAttempt
                                + "<hr />"
                                + "<br />documentElement.scrollTop: " + document.documentElement.scrollTop
                                + "<br />body.scrollTop: " + document.body.scrollTop
                                + "<br />documentElement.scrollLeft: " + document.documentElement.scrollLeft
                                + "<br />body.scrollLeft: " + document.body.scrollLeft
                                + "<br />body.clientWidth: " + document.body.clientWidth + " / 2 = " + (document.body.clientWidth / 2)
                                + "<br />body.clientHeight: " + document.body.clientHeight + " / 2 = " + (document.body.clientHeight / 2)
                                + "<br />window.innerWidth: " + window.innerWidth + " / 2 = " + (window.innerWidth / 2)
                                + "<br />window.innerHeight: " + window.innerHeight + " / 2 = " + (window.innerHeight / 2)
                                + "<br />document.body.scrollHeight: " + document.body.scrollHeight
                                + "<br />document.documentElement.offsetHeight: " + document.documentElement.offsetHeight
                                + "<hr />"
                                + "<br />BrowserDetect.browser: " + BrowserDetect.browser
                                + "<br />BrowserDetect.version: " + BrowserDetect.version
                                + "</span>";

    }

    o_NFMM_SH.style.top = "" + (i_ScrollTop + __nfmw_TopStart + __nfmw_PopupShadowOffsetTop) + "px";
    o_NFMM_SH.style.left = "" + (document.documentElement.scrollLeft + __nfmw_LeftStart + __nfmw_PopupShadowOffsetLeft) + "px";
}

function __NFMW_PreparePopup(sCustomId, sContentsID, iWidth, iHeight) {

    if (sCustomId) {
        __nfmw_Popup = sCustomId;
    }

    var o_NFMM_CP = document.getElementById(sContentsID);

    __NFMW_AppendParts_Popup(__nfmw_Popup);

    var o_NFMM_BG = document.getElementById(__nfmw_Key_BG + __nfmw_Popup);
    var o_NFMM_PP = document.getElementById(__nfmw_Key_PP + __nfmw_Popup);
    var o_NFMM_SH = document.getElementById(__nfmw_Key_SH + __nfmw_Popup);

    __nfmw_PopupWidth = iWidth;
    __nfmw_PopupHeight = iHeight;

    if (o_NFMM_BG && o_NFMM_PP && o_NFMM_SH && o_NFMM_CP) {
        o_NFMM_BG.style.display = "block";

        o_NFMM_PP.style.display = "block";
        o_NFMM_PP.innerHTML = o_NFMM_CP.innerHTML;
        o_NFMM_PP.style.width = "" + __nfmw_PopupWidth + "px";
        o_NFMM_PP.style.height = "" + __nfmw_PopupHeight + "px";

        o_NFMM_SH.style.display = "block";
        o_NFMM_SH.style.width = "" + __nfmw_PopupWidth + "px";
        o_NFMM_SH.style.height = "" + __nfmw_PopupHeight + "px";

        window.onscroll = function () {
            __NFMW_ResetPopup(__nfmw_Popup);
        };

        __NFMW_ResetPopup(__nfmw_Popup);
    }
}

function __NFMW_AppendParts_Popup(sCustomId) {

    if (!sCustomId) {
        sCustomId = __nfmw_Popup;
    }

    var o_New_Object = document.createElement('div');
    o_New_Object.id = __nfmw_Key_BG + sCustomId;
    o_New_Object.className = "ModalBackground";
    o_New_Object.style.display = "none";
    document.body.appendChild(o_New_Object);

    o_New_Object = document.createElement('div');
    o_New_Object.id = __nfmw_Key_PP + sCustomId;
    o_New_Object.className = "ModalPopup";
    o_New_Object.style.display = "none";
    document.body.appendChild(o_New_Object);

    o_New_Object = document.createElement('div');
    o_New_Object.id = __nfmw_Key_SH + sCustomId;
    o_New_Object.className = "ModalShadow";
    o_New_Object.style.display = "none";
    document.body.appendChild(o_New_Object);

    o_New_Object = null;
}

function __NFMW_DestroyParts_Popup(sCustomId) {

    if (!sCustomId) {
        sCustomId = __nfmw_Popup;
    }

    if (document.getElementById(__nfmw_Key_PP + sCustomId)) {
        document.body.removeChild(document.getElementById(__nfmw_Key_PP + sCustomId));
    }
    if (document.getElementById(__nfmw_Key_SH + sCustomId)) {
        document.body.removeChild(document.getElementById(__nfmw_Key_SH + sCustomId));
    }
    if (document.getElementById(__nfmw_Key_BG + sCustomId)) {
        document.body.removeChild(document.getElementById(__nfmw_Key_BG + sCustomId));
    }
    window.onscroll = "";
}
