/* 
 * 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);
        }
        
        element.children('ul.helper').children('li').children('a').bind('click', function() {
            if (element.attr('usrint') != 'true') {
                element.attr('usrint', true);
                clearTimeout(element.attr('tid'));
                ImageAjaxSlideIndex(element, tileId, catId, $(this).attr('href'));
            }
            return false;
        });
        element.children('ul.helper:first').css('margin-left', '-' + Math.round(parseInt(element.children('ul.helper:first').css('width'))/2) + 'px');
    });
    
    ImageAjaxSlideIndex = function(element, tileId, catId, sIndex) {
        var loader = element.children('ul:first').children('li.loader');
        loader.show();
        $.post('/xmlhttprequest.php?service=ImageAjaxSlide.index',
            {tId: tileId, cId: catId, sId: sIndex},
            function(imageData) {
                var next = element.children('ul:first').children('li.next:first');
                var current = element.children('ul:first').children('li.current:first');
                current.stop();
                next.html(imageData.data);
                element.attr('usrint', false);                
                loader.hide();
                current.fadeOut(500, function() {
                    next.addClass('current').removeClass('next');
                    current.addClass('next').removeClass('current');
                    current.fadeTo(1,1);                        
                    var tID = setTimeout(function() {
                        ImageAjaxSlideRefresh(element, tileId, catId);
                    }, 2000);
                    clearTimeout(element.attr('tid'));
                    element.attr('tid', tID);
                });
            }, 'json'
        );
    }
    
    ImageAjaxSlideRefresh = function(element, tileId, catId) {
        clearTimeout(element.attr('tid'));
        $.post('/xmlhttprequest.php?service=ImageAjaxSlide.refresh',
            {tId: tileId, cId: catId},
            function(imageData) {
                if (element.attr('usrint') != 'true') {
                    var next = element.children('ul:first').children('li.next:first');
                    var current = element.children('ul:first').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);
                            if (element.attr('usrint') != 'true') {
                                var tID = setTimeout(function() {
                                    ImageAjaxSlideRefresh(element, tileId, catId);
                                }, 2000);
                                clearTimeout(element.attr('tid'));
                                element.attr('tid', tID);
                            }
                        })
                    });
                }
            }, 'json'
        );
    }
});
