﻿var PopupEvents = new Object(); PopupEvents.onOpening = new Array(); PopupEvents.onPosition = new Array(); PopupEvents.onClose = new Array(); PopupEvents.onClosing = new Array(); PopupEvents.gObjects = new Array();
PopupEvents.onOpening.push(function(o) {
    PopupEvents.gObjects.length = 0;
    var ob = null;

    ob = document.getElementsByTagName("iframe");
    for (var i = 0; i < ob.length; i++) {
        if (ob[i].name == "Zebra.Web.UI.Webcontrols.Popup") break;

        PopupEvents.gObjects[PopupEvents.gObjects.length] = ob[i];
        ob[i].style.display = "none";
    }

    if (!Popup.bck) {
        Popup.bck = document.createElement("div");
        Popup.bck.className = "popupMask";
        document.getElementsByTagName("body")[0].appendChild(Popup.bck);

    }
    o.frame.style.height = '1px';
    o.frame.style.width = '1px';
    o._safe = window.setTimeout(ResizePopup, 3000);
})

function ResizePopup() {
    for (var i in Popup.items) {
        if (Popup.items[i]) Popup.items[i].Position(false);

    }
}

PopupEvents.re = function(t) {
    var ob = document.getElementsByTagName(t);
    for (var i = 0; i < ob.length; i++) {
        PopupEvents.gObjects[PopupEvents.gObjects.length] = ob[i];
        ob[i].style.display = "none";
    }
}
PopupEvents.onOpening[PopupEvents.onOpening.length] = function(o) {

    PopupEvents.re("embed");

}

PopupEvents.onClose[PopupEvents.onClose.length] = function(o) {
    if (!Popup.glwa() && Popup.bck) {
        document.body.removeChild(Popup.bck);
        Popup.bck = null;

        window.parent.document.getElementsByTagName("HTML")[0].style.overflow = "auto";
        window.scroll(o._currentScroll[0], o._currentScroll[1]);
    }
    if (!Popup.glwr("h")) {
        for (var i = 0; i < PopupEvents.gObjects.length; i++) {
            PopupEvents.gObjects[i].style.display = "";
        }
        PopupEvents.gObjects.length = 0;
    }
}
PopupEvents.bck = null;
Popup.properties = {
    "t": "<table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"pop_tl\"></td><td class=\"pop_brd\"></td><td class=\"pop_tr\"></td></tr><tr valign=\"top\"><td class=\"pop_brd\"></td><td id=\"[CONTAINER]\"></td><td class=\"pop_brd\"></td></tr><tr><td class=\"pop_bl\"></td><td class=\"pop_brd\"></td><td class=\"pop_br\"></td></tr></table>",
    "onOpening": "OnPopupOpening(this)", "onOpen": "OpenPopup(this)", "onClose": "ClosePopup(this)", "onClosing": "ClosingPopup(this)", "onPosition": "PositionPopup(this,p)",
    "loadingText": " ", "loadingClass": "loading", "class": "popup"
}

function OnPopupOpening(o) {
    if (PopupEvents.onOpening != null)
    { for (var i = 0; i < PopupEvents.onOpening.length; i++) { PopupEvents.onOpening[i](o); } }
    o._currentScroll = GetScrollXY();
    window.parent.document.getElementsByTagName("HTML")[0].style.overflow = "hidden";
}

function PositionPopup(o, p) {
    var ise = true;
    if (o._safe == undefined || !o._safe) { window.clearTimeout(o._safe); o._safe = null; ise = false; };
    var fullH = o._g("h") == "max" ? true : false;
    var fullW = o._g("w") == "max" ? true : false;
    var autoH = o._g("h") == "auto" ? true : false;
    var autoW = o._g("w") == "auto" ? true : false;
    var isPostback = (p) ? p : false;
    var clientSize = GetClientSize();
    var newSize;
    var newPosition;
    var scrollPos = GetScrollXY();
    var ibody = o.frame.contentWindow.document.getElementsByTagName("body")[0];
    var lNSW = o._g("w");
    var lNSH = o._g("h");

    o.frame.style.height = '1px';
    o.frame.style.width = '1px';

    var ll = GetClientFullSize(o.frame.contentWindow);

    if (lNSW == "auto") lNSW = ll[0]; if (lNSW == "max") lNSW = clientSize[0]; if (lNSH == "auto") lNSH = ll[1]; if (lNSH == "max") lNSH = clientSize[1];

    newSize = [lNSW, lNSH];
    newPosition = [(clientSize[0] - newSize[0]) / 2, (clientSize[1] - newSize[1] - 16) / 2];

    if (isNaN(newPosition[0])) newPosition[0] = 0;
    if (isNaN(newPosition[1])) newPosition[1] = 0;
    if (newPosition[0] < 0) newPosition[0] = 0;
    if (newPosition[1] < 0) newPosition[1] = 0;

    if (newSize[1] > (clientSize[1])) {
        newSize[1] = clientSize[1];

        newPosition[1] = 0;
        o.frame.scrolling = "auto";

    } else {
        try {
            o.frame.scrolling = "hidden";

        } catch (e) {

            o.frame.scrolling = "no";
        }
    }

    if (newSize[1] >= clientSize[1]) {
        newSize[1] = clientSize[1] - 16;
        newSize[0] += 16;
    }

    o.frame.style.left = "0px";
    o.frame.style.top = "0px";


    
    o.container.style.left = (parseInt(newPosition[0])) + 'px';
    o.container.style.top = (parseInt(newPosition[1])) + 'px';
    window.scroll(0, 0);

    if (isNaN(newSize[0])) newSize[0] = 0;
    if (isNaN(newSize[1])) newSize[1] = 0;
    
    o.frame.style.height = (parseInt(newSize[1])) + 'px';
    o.frame.style.width = (parseInt(newSize[0])) + 'px';

    if (PopupEvents.onPosition != null) { for (var i = 0; i < PopupEvents.onPosition.length; i++) { PopupEvents.onPosition[i](o, p); } }

}

function OpenPopup(o) { }

function ClosingPopup(o) {
    if (PopupEvents.onClosing != null)
    { for (var i = 0; i < PopupEvents.onClosing.length; i++) { PopupEvents.onClosing[i](o); } }
    return true;
}

function ClosePopup(o) {
    if (PopupEvents.onClose != null) {
        for (var i = 0; i < PopupEvents.onClose.length; i++) {

            PopupEvents.onClose[i](o);

        } 
    }
}

function GetClientFullSize(o) {
    var aW, aH;
    if (typeof (o.scrollWidth) == 'number') {
        aW = o.scrollWidth; aH = o.innerHeight;
    }
    else if (o.document.documentElement && (o.document.documentElement.scrollWidth || o.document.documentElement.scrollHeight)) {
        var lll = o.document.documentElement.offsetHeight;
        var lll1 = o.document.documentElement.offsetWidth;
        aW = o.document.documentElement.scrollWidth;
        aH = o.document.documentElement.scrollHeight;

    }
    else if (o.document.body && (o.document.body.scrollWidth || o.document.body.scrollHeight)) {
        aW = o.document.body.scrollWidth; aH = o.document.body.scrollHeight;
    }
    return [aW, aH];
}
