$.fn.circularScroll = function(conf) {

    var options = {
        size: 4,
        scroll: 1,

        items: '.blogger_block',
        prev: '.prev',
        next: '.next',
        prevPage: '.prevPage',
        nextPage: '.nextPage',

        // disable scroller
        disable: false
    };

    $.extend(options, conf);

    return this.each(function () {

        var $wrapper = $(this);
        var $slider = $('> div', this);
        var $items = $(options.items, this);

        var wrapperWidth = $wrapper.innerWidth();
        var itemWidth = $items.eq(0).outerWidth();
        var nItems = $items.length;
        //var nPages = Math.ceil($items.length / options.size);
        //var currentPage = 1;
        var currentItem = 1;

        $items.filter(':first').before($items.slice(- options.size).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, options.size).clone().addClass('cloned'));

        var clonedWidth = itemWidth * options.size;

        // scroll To first item
        $wrapper.scrollLeft(clonedWidth);

        // bind function
        $(options.prev).click(function() {
            return prev();
        });
        $(options.next).click(function() {
            return next();
        });

        // disable scroller if number of item < size
        if (nItems <= options.size) disableScroller(true);

        function disableScroller(disable) {
            if (disable) {
                options.disable = true;
                // disable prev/next button
                $(options.prev).css({'opacity': 0.5});
                $(options.next).css({'opacity': 0.5});
            } else {
                options.disable = false;
                $(options.prev).css({'opacity': 1});
                $(options.next).css({'opacity': 1});
            }
        }

        function next() {
            return scrollTo(currentItem + options.scroll);
        }

        function prev() {
            return scrollTo(currentItem - options.scroll);
        }

        function scrollTo(item) {
            if (options.disable == true) return;
            var dir = item < currentItem ? -1 : 1;
            var delta = dir * itemWidth;

            $wrapper.filter(':not(:animate)').animate({
                scrollLeft: '+=' + delta
            }, 400, function() {
                if (item == 0) {
                    $wrapper.scrollLeft(itemWidth * (nItems-1) + clonedWidth);
                    item = (nItems);
                } else if (item > nItems) {
                    $wrapper.scrollLeft(clonedWidth);
                    item = 1;
                }
                currentItem = item;
            });
        }
    });
};




