(function($) {
    jQuery.fn.carrossel = function( options ){
        var obj = $( this );
        
        var defaults = {
            speed : 500,
            numShowLi : 4,
            next : ".left",
            previous : ".right",
            type : "vertical",
            typeList : 1
        }
        
        options = jQuery.extend( defaults, options );
        
        obj.each( function(){
            numLi = ( parseFloat( $( this ).find( "li" ).size() ) / options.typeList );
            if( options.type == "vertical" ){
                widthLi = parseFloat( $( this ).find( "li" ).width() );
                marginLeft = parseFloat( $( this ).find( "li" ).css( "margin-left" ) );
                marginRight = parseFloat( $( this ).find( "li" ).css( "margin-right" ) );
                borderLeft = parseFloat( $( this ).find( "li" ).css( "border-left-width" ) );
                borderRight = parseFloat( $( this ).find( "li" ).css( "border-right-width" ) );
                paddingLeft = parseFloat( $( this ).find( "li" ).css( "padding-left" ) );
                paddingRight = parseFloat( $( this ).find( "li" ).css( "padding-right" ) );
                widthLi = widthLi + marginLeft + marginRight + borderLeft + borderRight + paddingLeft + paddingRight;
                luWidth = numLi * widthLi;
                maxMargin = parseFloat( ( widthLi ) * ( options.numShowLi - numLi ) );
                $( this ).find( "ul" ).width( luWidth ).attr({ "margin-left" : 0, "max-margin" : maxMargin });
                $( this ).find( options.next ).bind( "click", function(){
                    marginLeft = parseFloat( $( this ).parent().parent().find( "ul" ).attr( "margin-left" ) );
                    maxMargin = parseFloat( $( this ).parent().parent().find( "ul" ).attr( "max-margin" ) );
                    if( marginLeft > maxMargin ){
                        marginLeft -= widthLi;
                    }
                    $( this ).parent().parent().find( "ul" ).attr( "margin-left", marginLeft ).animate({
                        "margin-left" : marginLeft
                    }, options.speed );
                });
                $( this ).find( options.previous ).bind( "click", function(){
                    marginLeft = parseFloat( $( this ).parent().parent().find( "ul" ).attr( "margin-left" ) );
                    if( marginLeft < 0 ){
                        marginLeft += widthLi;
                    }
                    $( this ).parent().parent().find( "ul" ).attr( "margin-left", marginLeft ).animate({
                        "margin-left" : marginLeft
                    }, options.speed );
                });
            }else{
                heightLi = parseFloat( $( this ).find( "li" ).height() );
                marginTop = parseFloat( $( this ).find( "li" ).css( "margin-top" ) );
                marginBottom = parseFloat( $( this ).find( "li" ).css( "margin-bottom" ) );
                borderTop = parseFloat( $( this ).find( "li" ).css( "border-top-width" ) );
                borderBottom = parseFloat( $( this ).find( "li" ).css( "border-bottom-width" ) );
                paddingTop = parseFloat( $( this ).find( "li" ).css( "padding-top" ) );
                paddingBottom = parseFloat( $( this ).find( "li" ).css( "padding-bottom" ) );
                heightLi = heightLi + marginTop + marginBottom + borderTop + borderBottom + paddingTop + paddingBottom;
                maxMargin = parseFloat( ( heightLi ) * ( options.numShowLi - numLi ) );
                luHeight = numLi * heightLi;
                $( this ).find( "ul" ).height( luHeight ).attr({ "margin-top" : 0, "max-margin" : maxMargin } );
                $( this ).find( options.next ).bind( "click", function(){
                    marginTop = parseFloat( $( this ).parent().parent().find( "ul" ).attr( "margin-top" ) );
                    maxMargin = parseFloat( $( this ).parent().parent().find( "ul" ).attr( "max-margin" ) );
                    if( marginTop > maxMargin ){
                        marginTop -= heightLi;
                    }
                    $( this ).parent().parent().find( "ul" ).attr( "margin-top", marginTop ).animate({
                        "margin-top" : marginTop
                    }, options.speed );
                });
                $( this ).find( options.previous ).bind( "click", function(){
                    marginTop = parseFloat( $( this ).parent().parent().find( "ul" ).attr( "margin-top" ) );
                    if( marginTop < 0 ){
                        marginTop += heightLi;
                    }
                    $( this ).parent().parent().find( "ul" ).attr( "margin-top", marginTop ).animate({
                        "margin-top" : marginTop
                    }, options.speed );
                });
            }
        });
    }
})(jQuery);
