﻿jQuery.noConflict();
function PopupClass(id, h) {
    this.id = id;
    this.properties = null;
    this.context = window;
    this.container = null;
    this.containerBase = null;
    this.frame = null;
    this.visible = false;
    this.sc = null;
    this.handler = h;
    this.isPostback = false;
    this._isOpen = false;
    this.urlNotSetException = new Error("URL argument not set!");
    this.propertiesNotSetException = new Error("Properties argument not set!");
    this._currentScroll = null;
    this._mainform = null;
    this._mainhtml = null;

    
     this.dispose = function() {
        this.id = null;
        this.properties = null;
        this.context = null;
        this.container = null;
        this.frame = null;
        this.visible = false;
        this.sc = null;
        this.handler = null;

    };

    this.Show = function() { this.visible = true; if (this.container) this.container.show(); this.Position(); }
    this.Hide = function() { this.visible = false; if (this.container) this.container.hide(); }

    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.Open = function(properties) {

        if (properties == null) throw this.propertiesNotSetException;

        this.properties = properties;

        if (!this._is("url")) throw this.urlNotSetException;

        this.isPostback = false;

        this._r("url"); this._r("w", "auto"); this._r("h", "auto"); this._r("class");
        this._r("sc"); this._r("t");

        if (!Popup.glwr("v")) {
            jQuery(Popup).trigger("openFirst");
            Popup.CreateMask();
        };

        jQuery('html,body').css({ 'overflow': 'hidden', 'position': 'absolute', 'width': '100%' });

        _mainhtml = jQuery('html');
        _mainform = jQuery('form');
        
        if (!Popup.isIE6)
            _mainform.css({ width: jQuery(window).width() + "px", height: jQuery(window).height() + "px", 'overflow': 'hidden', 'position': 'absolute' });

        //console.log(navigator.userAgent);
        var lClass = "popup";
        this.container = jQuery("<div class=\"" + lClass + "\" id=\"dv_" + this.id + "\" style='position:relative!important;z-index:1000;' ></div>");
        this.containerBase = jQuery("<div style='height:100%;position:absolute;top:0;width:100%;z-index:1000;' />"); // overflow:scroll;
        this.containerBase.append(this.container);
        jQuery("body").append(this.containerBase);

        this.frame = jQuery("<iframe allowTransparency=true id=\"if_" + this.id + "\" name=\"ifn_" + this.id + "\" frameBorder=0></iframe>");

        this.container.append(this.frame);

        this.frame.css({ "border": "0", "padding": "0", "margin": "0", "overflow": "visible" });
        this.container.css({ "top": "-9999px", "left": "-9999px" });

        var that = this;

        this.frame.bind("load.popup", function() {
            that.loaded(); 
        });

        this.frame.attr("src", this._g("url"));
        this.visible = true;
        this.containerBase.focus();
    }

    this.loaded = function() {
        this._isOpen = true;

        if (!Popup.isIE6)
            jQuery('html,body').css({ 'overflow': '', 'overflow-x': '', 'overflow-y': '', 'position': '', 'width': '' });

        if (this.container.position().left >= 0) {
            this.Position();
        } else {
            this.setSize();
            this.container.hide();
            this.setPosition();
            this.container.fadeIn("fast");
        };
        jQuery(this.frame[0].contentWindow.document).bind("keyup.popup", function(e) {
            if (e.which != 27) return;
            this.Close();
        });
    }

    this.Close = function(e, cn, ca, bg) {

        if (!e) e = false;
        if (!cn) cn = '';
        if (!ca) ca = '';

        try {

            if (e) {
                if (this._is("sc")) {
                    this.isPostback = true;
                    this.context.setTimeout("__doPostBack(\"" + this._g("sc") + "\",\"" + cn + "|" + ca + "|" + bg + "\");", 0);

                } else {
                    this.isPostback = false;
                }
            }
        } finally {

            // if (this.isPostback) return;

            //if (this.container) {
            //     this.container.hide();
            //     this.container.remove();

            // }

            //  this.container = null;

            // this.frame = null;

            this._isOpen = false;


            for (var i = 0; i < Popup.items.length; i++) {
                var l = Popup.items[i];
                if (!l) continue;
                if (l.container) {
                    l.container.hide();
                    l.container.remove();
                    l.container = null;
                }
                if (l.containerBase) {
                    l.containerBase.hide();
                    l.containerBase.remove();
                    l.containerBase = null;
                }
                if (!Popup.isIE6)
                    _mainform.css({ 'overflow': '', 'position': '', 'height': '', 'width': '' });
                    
                jQuery('html,body').css({ 'overflow': '', 'overflow-x': '', 'overflow-y': '', 'position': '', 'width': '' });
            }
            Popup.DestroyMask();
            Popup.items = [];
            jQuery(Popup).trigger("closeLast");
        }
        jQuery(window).focus();
    }

    this.setPosition = function() {
        var x = { x: (jQuery(window).height() - this.container.outerHeight()) / 2, y: (jQuery(window).width() - this.container.outerWidth()) / 2 };
        var cntr = Popup.isIE6 ? this.containerBase : _mainhtml;
        if (true) {
            if (x.y > 0)
                cntr.css({ "overflow-x": "hidden" });
            else
                cntr.css({ "overflow-x": "scroll" });
            if (x.x > 0)
                cntr.css({ "overflow-y": "hidden" });
            else
                cntr.css({ "overflow-y": "scroll" });
        }

        if (Popup.setPosition) Popup.setPosition(x);

        if (x.x < 0) x.x = 0;
        if (x.y < 0) x.y = 0;
        this.container.css("top", x.x + "px");
        this.container.css("left", x.y + "px");
    };

    this.setSize = function() {
        try {
            var lBody = this.frame.contents().find("form");
            var x = { width: lBody.outerWidth(), height: lBody.outerHeight() };

            //if (x.height > jQuery(window).height()) x.height = jQuery(window).height();
            this.frame.css({ width: x.width + "px", height: x.height + "px" });
            this.container.css({ width: x.width + "px", height: x.height + "px" });

            if (!Popup.isIE6) {
                if (jQuery(window).width() > x.width)
                    x.width = jQuery(window).width();
                if (jQuery(window).height() > x.height)
                    x.height = jQuery(window).height();
                _mainform.css({ width: x.width + "px", height: x.height + "px" });
            }
        }
        catch (ex) { };
    };

    this.Position = function() {
        this.setSize();
        this.setPosition();
    }

    this.PositionAnimate = function() {
        var lBody = this.frame.contents().find("form");
        var x = {
        x: (jQuery(window).height() - lBody.outerHeight()) / 2 + jQuery(window).scrollTop(),
            y: (jQuery(window).width() - lBody.outerWidth()) / 2 + jQuery(window).scrollLeft(),
            width: lBody.outerWidth(),
            height: lBody.outerHeight()
        };
        if (x.x < 0) x.x = 0;
        if (x.height > jQuery(window).height()) x.height = jQuery(window).height();
        this.frame.css({ width: x.width + "px", height: x.height + "px" });
        this.container.animate({ top: x.x + "px", left: x.y + "px", width: x.width + "px", height: x.height + "px" }, "fast");
    }
}

function PopupHandlerClass() {
    this.id = null;
    this.window = null;
    this.properties = new Object();
    this.items = [];
    this.c = function(id) { return new PopupClass(id, this); }
           
    var mask = null;
    var that = this;

    this.setPosition = null;
    this.setMask = null;

    function createMask() {
        if (mask) return;
        
        var lClass = "popupMask positionFixed";
        mask = jQuery("<div class=\"" + lClass + "\">&nbsp;</div>");

        if (Popup.setMask) Popup.setMask(mask);
        jQuery("body").append(mask);
        
        mask.show();
    };

    this.getMask = function() {
        return mask;
    };

    function destroyMask() {
        if (!mask) return;
        mask.remove();
        mask = null;
    };

    this.DestroyMask = function() {
       destroyMask();
    };

    this.CreateMask = function() {
        createMask();
    };
    
    
    this.glwr = function(p) {
        var o = null;
        for (var i=0;i<this.items.length;i++) {
            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 = 0; i < this.items.length; i++) {
            if (this.items[i] && this.items[i].container) return this.items[i];
        }
        return null;
    }
    this.Open = function(id, properties) {
        this.HideAll();
        
        var l = this.c(id);
        this.items[this.items.length] = l;
        l.Open(properties);


    }

    this.swp = function(oid, nid) {
        var lClient = this.getClient(oid);
        if (lClient) lClient.id = nid;
    }

    this.is = function(id) {
        var lClient = this.getClient(id);
        return (lClient && lClient.container);
    }

    this.getClient = function(id) {
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].id.toLowerCase() == id.toLowerCase()) return this.items[i];
        }

        return null;
    }

    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;

        jQuery(c).find("body").bind("keyup.popup", function(e) {
            if (e.which != 27) return;
            that.Close();
        });
    }

    this.dispose = function() {
        jQuery(document).unbind(".popup");
        for (var i=0;i<this.items.length;i++) {
            if (this.items[i] && this.items[i].dispose) this.items[i].dispose();

        }
        this.id = null;
        this.window = null;
        this.properties = null;
        this.items = null;
        mask = null;
    };

    this.Close = function(id) {
        var o = null;
        if (!id) {
            o = this.glwr("v");
        } else {
            o = this.getClient(id);
        };
        if (o) {
            o.Close();
        };

    };

    this.CloseAll = function() {
       for (var i=0;i<this.items.length;i++) {
           if (this.items[i] && this.items[i].Close) { this.items[i].Close(); }
        };
    };

    this.HideAll = function() {
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i] && this.items[i].Hide) { this.items[i].Hide(); }
        };
    };

    jQuery(window).bind("resize.popup", function() {
        var o = that.glwr("v");
        if (o) o.Position();
    });

    jQuery(window).unload(function() {
        that.dispose();
    });

    jQuery(document).bind("keyup.popup", function(e) {
        if (e.which != 27) return;
        that.Close();
    });
}

var Popup = new PopupHandlerClass();

jQuery(Popup).bind("openFirst", function() {
    this._currentScroll = GetScrollXY();

    jQuery('html, body').animate({
        scrollTop: 0
    }, 200);

    jQuery("embed").hide();
});

jQuery(Popup).bind("closeLast", function() {

    if (this._currentScroll[1] == 0) {
        jQuery('html, body').animate({
            scrollTop: 1
        }, 1);
        jQuery('html, body').animate({
            scrollTop: 0
        }, 1);
    }
    else {
        jQuery('html, body').animate({
            scrollTop: this._currentScroll[1]
        }, 200);
    }

    jQuery("embed").show();
});