(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(99999),
                $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);
                    }
					
					$(this).parent().find('.page' + currentPage).removeClass("active");
                    currentPage = page;
					$(this).parent().find('.page' + page).addClass("active");
                });
            }
            
            // 5. insert the back and forward link
            $wrapper.after('<div class="pager_container"><ul class="pager"><li id="pager_width"><a rel="0" class="pagenum page1 active" href="#">1</a></li><li><a rel="1" class="pagenum page2" href="#">2</a></li><li><a rel="2" class="pagenum page3" href="#">3</a></li><li><a rel="3" class="pagenum page4" href="#">4</a></li><li><a rel="5" class="pagenum page5" href="#">5</a></li><li><a rel="6" class="pagenum page6" href="#">6</a></li><li><a rel="7" class="pagenum page7" href="#">7</a></li><li><a rel="8" class="pagenum page8" href="#">8</a></li><li><a rel="9" class="pagenum page9" href="#">9</a></li><li><a rel="10" class="pagenum page10" href="#">10</a></li><div style="clear:both;"></div></ul><div style="clear:both;"></div></div>');
            
            // 6. bind the back and forward links
            $('a.page1', this).click(function () {
                gotoPage(1);
                return false;
            });
			
			$('a.page2', this).click(function () {
                gotoPage(2);
                return false;
            });
			
			$('a.page3', this).click(function () {
                gotoPage(3);
                return false;
            });
			
			$('a.page4', this).click(function () {
                gotoPage(4);
                return false;
            });
			
			$('a.page5', this).click(function () {
                gotoPage(5);
                return false;
            });
			
			$('a.page6', this).click(function () {
                gotoPage(6);
                return false;
            });
			
			$('a.page7', this).click(function () {
                gotoPage(7);
                return false;
            });
			
			$('a.page8', this).click(function () {
                gotoPage(8);
                return false;
            });
			
			$('a.page9', this).click(function () {
                gotoPage(9);
                return false;
            });
			
			$('a.page10', this).click(function () {
                gotoPage(10);
                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);

