(function($){
    $.fn.lightbox = function(){
        var images = this;
        var nbImages = 0;
        var infosImages = {};
        var currentImage = 0;
        _parcourirImages();
        $(this).click(function(){
            for(key in infosImages)
            {
                if(infosImages[key].src == $(this).attr('src'))
                {
                    id = key;
                }
            }
            if(!id) id = 1;

            _openWindows(id);
        });

        function _openWindows(id){
            _closeWindows();
            $('BODY').append(
                '<div id=\'lightboxWindows\'></div>' +
                '<div id=\'lightboxClose\'><img src="images/closeLightbox.png" id="closeLightbox"/></div>' +
                '<div id=\'lightboxContainer\'></div>' +
                '<div id=\'lightboxNav\'></div>');
            $('#lightboxWindows').css({height: $(document).height()});
            
            _show(id);
            _showNav();

            $('#nextNav').live('click', function(){
                _nextImage();
            });
            $('#previousNav').live('click', function(){
                _previousImage();
            });
            $('#closeLightbox').live('click', function(){
                _closeWindows();
            });
            $('#lightboxWindows').dblclick(function(){
                _closeWindows();
            });
            _setPosition();
        };

        function _closeWindows(){
            $('#lightboxContainer').fadeOut('slow', function(){
                $('#lightboxContainer').remove();
            $('#lightboxWindows').fadeOut('slow', function(){
                $('#lightboxWindows').remove();
            });
            });
            $('#lightboxClose').fadeOut('slow', function(){
                $('#lightboxClose').remove();
            });
            $('#lightboxNav').fadeOut('slow', function(){
                $('#lightboxNav').remove();
            });

        };

        function _setPosition(){
            setTop = (($(window).height() / 2) - ($("#lightboxContainer").outerHeight() / 2));
            setLeft = (($(window).width() / 2) - ($("#lightboxContainer").outerWidth() / 2));
            pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';
            $('#lightboxContainer').css({
                top: setTop,
                left: setLeft,
                position: pos
            });

            $('#lightboxNav').css({
                top: setTop + $("#lightboxContainer").outerHeight() + 10,
                left: setLeft,
                width:$("#lightboxContainer").outerWidth(),
                position: pos
            });

            $('#lightboxClose').css({
                top: setTop - 2,
                left: setLeft + $("#lightboxContainer").width() - 12,
                position: pos
            });
        };

        function _parcourirImages(){
            var nb = 0;
            $(images).each(function(){
                nb++;
                if($(this).attr('alt'))
                {
                    infosImages[nb] = {
                        src : $(this).attr('src'),
                        alt : $(this).attr('alt')                
                    }
                }
                else
                {
                    infosImages[nb] = {
                        src : $(this).attr('src'),
                        alt : ''                
                    }
                }
                $(this).css('cursor','pointer');
            });
            nbImages = nb;
        };

        function _show(id){
            $('#lightboxContainer').html('<img src=\''+infosImages[id].src+'\' alt="lightbox"/>');
            currentImage = id;
            
        }

        function _showNav(){
            html = '';
            if(infosImages[currentImage].alt != '')
            {
                html = html + infosImages[currentImage].alt+'<br/>';
            }

            if(nbImages > 1)
            {
                if(currentImage > 1 && currentImage < nbImages)
                {
                    html = html + '<span id="previousNav">Précédent</span> | <span id="nextNav">Suivant</span><br/>' + currentImage + ' - ' + nbImages;
                }
                else if(currentImage > 1 && currentImage == nbImages)
                {
                    html = html + '<span id="previousNav">Précédent</span> <br/>' + currentImage + ' - ' + nbImages;
                }
                else if(currentImage == 1 && currentImage < nbImages)
                {
                    html = html + '<span id="nextNav">Suivant</span><br/>' + currentImage + ' - ' + nbImages;
                }
            }
            $('#lightboxNav').html(html);
        }

        function _nextImage(){
            if(currentImage < nbImages)
            {
                currentImage++;
                $('#lightboxContainer').fadeOut('slow', function(){
                    $('#lightboxContainer').html('<img src=\''+infosImages[currentImage].src+'\' alt="lightbox"/>');
                    _setPosition();
                    _showNav()
                });
                $('#lightboxClose').fadeOut('slow');
                $('#lightboxNav').fadeOut('slow');
                
                $('#lightboxContainer').fadeIn('slow');
                $('#lightboxClose').fadeIn('slow');
                $('#lightboxNav').fadeIn('slow');
            }
        }

        function _previousImage(){
            if(currentImage > 1)
            {
                currentImage--;
                $('#lightboxContainer').fadeOut('slow', function(){
                    $('#lightboxContainer').html('<img src=\''+infosImages[currentImage].src+'\' alt="lightbox"/>');
                    _setPosition();
                    _showNav()
                });
                $('#lightboxClose').fadeOut('slow');
                $('#lightboxNav').fadeOut('slow');
                
                $('#lightboxContainer').fadeIn('slow');
                $('#lightboxClose').fadeIn('slow');
                $('#lightboxNav').fadeIn('slow');
            }
        }
    };
})(jQuery);
