var FormBuilderLightbox = {
    options: {
        closeable: true
    },
    
    Hide: function() {
        //Reshow the hidden elements
        $$("body select, object, embed").each(function(obj) {
			obj.style.visibility = "";
		});
        
        //Do an after hide event if there is one
        if(this.options.beforeHide)
			this.options.beforeHide(this.options);
    
        //Hide the light box.
        $("FormBuilderLightBox").hide();
        $("lightbox_content").hide();
    },
    
    Show: function(options) {
        //Add the user defined options
        Object.extend(this.options, options);
        
        //Build the lightbox if needed
        if(!$("FormBuilderLightBox"))
            this.constructLightBox();
            
        //Do an before show event if there is one
        if(this.options.beforeShow)
			this.options.beforeShow(this.options);
            
		//Set close event
		if(this.options.closeable)
		    $("FormBuilderLightBox").onclick = this.Hide.bind(this);
		else
		    $("FormBuilderLightBox").onclick = null;
		    
        //Show the box
        $("FormBuilderLightBox").show();
        
        //Set the dimensions and position of the containers
        this.setPlains();
        
        //Hide certain elements
        $$("object, embed, select").invoke("hide");
        
        //Do an after show event if there is one
        if(this.options.afterShow)
			this.options.afterShow(this.options);
		
        //Show the light box content.
        $("lightbox_content").show();
    },
    
    constructLightBox: function() {
        //Build the lightbox overlay and the content containers
        var lightBox = new Element("div", { "id": "FormBuilderLightBox", "class": "lightbox", "style": "display: none; position: absolute; top: 0; left: 0; zIndex: 9000;" });
		var lightBoxContent = new Element("div", { "id": "lightbox_content", "style": "position: absolute; left: 0px; top: 0px; zIndex: 9001;" });
        lightBoxContent.insert(new Element("div", { "class": "lightbox-content" }));
	    
		//Add to the container or body.
		if(this.options.container) {
            this.options.container.insert(lightBox);
            this.options.container.insert(lightBoxContent);
		} else {
            document.body.appendChild(lightBox);
            document.body.appendChild(lightBoxContent);
        }
    },
    
    setPlains: function() {
        var lightBox = $("FormBuilderLightBox");
        var lightBoxContent = $("lightbox_content");
        var viewPort = document.viewport.getDimensions();
        var scrollOffsets = document.viewport.getScrollOffsets();
        var contentDimensions = lightBoxContent.getDimensions();
        
        lightBox.style.width = viewPort.width + "px";
        lightBox.style.height = viewPort.height + "px";
        lightBox.style.top = scrollOffsets.top + "px";
        lightBox.style.left = scrollOffsets.left + "px";
        
        lightBoxContent.style.left = (((viewPort.width / 2) - (contentDimensions.width / 2)) + scrollOffsets.left) + "px";
        lightBoxContent.style.top = (((viewPort.height / 2) - (contentDimensions.height / 2)) + scrollOffsets.top) + "px";
    }
};