
$.fn.infiniteCarousel = function (wrapperSelector, preAnimateFunc, postAnimateFunc, liClickFunc) {
    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    // Attach stuff to the FIRST element identified by "wrapperSelector"
    return this.each(function (theIndex) {

        var $wrapper = $(wrapperSelector, this),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            

            $items.each(function(liIndex,liElem) {
                if (liClickFunc) {
                    $(liElem).click(liClickFunc);
                }
                $(liElem).hover(
                    function() {
                        $(liElem).addClass('hover');
                    },
                    function() {
                        $(liElem).removeClass('hover');
                    }
                );
            });
        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned')); // .css("border", "1px solid blue")
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));  // .css("border", "1px solid red")
        
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.get(0).scrollLeft = singleWidth * visible;
        
        // 4. Bind to the forward and back buttons
        $('img.scrollLeft', this).click(function () {
            
            var pg = currentPage - 1;
            var toPg = (pg < 1 ? pages : pg);
            
            if (preAnimateFunc) preAnimateFunc(currentPage, toPg);
            gotoPage(pg);
            if (postAnimateFunc) postAnimateFunc(currentPage, toPg);
            return false;
        });
        
        $('img.scrollRight', this).click(function () {
            var pg = currentPage + 1;
            var toPg = (pg > pages ? 1 : pg);
            // alert(pg + ", "+toPg);

            if (preAnimateFunc) preAnimateFunc(currentPage, toPg);
            gotoPage(pg);
            if (postAnimateFunc) postAnimateFunc(currentPage, toPg);
            return false;
        });
        
        // 5. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1, // page < currentPage ? 1 : -1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            // console.log('currentPage', currentPage, 'left', left, 'n', n);
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    currentPage = pages;
                    // $wrapper.css('scrollLeft', singleWidth * visible * pages); //  * -1
                    this.scrollLeft = singleWidth * visible * pages;
                } else if (page > pages) {
                    currentPage = 1;
                    this.scrollLeft = singleWidth * visible; //  * -1
                    // $wrapper.css('scrollLeft', singleWidth * visible); //  * -1
                    // reset back to start position
                } else {
                    currentPage = page;
                }
                // console.log($slider.attr('id') + ", currentPage = " + currentPage);
            });                
            
            return false;
        }
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};



