function initSlideShow(slideshowId, settings)
{
    $(function() {
        //Settings
        if (typeof settings == 'undefined') {
            settings = {};
        }
        if (!('speed' in settings)) {
            settings.speed = 6000; //speed in mseconds
        }
        if (!('showcaption' in settings)) {
            settings.showcaption = true; //show caption [true/false/onmouseover]
        }

        //Global vars
        var slideshow = $('#'+slideshowId);
        var slideshowPause      = false;
        var slideshowStop       = false;
        var currentSlideNr      = 0;
        var slideshowFirstSlide = slideshow.find('li:first');
        var slideshowSlides     = slideshow.find('li');
        var slideshowCaption    = slideshow.find('li#slideshowcaption');
        var slideshowCContent   = slideshow.find('div#slideshowcontent');
        var slideshowMenu       = $('#slideshowmenu');
        var minCaptionHeight    = slideshowCaption.height();
        var captionHeight       = minCaptionHeight;

        //Show or Hide caption function
        var showHideCaption = function(cap, showHide, animate)
        {
            //Hide caption
            if (showHide == 'hide') {
                if (animate) {
                    slideshowCaption
                        .animate({
                            'opacity': 0.0
                        }, {
                            'queue': false,
                            'duration': settings.speed/120
                        })
                        .animate({
                            'height': '1px'
                        }, {
                            'queue': true,
                            'duration': settings.speed/20,
                            'complete': function() {
                                slideshowCContent.html(''); //empty caption text
                            }
                        });
                }
            }
            //Show caption
            if (showHide == 'show') {
                if (animate) {
                    slideshowCaption
                        .animate({
                            'opacity': 0.7
                        }, {
                            'queue': true,
                            'duration': settings.speed/60,
                            'complete': function() {
                                slideshowCContent.html(cap); //new caption text
                                captionHeight = parseFloat(slideshowCContent.height()) + parseFloat(slideshowCContent.css('margin-top').replace(/px/,'')) + parseFloat(slideshowCContent.css('margin-bottom').replace(/px/,''));
                                slideshowCaption
                                    .animate({
                                        'height': (captionHeight > minCaptionHeight) ? captionHeight : minCaptionHeight
                                    }, {
                                        'queue': true,
                                        'duration': settings.speed/12
                                    });
                            }
                    })
                }
            } else {
                slideshowCContent.html(cap); //new caption text
                if (slideshowCaption.height() > 1) {
                    captionHeight = parseFloat(slideshowCContent.height()) + parseFloat(slideshowCContent.css('margin-top').replace(/px/,'')) + parseFloat(slideshowCContent.css('margin-bottom').replace(/px/,''));
                    slideshowCaption.css({'height': (captionHeight > minCaptionHeight) ? captionHeight : minCaptionHeight});
                }
            }
        } // showHideCaption funcion

        var gallery = function(nr)
        {
            if ((!slideshowPause && !slideshowStop) || typeof nr != 'undefined') {
                //if no IMGs have the show class, grab the first image
                var currentSlide = ($('#slideshow li.show')?  $('#slideshow li.show') : slideshowFirstSlide);

                //if no button is clicked, get next picture
                if (typeof nr == 'undefined') {
                    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
                    var nextSlide;
                    if (currentSlide.next().length) {
                        if (currentSlide.next().attr('id') == slideshowCaption.attr('id')) {
                            nextSlide = slideshowFirstSlide;
                            currentSlideNr = 0;
                        } else {
                            nextSlide = currentSlide.next();
                            ++currentSlideNr;
                        }
                    } else {
                        nextSlide = slideshowFirstSlide;
                        currentSlideNr = 0;
                    }
                } else {
                    currentSlideNr = nr;
                    nextSlide = slideshow.find('li:eq('+nr+')');
                }

                //Set the fade in effect for the next image, show class has higher z-index
                nextSlide
                    .css({opacity: 0.0})
                    .addClass('show')
                    .animate({'opacity': 1.0}, settings.speed/6);

                //Hide the current image
                currentSlide
                    .animate({'opacity': 0.0}, settings.speed/6)
                    .removeClass('show');

                //Hide, change and show caption
                var captionText = nextSlide.find('img').attr('rel');
                showHideCaption('', 'hide', (settings.showcaption == true ? true : false));
                showHideCaption(captionText, 'show', (settings.showcaption == true ? true : false));

                //Set menubutton active
                slideshowMenu.find('li').removeClass('selected');
                slideshowMenu.find('li:eq('+currentSlideNr+')').addClass('selected');
            }
        }

        //Prepare all slides
        slideshowSlides.css({'opacity': 0.0}); //opacity to 0%
        slideshowFirstSlide.css({'opacity': 1.0}); //first image opacity to 100%

        //Place slidebuttons
        slideshowMenu.css({'visibility': 'visible'});
        slideshowMenu.find('li').find('a').each(function(i) {
            $(this).click(function() {
                gallery(i);
                slideshowStop = true;
            })
        });

        //Prepare first caption
        slideshowCContent.html(slideshowFirstSlide.find('img').attr('rel')); //set caption html
        slideshowCaption.css({
            'opacity': 0.0, //opacity of caption background to 0%
            'width': slideshowFirstSlide.find('img').css('width') //set width of caption
        });

        //Show caption direct
        if (settings.showcaption == true) {
            slideshowCaption.css({'opacity': 0.8}); //opacity of caption background to 70%
        } else {
            slideshowCaption.height(1); //height to 1px
        }

        //Prepare show caption on mouseover
        slideshow.hover(
            function() {
                slideshowPause = true;
                if (settings.showcaption == 'onmouseover') {
                    showHideCaption(slideshow.find('li.show').find('img').attr('rel'), 'show', true);
                }
            },
            function() {
                slideshowPause = false;
                if (settings.showcaption == 'onmouseover') {
                    showHideCaption('', 'hide', true);
                }
            }
        );

        //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
        setInterval(function() {gallery()}, settings.speed);
    });
}