/*
 * xsBox by Boris POPOFF
 * Version 0.2
 * To be used with mootools 1.2
 */

var xsBox = {
    init: function(){
        // Insertion du html nécessaire au bon fonctionnement
        new Element('div', {
            'id': 'xsBoxOverlay',
            'opacity': '0.35'
        }).inject(document.body);
        
        new Element('div', {
            'id': 'xsBoxLoading',
            'html': '<img id="xsBoxLoadingImage" src="loading.gif" />'
        }).inject(document.body);
        
        new Element('div', {
            'id': 'xsBoxContainer',
            'html': '<div id="xsBox"><div id="xsBoxHeader"></div><div id="xsBoxContent"></div><a href="javascript:void(0);" id="xsBoxClose">Close</a></div>'
        }).inject(document.body);
        
        $('xsBoxClose').addEvent('click', xsBox.hide);

    },
    
    request: function(options){
    
        var handlerFunc = function(responseTree, responseElements, responseHTML, responseJavaScript){
            // On set le html et le titre
            $('xsBoxHeader').set('html', options.title);
            $('xsBoxContent').set('html', responseHTML);
            
            if (options.width) {
                $('xsBox').setStyle('width', options.width + 'px');
            }
            
            if (options.height) {
                $('xsBoxContent').setStyle('height', options.height + 'px');
            }
            
            // On affiche
            $('xsBoxOverlay').setStyle('display', 'block');
            $('xsBoxLoadingImage').setStyle('display', 'none');
            $('xsBox').setStyle('display', 'block');
            
            xsBox.center();
            
            if (options.autoCenter) {
                xsBox.autoCenter();
            }
            
            if (responseJavaScript) {
                eval(responseJavaScript);
            };
            
            if (typeof finalFunc != 'undefined') 
                finalFunc();
        }
        
        if (!options.title) {
            options.title = '';
        };
        if (!options.width) {
            options.width = (window.getSize().x) / 2;
        };
        if (!options.autoCenter) {
            options.autoCenter = true
        };
        
        // On regarde si on a deja fait l'init ou pas
        if (!$('xsBox')) {
            xsBox.init()
        };
		
        // On feinte le onComplete	
        if (options.onComplete) {
			var finalFunc = options.onComplete;
		};
		
        options.onComplete = handlerFunc;
        
		// On affiche l'overlay
        $('xsBoxOverlay').setStyle('display', 'block');
        $('xsBoxLoadingImage').setStyle('display', 'block');
        
		// Requete AJAX
        var xsRequest = new Request.HTML(options).send();
    },
    
    center: function(){
        var marginY = ($('xsBoxContent').getSize().y + 22) / 2;
        var marginX = $('xsBoxContent').getSize().x / 2;
        $('xsBoxContainer').setStyle('margin', '-' + (marginY) + 'px 0pt 0pt -' + (marginX) + 'px');
        xsBox.height = $('xsBoxContent').getSize().y;
    },
    
    autoCenter: function(){
        if (xsBox.height != $('xsBoxContent').getSize().y) {
            xsBox.center();
        }
        xsBox.autoCenter.delay(50);
    },
    
    hide: function(){
        $('xsBoxOverlay').setStyle('display', 'none');
        $('xsBox').setStyle('display', 'none');
    }
};

// On ajoute l'evenement
window.addEvent('domready', xsBox.init);


/*

 window.addEvent('domready', function(){

 xsBox.request({

 'title':'kikoo',

 'width':800,

 'height':50,

 'url':'http://test.local/'

 });

 });

 */

