var ModalWindow = Class.create({
    initialize: function (settingsObj) {
        this.title = "";
        this.content = "";
        this.status = "begin";
        this.outer_id = "obj_modal_outer";
        this.background_id = "obj_modal_background";
        this.content_position_id = "obj_modal_content_positioning";
        if (settingsObj.title) this.title = settingsObj.title;
        if (settingsObj.content) this.content = settingsObj.content;
        if (settingsObj.closeAfter) this.closeAfter = settingsObj.closeAfter;
        if (settingsObj.position) this.position = settingsObj.position;
        if (isRunningIE6OrBelow) {
            this.position = "absolute";
        }
        if (this.content != '') {
            this.open();
        }
        if (settingsObj.scrollTo) Effect.ScrollTo(this.content_position_id);
        if (this.closeAfter) {
            setTimeout(this.close.bind(this), this.closeAfter);
        }
    },
    open: function (modal_content) {
        if (!this.isOpen()) {

            if (modal_content == undefined) modal_content = this.content;
            var backDiv = document.createElement('div');
            backDiv.id = this.outer_id;
            backDiv.innerHTML = "<div id='" + this.background_id + "'> &nbsp; </div>";
            backDiv.innerHTML += "<div id='" + this.content_position_id + "'> " + modal_content + " </div>";
            document.body.appendChild(backDiv);
            if (this.position) $(this.content_position_id).style.position = this.position;
            
            // show the background 
            jQuery( '#' + this.background_id )
                .show()
                .animate({ opacity: 0.7 }, { duration: 500, queue: false });

            // show the content
            jQuery( '#' + this.content_position_id )
                .animate({ opacity: 1 }, { duration: 500, queue: false })
                .slideDown();
                
            this.status = "open";
            if ((this.title != '') && document.title) {
                this.sites_old_title = document.title;
                document.title = this.title;
            }
            if (isRunningIE6OrBelow) {
                $$('select').each(function (el) {
                    el.style.display = "none";
                })
            }
        }
    },
    close: function () {
        if (this.isOpen()) {
            
            // hide the background 
            jQuery( '#' + this.background_id )
                .animate({ opacity: 0 }, { duration: 500 })
                .hide();

            // hide the content
            jQuery( '#' + this.content_position_id )
                .animate({ opacity: 0 }, { duration: 500, queue: false })
                .slideUp()
                .hide({ queue: true });

            //setTimeout(this.removeOuterElement.bind(this), 600);

            this.status = "closed";
            if ((this.title != '') && document.title) document.title = this.sites_old_title
        }
        setTimeout(function () {
            if (isRunningIE6OrBelow) {
                $$('select').each(function (el) {
                    el.style.display = "inline"
                })
            }
        }, 600)
    },
    removeOuterElement: function () {
        document.body.removeChild(document.getElementById(this.outer_id))
    },
    isOpen: function () {
        if (this.status == 'open') return true;
        else return false
    }
});
