
(function ($) {
    
    var bg;

    function getBG() {        
        if (!bg) {
            bg = $("<div></div>").addClass("popupBG");
            $("body").append(bg);
            MVD.Popup.prototype.bg = bg;            
        }
        return bg;
    }

    MVD.Popup = function (content, onOpen) {    
        this.cont = content; 
        this.onOpen = onOpen;
        this._init();
    }
        
    $.extend(MVD.Popup.prototype, {
        view : false,        
    
        open : function () {
            // if (typeof onClose != 'undefined') {
                // alert("Estan llamando MVD.Popup.open con funcion");
            // }
        
            if (!this.view) {
                getBG().css("opacity", "0.7").fadeIn("slow");
                this.view = true;          
                this.center();                
                if (this.onOpen) { 
                    this.onOpen(); 
                }                
                this.cont.slideDown("slow");
                $(window).bind("scroll resize", this.recenter);
            }
        },
        
        showText: function (msg) {            
            $('.text', this.cont).html(msg);        
            this.open();
        },
        
        close : function () {
            if (this.view) {
                this.view = false;
                getBG().unbind("click").fadeOut("slow");
                this.cont.fadeOut("slow");
                if (this.onClose) {
                    this.onClose();
                    // delete this.onClose;
                }
                $(window).unbind("scroll resize", this.recenter);
            }
        },
        
        center : function () {
            var w = $(window);
            getBG().css({"height": $(document).height(), 
                         "width": w.width()});
                                     
            var sx = w.scrollLeft();
            var sy = w.scrollTop();
                                 
            var winH = w.height();  
            var winW = w.width();
            var pHeight = this.cont.height();
            var pWidth = this.cont.width();
            
            this.cont.css({
                "position": "absolute",
                "top": sy + (winH - pHeight)/2,
                "left": sx + (winW - pWidth)/2
            });
        },
        
        _init : function () {            
            var that = this;    
            var closefn = function () { that.close(); return false; };            
            $(document).keypress(function(e) {
                if (!e) { e = event };
                if(e.keyCode == 27) {
                    closefn();
                }
            });
            getBG().click(closefn);
            $('.btnClose', this.cont).click(closefn);
            this.recenter = function () { that.center(); };            
        }
        
    });
    
    $(function () {
        MVD.errorPopup = new MVD.Popup($("#popupError"));
    });

}) (jQuery);