
addListenner(window, "load", loadContentLightBox, true);

/**
 * Place l'ouverture du popup de l'image sur le clic
 */
function loadContentLightBox()
{
    var linklist = document.getElementsByTagName('a');
    for (var i = 0; i < linklist.length; i++) {
        if (/ajax-content-lightbox/.test(linklist[i].className)) {
            linklist[i].onclick = callAjaxContentLighBox;
            
        } else if (/content-lightbox/.test(linklist[i].className)) {
            linklist[i].onclick = showContentLighBox;
            
        }
    }

} // end function "loadContentLightBox"



/**
 * Affiche un contenu html présent dans la page dans une lightbox
 * 
 * @returns {Boolean}
 */
function showContentLighBox()
{
    var lightBox = document.createElement('div');
    lightBox.innerHTML = '';
    
    var idContent = this.className.split('content-lightbox');

    if (idContent[1]) {
        idContent = trim(idContent[1]);
        if (idContent.indexOf(' ') >= 0) {
            idContent = idContent.split(' ');
            if (idContent[0]) {
                idContent = idContent[0];
            }
        }
    }
    
    //alert('idContent = "' + idContent + '"');

    if (idContent) {
        var contentHtml = document.getElementById(idContent);
        if (contentHtml) {
            lightBox.innerHTML = contentHtml.innerHTML;
        }
    }
    
    var options = {content: lightBox, width: '200px', height: 'auto'};
    var lightBox = createLightBoxHtml('spg_lightBoxBackground',
                                      'spg_lightBox',
                                      options);

    // Centre la boîte et agrandi le fond noir à la hauteur de la page
    adaptLightboxPosAndSize(lightBox);
    
    return false;
} // end function "showContentLighBox"










/**
 * Appel de l'url du lien en ajax pour afficher son contenu dans la lightbox
 * 
 * Il est possible de placer des paramètres dans la classe du lien ainsi :
 * <a href="url" class="ajax-content-lightbox  fn:loadMailActions,width:750,height:400">lien</a>
 * 
 * @returns {Boolean}
 */
function callAjaxContentLighBox()
{
    var oData = {};
    oData['xhrAction'] = 'getLightBoxContent';
    oData['no-gab'] = 1;
    var lightBoxId = 'spg_lightBox';
    var lightBoxBgId = 'spg_lightBoxBackground';
    var linkClass = 'ajax-content-lightbox';
    var args = {};
    
    //alert('test');
    
    if (this.className && this.className.indexOf(linkClass) >= 0) {
        args = getArgsFromClass(this.className, linkClass);
        //alert('args = ' + args);
    }
    var that = this;
    
    that.close = closeLightBox;
    //alert('test');
    
    //XHR_post(this.href, oData, showAjaxContentLighBox, false, true);
    XHR_get(this.href, oData, showAjaxContentLighBox, false, true);
    
    return false;
    
    
    
    
    
    /**
     * Reçois le code html à afficher et le place dans la lightbox
     * 
     * @param htmlContent
     * @returns {Boolean}
     */
    function showAjaxContentLighBox(htmlContent)
    {
        // Le contenu peut être html (page à afficher)
        // ou "données" à transmettre à une fonction
        var header = htmlContent.substring(0, 5);
        
        //alert('"' + header + '"');
        
        that.saveSuccess = false;
        if (header == '#JSON') {
            that.saveSuccess = true;
            var data = XHR_parseResponse(htmlContent.substring(5));
            if (args['callOnSuccess']) {
                eval(args['callOnSuccess'] + '(data, that)');
            }
            //closeLightBox();
            return false;
        }
        
        
        // place le contenu dans la lightbox
        var boxContent = document.createElement('div');
        boxContent.className = 'ajaxBoxWithScroll';
        htmlContent = htmlContent.replace('<?xml version="1.0" encoding="iso-8859-1"?>', '');
        htmlContent = htmlContent.replace('<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', '');
        boxContent.innerHTML = htmlContent;
        
        // dimensions
        var lbWidth = '750px';
        var lbHeight = '400px';
        var scrollWidth = '750px';
        var scrollHeight = '400px';
        
        if (typeof document.body.style.maxHeight == "undefined") {
            var scrollWidth = '725px';
        }
        
        //alert('args[width] = ' + args['width']);
        
        
        if (args['width']) {
            lbWidth = args['width'] + 'px';
            scrollWidth = (args['width'] - 15) + 'px';
        }
        if (args['height']) {
            lbHeight = args['height'] + 'px';
            scrollHeight = (args['height'] - 15) + 'px';
        }
        
        boxContent.style.width  = scrollWidth;
        boxContent.style.height = scrollHeight;
        

        // Créé la lightbox ou remplace son contenu
        var lightBox = document.getElementById(lightBoxId);
        if (!lightBox) {
            var options = {content: boxContent, width: lbWidth, height: lbHeight};
            var lightBox = createLightBoxHtml(lightBoxBgId,
                                              lightBoxId,
                                              options);
        
            // Centre la boîte et agrandi le fond noir à la hauteur de la page
            adaptLightboxPosAndSize(lightBox);
            
        } else {
            lightBox.replaceChild(boxContent, lightBox.firstChild);
        }

        
        // place les évènements de la lightbox (liens, bouton submit)
        loadFormActions();  
        if (args['fn']) {
            window[args['fn']](lightBoxId, that.href);
        }
        
        
        return false;
    }; // end function "showAjaxContentLighBox"
    
    
    
    
    
    /**
     * Place les actions "soumission de formulaire", "lien" et "fermeture lightbox"
     * 
     */
    function loadFormActions()
    {
        var lightBox = document.getElementById(lightBoxId);
        lightBox = lightBox.firstChild;
        var linklist = lightBox.getElementsByTagName('a');
        var input2 = false;
        for (var i = 0; i < linklist.length; i++) {
            if (linklist[i].className.indexOf('fermer-lightbox') >= 0) {
                linklist[i].onclick = closeLightBox;
                
            } else if (linklist[i].href.indexOf('http://') < 0) {
                linklist[i].onclick = callAjaxContentLighBox;
            }
        }

        // Parcours les boutons de type "submit"
        linklist = lightBox.getElementsByTagName('input');
        for (i = 0; i < linklist.length; i++) {
            if (linklist[i].type && (linklist[i].type == 'submit' || linklist[i].type == 'button' || linklist[i].type == 'image')) {

                // IE semble merder sur l'accès à type, même après test qu'il existe...
                try {
                    // indique que le bouton est traité et désactive le comportement par défaut
                    if (linklist[i].type && linklist[i].type != 'image') {
                        linklist[i].type = 'button';
                    }
                    
                } catch (e) {
                    input2 = document.createElement('input');
                    if (linklist[i].id) {
                        input2.id = linklist[i].id;
                    }
                    if (linklist[i].name) {
                        input2.name = linklist[i].name;
                    }
                    if (linklist[i].className) {
                        input2.className = linklist[i].className;
                    }
                    if (linklist[i].value) {
                        input2.value = linklist[i].value;
                    }
                    input2.type = 'button';
                    
                    linklist[i].parentNode.replaceChild(input2, linklist[i]);
                }
                
                
                linklist[i].onclick = function () { submitForm(this); return false; };
            }
        }
        
       
        if (typeof installGeoPopup == 'function') {
            installGeoPopup();
        }
        if (typeof addCalendarEventListener == 'function') {
            addCalendarEventListener(lightBox);
        }
        if (typeof loadSubFormActions == 'function') {
            loadSubFormActions(lightBox);
        }
        if (typeof loadAgendaDesCours == 'function') {
            loadAgendaDesCours(lightBox);
        }
        if (typeof spg_menuRollover == 'function') {
            spg_menuRollover(lightBox);
        }
        if (typeof lili_loadRechercheDates == 'function') {
            lili_loadRechercheDates();
        }
        if (typeof lili_loadAjoutResa == 'function') {
        	lili_loadAjoutResa(that.href);
        }
        if (typeof addSearchListEventListener == 'function') {
        	addSearchListEventListener(that.href);
        }
        if (typeof lili_loadDebitCredits == 'function') {
            lili_loadDebitCredits(that.href);
        }
        

    } // end function "loadFormActions"
    
    
    
    
    /**
     * Fermeture de la lightbox
     * 
     * @returns {Boolean}
     */
     function closeLightBox() {
         var bg = document.getElementById(lightBoxBgId);
         bg.parentNode.removeChild(bg);
         bg = document.getElementById(lightBoxId);
         bg.parentNode.removeChild(bg);
         return false;
     }; // end function "closeLightBox"
    
    
     
    /**
     * Simule la soumission d'un formulaire
     * 
     * Note: ne supporte pas les champs "file"
     * 
     * Note: lit l'attribut "action", les données sont envoyées cette page.
     * 
     * @returns {Boolean}
     */
    function submitForm(button)
    {
        var lightBox = document.getElementById(lightBoxId);
        var url = that.href;
        var form = button.parentNode;
        while (form.parentNode && form.nodeName != 'FORM') {
            form = form.parentNode;
        }
        if (form.nodeName != 'FORM') {
            //alert('formulaire non trouvé');
            form = lightBox;
            
        } else if (form.action) {
            //alert('form trouvé, url: "' + form.action + '"');
            url = form.action;
            
        } /*else {
            alert('form trouvé');
        }*/
        
        var oData = readForm(form);
        var toString = '';
        for (i in oData) {
            toString += i + ':' + oData[i] + ', ';
        }
        //alert('données : ' + toString);
        
        oData['xhrData'] = 'on';
        oData['no-gab'] = 1;
        
        if (button.id) {
            oData['form-target'] = button.id;
        } else if (button.name) {
            oData['form-target'] = button.name;            
        }
    
        
        
        
        
        
        
        var fnCount = 0;
        var fnOnSuccess = [];
        var fnOnSuccessLabels = [];
        fnOnSuccess[fnCount] = showAjaxContentLighBox;
        fnOnSuccessLabels[fnCount] = 'showAjaxContentLighBox';
        fnCount++;
        
        /*if (args['autoclose']) {
            fnOnSuccess[fnCount] = closeLightBox;
            fnOnSuccessLabels[fnCount] = 'closeLightBox';
            fnCount++;
        }
        if (args['refreshMain']) {
            fnOnSuccess[fnCount] = function () {
                if (args['refreshMain'] == 1) {
                    window.location.reload(true);
                    return false;
                    
                } else {
                    var url = window.location.href;
                    if (url.indexOf('#') >= 0) {
                        url = url.substr(0, url.indexOf('#'));
                    }
                    url += '#' + args['refreshMain'];
                    window.location.href = url;
                    window.location.reload(true);
                    return false;
                }
            };
            fnOnSuccessLabels[fnCount] = 'refreshMain';
            fnCount++;
        }*/
        
        XHR_post(url, oData, function(htmlContent)
                            { 
                                //alert('reception des données ajax');
                                if (fnOnSuccess.length) {
                                    //alert('appel des fonctions...');
                                    for (var fn = 0; fn < fnOnSuccess.length; fn++) {
                                        //alert('appel fonction ' + fn + ' "' + fnOnSuccessLabels[fn] + '"');
                                        if (fnOnSuccess[fn] && typeof fnOnSuccess[fn] == 'function') {
                                            fnOnSuccess[fn](htmlContent);
                                        }
                                    }
                                }
                                //fnOnSuccess(htmlContent);
                                if (button.onReady && that.saveSuccess) {
                                    //alert('"' + button.id + '": Lancement onReady()');
                                    button.onReady(that.saveSuccess);
                                    button.onReady = null;
                                }
                                
                                
                                
                                if (that.saveSuccess) {
                                    
                                    var data = XHR_parseResponse(htmlContent.substring(5));
                                    if (data['data']['message']) {
                                        alert(data['data']['message']);
                                    }
                                    
                                    if (args['autoclose']) {
                                        closeLightBox();
                                    }
                                    if (args['refreshMain']) {
                                        if (args['refreshMain'] == 1) {
                                            window.location.reload(true);
                                            return false;
                                            
                                        } else {
                                            var url = window.location.href;
                                            if (url.indexOf('#') >= 0) {
                                                url = url.substr(0, url.indexOf('#'));
                                            }
                                            url += '#' + args['refreshMain'];
                                            window.location.href = url;
                                            window.location.reload(true);
                                            return false;
                                        }
                                    }
                                }
                                
                                /*if (that.saveSuccess) {
                                    alert('"' + button.id + '": enregistrement sans erreur');
                                } else {
                                    alert('"' + button.id + '": un problème est survenu');
                                }*/
                            }, false, true);
        
        
        
        
        
        
        
        /*var fnOnSuccess = showAjaxContentLighBox;
        if (args['autoclose']) {
            fnOnSuccess = closeLightBox;
        }
        if (args['refreshMain']) {
            //alert('typeof : ' + typeof(args['refreshMain']));
            //var urlRefresh = args['refreshMain'];
            
            fnOnSuccess = function () {
                if (args['refreshMain'] == 1) {
                    window.location.reload(true);
                    return false;
                    
                } else {
                    var url = window.location.href;
                    if (url.indexOf('#') >= 0) {
                        url = url.substr(0, url.indexOf('#'));
                    }
                    url += '#' + args['refreshMain'];
                    window.location.href = url;
                    //document.location.href = url;
                    //alert('url = "' + url + '"');
                    window.location.reload(true);
                    return false;
                }
                //closeLightBox();
                //return true; 
            };
        }
                
        
        XHR_post(url, oData, fnOnSuccess, false, true);*/
        return false;
    } // end function "submitForm"


    


} // end function "callAjaxContentLighBox"




function refreshMainCredits ()
{
    alert('appel de refreshMainCredits()');
}









