function Popup(width, height, domObject) {
    var base = this;
    
    this.width = width;
    this.height = height;
    this.domObject = domObject;

    this.showPopup = function() {
        var verticalPos = $(document).scrollTop();
        this.domObject
        .css({ "display": "block", "top": verticalPos })
        .animate({ "height": this.height }, 400,
            function() {
                base.domObject.animate({ "width": base.width }, 800, function() { base.domObject.find(".content").fadeIn(); });
            });
    };

    this.closePopup = function() {
        this.domObject.hide().css({ "width": "0", "height": "0" }).find(".content").hide();
    };
}

var popup = (function() {
    var popups = new Array();
    return {
        init: function() {
            $("div.popup").each(function() {

                var popupObject = $(this);
                var width = popupObject.width();
                var height = popupObject.height();
                popupObject.css({ "display": "none", "visibility": "visible" }).find(".content").css({ "display": "none", "visibility": "visible" });
                var popup = new Popup(popupObject.width(), height, popupObject);
                popup.domObject.css({ "width": "0", "height": "0" });
                popups.push(popup);
            })
            .find("a.close").click(function(e) {
                popups[popup.currentIndex].closePopup();
                popup.overlay.css("display", "none");
                e.preventDefault();
            });

            $("a.popupLink").click(function(e) {
                var pageHeight = $(document).height();
                var pageWidth = $(document).width();

                popup.overlay.css({ "display": "block", "width": pageWidth, "height": pageHeight });

                var id = $(this).attr("id");
                var index = parseInt(id.replace("no", "")) - 1;
                popup.currentIndex = index;
                popups[index].showPopup();
                e.preventDefault();
            });
        } (),
        currentIndex: 0,
        overlay: function() {
            return $("div.overlay").click(function(e) {
                popups[popup.currentIndex].closePopup();
                popup.overlay.css("display", "none");
            });
        } ()
    }
})();
