(function($) {                                          // Compliant with jquery.noConflict()
$.fn.carouselRight = function(o) {
    o = $.extend({
        btnPrev: null,
        btnNext: null,
        btnGo: null,
        mouseWheel: false,
        auto: null, 

        speed: 200,
        easing: null,

        vertical: false,
        circular: true,
        visible: 3,
        start: 0,
        scroll: 1,

        beforeStart: null,
        afterEnd: null
    }, o || {});

    return this.each(function() {                       // Returns the element collection. Chainable.
        var curr = o.start, running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
        var div = $(this), ul = $("ul", this), li = $("ul > li", this), itemLength = li.size();                       
        div.css("visibility", "visible");

        li.css("overflow", "hidden")                        // If the list item size is bigger than required
            .css("float", "left");
        ul.css("position", "absolute")                    // IE BUG - width as min-width
            .css("top", "38px")
			.css("left", "0px");
        div.css("overflow", "hidden")                       // Overflows - works in FF
            .css("position", "relative")                    // position relative and z-index for IE
            .css("z-index", "2")                            // more than ul so that div displays on top of ul
            .css("left", "0px")                            // after creating carousel show it on screen
			.css("height", "440px");
			
        var liSize = o.vertical ? height(li) : width(li);   // Full li size(incl margin)-Used for animation
        var ulSize = liSize * itemLength;                   // size of full ul(total length, not just for the visible items)
        var divSize = liSize * o.visible;                   // size of entire div(total length for just the visible items)


        ul.css(sizeCss, ulSize+"px")                        // Width of the UL is the full length for all the images
            .css(animCss, -(curr*liSize));                  // Set the starting item

        div.css(sizeCss, divSize+"px");                     // Width of the DIV. length of visible images

        if(o.btnPrev) {
            $(o.btnPrev).click(function() { 
                return go(curr-o.scroll); 
            });
        }
        
        if(o.btnNext) {
            $(o.btnNext).click(function() { 
                return go(curr+o.scroll); 
            });
        }
        
        if(o.btnGo) {
            $.each(o.btnGo, function(i, val) {
                $(val).click(function() {
                    return go(i);
					return false;
                });
            });
        }
        function vis() {
            return li.gt(curr-1).lt(o.visible);
        };

        function go(to) {
            if(!running) {
                running = true;
                if(o.beforeStart) o.beforeStart.call(this, vis());

                if(to<0 && curr==0) {                                               // If first, then goto last
                    if(o.circular) curr=itemLength-o.visible; 
                    else return;
                } else if(to>=itemLength-o.visible && curr+o.visible>=itemLength) { // If last, then goto first
                    if(o.circular) curr = 0; 
                    else return;
                } else curr = to;

                ul.animate(
                    animCss == "left" ? { left: -(curr*330) } : { top: -(curr*liSize) } , o.speed, o.easing,
                    function() {
                        ul.css(animCss, -(curr*330)+"px");    // For some reason the animation was not making left:0
                        if(o.afterEnd) o.afterEnd.call(this, vis());
                        running = false;
                    }
                );
            }
            return false;
        };
    });
};

function css(el, prop) {
    return parseInt($.css(el.jquery ? el[0] : el,prop)) || 0;
};
function width(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};
function height(el) {
    return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};

})(jQuery);