if (!window.Baobaz) {
    window.Baobaz = {};
}

document.observe('dom:loaded', function() {
    if(!$('modal-loading')) {
        $(document.body).insert({
            top: new Element('div', {
                id: 'modal-loading'
            }).hide()
        });    
    } 

})

Baobaz.Modal = {
    init: function(link, options) {   
        //$('modal-loading').setStyle({top: ((document.viewport.getHeight()-$('modal-loading').getHeight())/2) + 'px'});
        this.modalClose = new Element('div').addClassName('modal-close'); 
        this.modalContent = new Element('div').addClassName('modal-content'); 
        this.modalOptions = {
            beforeOpen: function() {$('modal-loading').addClassName("loader-changer").setStyle({top: ((document.viewport.getHeight()-$('modal-loading').getHeight())/2) + 'px'});},
            overlayOpacity: 0.75,
            fade: true,
            indicator: $('modal-loading'),
            closeOnClick: this.modalClose,
            insertRemoteContentAt: this.modalContent            
        };    
		options = options || {};
		Object.extend(this.modalOptions, options); 
        this.modalOptions.className = 'modal-window ' + (this.modalOptions.className ? this.modalOptions.className : '');
        if (this.modalOptions.afterClose == 'remove'){
        	this.modalOptions.afterClose = function(){
        		this.container.remove();
        		$('modal-loading').removeClassName("loader-changer");
        	};
        } else {
        	this.modalOptions.afterClose = function(){$('modal-loading').removeClassName("loader-changer");};
        }
        var modal = new Control.Modal(link, this.modalOptions);
        modal.container.insert(this.modalClose);
        modal.container.insert(this.modalContent);          
        return modal;        
    },
    
    open: function(link, options) {
        var modal = this.init(link, options);  
        modal.open();
        return modal;
    }
};
