﻿function PopupClass(id, h) {
    this.id = id;
    this.properties = null;
    this.context = window;
    this.container = null;
    this.frame = null;
    this.loading = null;
    this.visible = false;
    this.sc = null;
    this.handler = h;
    this._isOpen = false;
    this.urlNotSetException = new Error("URL argument not set!");
    this.propertiesNotSetException = new Error("Properties argument not set!");
    this.tidNotSetException = new Error("Template control ID argument not set or invalid!");

    this._onContentReady = function() {
        if (this._is("onContentReady")) {
            if (!eval(this._g("onContentReady"))) return;
        }
        this.OpenDone();
    }

    this.dispose = (function() {
        this.id = null;
        this.properties = null;
        this.context = null;
        this.container = null;
        this.frame = null;
        this.loading = null;
        this.visible = false;
        this.sc = null;
        this.handler = null;
    }).bind(this);

    this._g = function(p, d) { return this.properties[p]; }
    this._s = function(p, v, d) { this.properties[p] = (v == null || v == "undefined" || v == '') ? d : v; }
    this._is = function(p, o) {

        if (!o) {

            o = this.properties;
        };

        if (o == null || o[p] == "undefined" || o[p] == null || o[p] == '') {

            return false;
        }

        return true;
    }

    this._r = function(p, d) { if (this._is(p)) return; if (this._is(p, this.handler.properties)) { this._s(p, this.handler.properties[p]); return; } this._s(p, d); }

    this.Show = function() { this.visible = true; if (this.container) this.container.style.display = "block"; this.Position(); }
    this.Hide = function() { this.visible = false; if (this.container) this.container.style.display = "none"; }

    this.Open = function(properties) {
        if (properties == null) throw this.propertiesNotSetException;
        this.properties = properties;
        if (!this._is("url")) throw this.urlNotSetException;

        this._r("url"); this._r("w", "auto"); this._r("h", "auto"); this._r("class");
        this._r("sc"); this._r("t"); this._r("tid"); this._r("loadingText", ""); this._r("onOpening");
        this._r("onOpen"); this._r("onPosition"); this._r("onClosing"); this._r("onClose"); this._r("loadingClass"); this._r("onContentReady"); this._r("onGetUrl");

        if (this._is("t")) { this._s("tid", this._g("tid") + "_" + id); this._s("t", this._g("t").replace("[CONTAINER]", this._g("tid"))); }

        this.container = document.createElement("div");
        document.body.appendChild(this.container);
        if (this._is("class")) this.container.className = this._g("class");

        this.frame = document.createElement("iframe");
        this.frame.allowTransparency = true;

        this.frame.frameBorder = 0;
        this.frame.style.border = 0;
        this.frame.style.padding = 0;
        this.frame.style.margin = 0;
        this.frame.name = "Zebra.Web.UI.Webcontrols.Popup";
        this.container.style.top = "-9999px";
        this.container.style.left = "-9999px";

        this.loading = document.createElement("div");
        if (this._is("loadingClass")) this.loading.className = this._g("loadingClass");
        this.loading.innerHTML = this.properties["loadingText"];

        var lH = this.container;
        if (this._is("t"))
        { this.container.innerHTML = this._g("t"); lH = document.getElementById(this._g("tid")); if (!lH) throw this.tidNotSetException; }

        lH.appendChild(this.frame);
        document.body.appendChild(this.loading);
        if (this._is("onGetUrl")) eval(this._g("onGetUrl"));
        this.frame.src = this._g("url");
        AddEvent(document, "keydown", Popup.cesc);
        this.visible = true;
        if (this._is("onOpening")) eval(this._g("onOpening"));
    }

    this._closeInternal = function(e, cn, ca, bg) {
        if (!e) e = false;
        if (!cn) cn = '';
        if (!ca) ca = '';
        try {
            if (e) {
                if (this._is("sc")) {
                    this.context.setTimeout("__doPostBack(\"" + this._g("sc") + "\",\"" + cn + "|" + ca + "|" + bg + "\");", 0);
                }
            }
        } finally {
            if (this.container) {
                this.container.style.display = "none";
                try { if (this.loading) document.body.removeChild(this.loading); } catch (e) { }
                document.body.removeChild(this.container);
            }
            this.container = null;
            this.loading = null;
            this.frame = null;
            RemoveEvent(document, "keydown", Popup.cesc);
            if (this._is("onClose")) eval(this._g("onClose"));
            this._isOpen = false;
            var p = this.handler.glwr("h");
            if (p) p.Show();
        }
    }
    this.Close = function(e, cn, ca, bg) {
        if (this._is("onClosing")) {
            if (!eval(this._g("onClosing"))) return;
        }
        this._closeInternal(e, cn, ca, bg);

    }

    this.OpenDone = function() {
        if (!this._isOpen) {
            try { if (this.loading) document.body.removeChild(this.loading); } catch (e) { };
            this.loading = null;
            this.frame.style.display = "block";
        }
        this._isOpen = true;
        this.Position();

        if (this._is("onOpen")) eval(this._g("onOpen"));
    }

    this.Position = function(p) {
        if (!this.container) return;

        if (this._is("onPosition")) {
            if (!eval(this._g("onPosition"))) return;
        }

        var fullH = this._g("h") == "max" ? true : false;
        var fullW = this._g("w") == "max" ? true : false;
        var autoH = this._g("h") == "auto" ? true : false;
        var autoW = this._g("w") == "auto" ? true : false;
        var isPostback = (p) ? p : false;
        var clientSize = GetClientSize();
        var newSize;
        var newPosition;
        var scrollPos = GetScrollXY();
        var ibody = this.frame.contentWindow.document.getElementsByTagName("body")[0];
        var lNSW = this._g("w");
        var lNSH = this._g("h");

        if (lNSW == "auto") lNSW = ibody.offsetWidth;
        if (lNSW == "max") lNSW = clientSize[0];
        if (lNSH == "auto") lNSH = ibody.scrollHeight;
        if (lNSH == "max") lNSH = clientSize[1];

        newSize = [lNSW, lNSH];
        newPosition = [(clientSize[0] - newSize[0]) / 2, (clientSize[1] - newSize[1]) / 2];

        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;
            this.frame.scrolling = "auto";
        } else {
            try {
                this.frame.scrolling = "hidden";
            } catch (e) {
                this.frame.scrolling = "no";
            }
        }

        this.container.style.left = (parseInt(newPosition[0] + scrollPos[0])) + 'px';
        this.container.style.top = (parseInt(newPosition[1] + scrollPos[1])) + 'px';
        this.frame.style.left = "0px";
        this.frame.style.top = "0px";
        this.container.style.width = (parseInt(newSize[0])) + 'px';
        this.container.style.height = (parseInt(newSize[1])) + 'px';
        this.frame.style.width = (parseInt(newSize[0])) + 'px';
        this.frame.style.height = (parseInt(newSize[1])) + 'px';
    }
}

function PopupHandlerClass() {
    this.id = null;
    this.window = null;
    this.properties = new Object();
    this.items = new Object();
    this.c = function(id) { return new PopupClass(id, this); }
    this.cesc = function(e) { CaptureEsc(e, "Popup.Close()"); }
    this.num = function() { var l = 0; for (var i in this.items) { if (this.items[i] && this.items[i].container) l++; } return l; }
    this.glwr = function(p) {
        var o = null;
        for (var i in this.items) {
            if (this.items[i] && this.items[i].Close) {
                switch (p) {
                    case "h": { if (this.items[i].visible == false && this.items[i].container) o = this.items[i]; break; }
                    case "v": { if (this.items[i].visible == true && this.items[i].container) o = this.items[i]; break; }
                    default: { o = this.items[i]; break; }
                }
            }
        }
        return o;
    }
    this.glwa = function() {
        for (var i in this.items) {
            if (this.items[i] && this.items[i].container) return this.items[i];
        }
        return null;
    }
    this.Open = function(id, properties) {
        this.CloseAll();
        var l = this.c(id);
        this.items[id] = null;
        this.items[id] = l;

        l.Open(properties);

    }

    this.swp = function(oid, nid) {
        this.items[nid] = Clone(this.items[oid], false);
        this.items[oid] = null;
    }

    this.is = function(id) {
        return (this.items[id] && this.items[id].container);
    }

    this.initClient = function(c, o, id, rid) {
        var id = id;

        if (rid && rid != id && rid != this.id) this.swp(rid, id);
        if (rid == this.id && !this.is(id)) {
            var lid = this.glwr("v").id;
            this.swp(lid, id);
        }
        o.id = id;

        AddEvent(c, "keydown", this.cesc);

    }

    this.dispose = (function() {
        for (var i in this.items) {
            if (this.items[i] && this.items[i].dispose) this.items[i].dispose();

        }

        this.id = null;
        this.window = null;
        this.properties = null;
        this.items = null;
    }).bind(this);

    this.Close = function(id) { var o = null; if (!id) { o = this.glwr("v"); } else { o = this.items[id]; } if (o) { o.Close(); } }
    this.CloseAll = function() { for (var i in this.items) { if (this.items[i] && this.items[i].Close) { this.items[i].Close(); } } }

}

var Popup = new PopupHandlerClass();

AddEvent(document.body, "unload", Popup.dispose);