(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            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);
                
            $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');
            
            $wrapper.scrollLeft(singleWidth * visible);
            
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
					left = singleWidth * dir;
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * (visible - 1));
                    }
                    
                    currentPage = page;
					
                });
            }
			
			function gotoPageFast(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
					left = singleWidth * dir;
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 300, function () {
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * (visible - 1));
                    }
					
                    currentPage = page;
					
                });
            }
			
            
            $wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
            
            //$('a.back', this).click(function () {
            //    gotoPageFast(currentPage + 1);
            //    return false;
            //});
			
			$('a.back', this).mouseover(function () {
				nextautoscrolling = true;
				setInterval(function () {
					if (nextautoscrolling) {
						$('.infiniteCarousel').trigger('nextfast');
					}
				}, 120);
				}).mouseout(function () {
					nextautoscrolling = false;
			});
			
            //$('a.forward', this).click(function () {
            //    gotoPage(currentPage - 1);
            //    return false;
            //});
			
			$('a.forward', this).mouseover(function () {
				backautoscrolling = true;
				setInterval(function () {
					if (backautoscrolling) {
						$('.infiniteCarousel').trigger('goback');
					}
				}, 5);
				}).mouseout(function () {
					backautoscrolling = false;
			});
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });

            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
			
			$(this).bind('nextfast', function () {
                gotoPageFast(currentPage + 1);
            });
			
			$(this).bind('goback', function () {
                gotoPageFast(currentPage - 1);
            });
        });
    };
})(jQuery);
