/* 
 * ImageAjaxSlide
 * @author Lars Boldt 06/2011
 */
jQuery(document).ready(function($) {
    $('div.imageAjaxSlide').each(function() {
        var element = $(this);
        var idData = $(this).attr('id').split(':');
        var tileId  = idData[0];
        var catId   = idData[1];
        var nSlides = idData[2];
        
        if (nSlides > 1) {
            setTimeout(function() {
                ImageAjaxSlideRefresh(element, tileId, catId);
            }, 2000);
        }
    });
    
    ImageAjaxSlideRefresh = function(element, tileId, catId) {
        $.post('/xmlhttprequest.php?service=ImageAjaxSlide.refresh',
            {tId: tileId, cId: catId},
            function(imageData) {
                var next = element.children('ul').children('li.next:first');
                var current = element.children('ul').children('li.current:first');
                next.html(imageData.data);
                current.fadeTo(3000, 1, function() {
                    current.fadeOut(2000, function() {
                        next.addClass('current').removeClass('next');
                        current.addClass('next').removeClass('current');
                        current.fadeTo(1,1);
                        setTimeout(function() {
                            ImageAjaxSlideRefresh(element, tileId, catId);
                        }, 2000);
                    })
                });                
            }, 'json'
        );
    }
});
