$.fn.carousel = function(previous, next, options){
    var sliderList = $(this).children()[0];
    
    if (sliderList) {
        var increment = $(sliderList).children().outerWidth("true"), elmnts = $(sliderList).children(), numElmts = elmnts.length, sizeFirstElmnt = increment, shownInViewport = Math.round($(this).width() / sizeFirstElmnt), firstElementOnViewPort = 1, isAnimating = false;
        for (i = 0; i < shownInViewport; i++) {
            $(sliderList).css('width', (numElmts + shownInViewport) * increment + increment + "px");
            $(sliderList).append($(elmnts[i]).clone());
        }
        
        $(previous).click(function(event){
        
            if (!isAnimating) {
                if (firstElementOnViewPort <= 1) {
                    $(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
                    firstElementOnViewPort = numElmts;
                }
                else {
                    firstElementOnViewPort = firstElementOnViewPort - 1;
                }
                
                //Animate 
                var properties = {
                    left: "+=" + increment * 1,
                    y: 0,
                    queue: true
                }
                //Controls the animation time
                $(sliderList).animate(properties, 1, "swing", function(){
                    isAnimating = false;
                });
                isAnimating = true;
                
            }
            
        });
        
        $(next).click(function(event){
            if (!isAnimating) {
                if (firstElementOnViewPort > numElmts) {
                    firstElementOnViewPort = 2;
                    $(sliderList).css('left', "0px");
                }
                else {
                    firstElementOnViewPort = firstElementOnViewPort + 1;
                }
                
                //Animate 
                var properties = {
                    left: "-=" + increment * 1,
                    y: 0,
                    queue: true
                }
                //Controls the animation time
                $(sliderList).animate(properties, 1, "swing", function(){
                    isAnimating = false;
                });
                isAnimating = true;
            }
        });
    }
};

