
jQuery(function()
{
    var slideshow_delay = window.slideshow_delay ? window.slideshow_delay : 2000;
    if (slideshow_delay < 500) slideshow_delay = 500;

    jQuery('.slideshow').each(function(i, slideshow)
    {
        slideshow = jQuery(slideshow);
        // get slideshow id by stripping of 'slideshow-' prefix from id attribute
        var slideshow_id = slideshow.attr('id').substring(10); //10 = strlen('slideshow-')

        /**
         * Update photo using ajax request
         */
        function update_photo(url)
        {
            if ( ! url)
                return;

            jQuery.post(url + '&slideshow_id=' + slideshow_id, null, function(response){
                slideshow.html(response);
            });
        }

        // catch all slideshow clicks and make ajax requests
        slideshow.click(function(e)
        {
            var target = e.target;
            if (target.nodeName == 'A' && jQuery(target).hasClass('ajax'))
            {
                e.stopPropagation();
                e.preventDefault();

                update_photo(target.href);
            }
        });

        // move to the next slide automatically after a delay
        window.setInterval(function()
        {
            // get the url to the next photo from hidden <a>
            var url_next = jQuery('a.url_next').attr('href');

            update_photo(url_next);
        }, slideshow_delay);
    });
});

