(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
            
            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = true;
    
    $('.infiniteCarousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });
    
    setInterval(function () {
        if (autoscrolling) {
            $('.infiniteCarousel').trigger('next');
        }
    }, 5000);
});


(function () {
    $.fn.infiniteCarouselB = function () {
        function repeat(strB, nB) {
            return new Array( nB + 1 ).join(strB);
        }
        
        return this.each(function () {
            // magic!
            var $wrapperB = $('> div', this).css('overflow', 'hidden'),
                $sliderB = $wrapperB.find('> ul').width(9999),
                $itemsB = $sliderB.find('> li'),
                $singleB = $itemsB.filter(':first')
                
                singleWidthB = $singleB.outerWidth(),
                visibleB = Math.ceil($wrapperB.innerWidth() / singleWidthB),
                currentPageB = 1,
                pagesB = Math.ceil($itemsB.length / visibleB);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($itemsB.length % visibleB != 0) {
                // pad
                $sliderB.append(repeat('<li class="empty" />', visibleB - ($itemsB.length % visibleB)));
                $itemsB = $sliderB.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $itemsB.filter(':first').before($itemsB.slice(-visibleB).clone().addClass('cloned'));
            $itemsB.filter(':last').after($itemsB.slice(0, visibleB).clone().addClass('cloned'));
            $itemsB = $sliderB.find('> li');
            
            // 3. reset scroll
            $wrapperB.scrollLeft(singleWidthB * visibleB);
            
            // 4. paging function
            function gotoPageB(pageB) {
                var dirB = pageB < currentPageB ? -1 : 1,
                    nB = Math.abs(currentPageB - pageB),
                    leftB = singleWidthB * dirB * visibleB * nB;
                
                $wrapperB.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + leftB
                }, 500, function () {
                    // if page == last page - then reset position
                    if (pageB > pagesB) {
                        $wrapperB.scrollLeft(singleWidthB * visibleB);
                        pageB = 1;
                    } else if (pageB == 0) {
                        pageB = pagesB;
                        $wrapperB.scrollLeft(singleWidthB * visibleB * pagesB);
                    }
                    
                    currentPageB = pageB;
                });
            }
            
            // 5. insert the back and forward link
            $wrapperB.after('<a href="#" class="arrowB backB">&lt;</a><a href="#" class="arrowB forwardB">&gt;</a>');
            
            // 6. bind the back and forward links
            $('a.backB', this).click(function () {
                gotoPageB(currentPageB - 1);
                return false;
            });
            
            $('a.forwardB', this).click(function () {
                gotoPageB(currentPageB + 1);
                return false;
            });
            
            $(this).bind('goto', function (event, pageB) {
                gotoPageB(pageB);
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPageB(currentPageB + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrollingB = true;
    
    $('.infiniteCarouselB').infiniteCarouselB().mouseover(function () {
        autoscrollingB = false;
    }).mouseout(function () {
        autoscrollingB = true;
    });
    
    setInterval(function () {
        if (autoscrollingB) {
            $('.infiniteCarouselB').trigger('next');
        }
    }, 5000);
});