
addListenner(window, "load", installPicturePopup, true);

/**
 * Place l'ouverture du popup de l'image sur le clic
 */
function installPicturePopup()
{
    var linklist = document.getElementsByTagName('a');
    for (var i = 0; i < linklist.length; i++) {
        if (/popup-lightbox/.test(linklist[i].className)) {
            linklist[i].onclick = openLightBoxPopup;
            
        } else if (/popup/.test(linklist[i].className)) {
            linklist[i].onclick = openPicturePopup;
        }
    }

} // end function "installImagePopup"




function openLightBoxPopup()
{
    window.preloadLightBoxImg = new Image();
    window.preloadLightBoxImg.onload = showLighBox;
    window.preloadLightBoxImg.src = this.href;
    if (this.title) {
        window.preloadLightBoxImg.alt = this.title;
    }
    return false;
}


function showLighBox()
{
    //var zoomWin = document.getElementById('spg_lightBoxBackground');
    if (window.preloadLightBoxImg && !window.preloadLightBoxImg.complete) {
        setTimeout('showLighBox()', 50);
        return false;
    }
    
    
    var lightBoxImg = document.createElement('img');
    lightBoxImg.src = window.preloadLightBoxImg.src;
    if (window.preloadLightBoxImg.alt) {
        lightBoxImg.alt = window.preloadLightBoxImg.alt;
    }
    
    
    var options = {content: lightBoxImg, width: 'auto', 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);  
}




/**
 * Créé le popup du zoom de l'image
 */
function openPicturePopup ()
{
    var image = this.childNodes;
    for (var i = 0; i < image.length; i++) {
        if (image[i].nodeName == 'IMG') {
            var popup = window.open('', 'zoomillustration', 'height=300, width=280, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes, status=no');
            popup.document.write(getPopupWindow(this.href, this.title));
            popup.document.close();
        }
    }

    return false;

} // end function "openPicturePopup"




function getPopupWindow (url, title)
{
    var popupContent = '';
    popupContent += '<html>\r\n';
    popupContent += '    <head>\r\n';
    popupContent += '        <title>' + title + '</title>\r\n';
    popupContent += '    </head>\r\n';
    popupContent += '    <body style="background : #ffffff;">\r\n';
    popupContent += '        <img src="' + url + '" alt="' + title + '"/>\r\n';
    popupContent += '    <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">\r\n';
    popupContent += '    <!--\r\n';
    popupContent += '    var images = document.getElementsByTagName(\'img\');\r\n';

    popupContent += '    images[0].onload = function () {\r\n';
    popupContent += '    	if (this.width > 50 && this.height > 100) {\r\n';
    popupContent += '    		window.resizeTo(this.width + 40, this.height + 70);\r\n';
    popupContent += '    	}\r\n';
    popupContent += '    };\r\n';

    popupContent += '    // -->\r\n';
    popupContent += '    </SCRIPT>\r\n';
    popupContent += '    </body>\r\n';
    popupContent += '</html>\r\n';

    return popupContent;
} // end function "getPopupWindow"



