(function($) {

    // Default options
    var defaults = {
        numberOfRows: 1,
        scrollInterval: '50%',
        scrollEvent: 'click',
        scrollSpeed: 600
    };

    $.fn.gooseCarousel = function(o) {
        return this.each(function() {
            var options = $.extend(defaults, o);
            
            $(this).css('left','0px').addClass('gCarousel-inner').wrap('<div class="gCarousel-clip"><div class="gCarousel-wrap"></div></div>');
            var clipHeight = parseInt($('.gCarousel-clip').height());

            
            var fullWidth = 0;
            $(this).contents('li').each(function() {
                fullWidth += parseInt($(this).outerWidth(true));
            });
            
            // Wrap the thing in the clipping div, and explicitely set the width
            fullWidth = ( options.numberOfRows > 1 ? fullWidth / options.numberOfRows : fullWidth );
            $(this).width(fullWidth+'px');
            
            // Add the buttons
            $('.gCarousel-clip').prepend('<a href="javascript:;" class="gCarousel-btnLeft"></a>').append('<a href="javascript:;" class="gCarousel-btnRight"></a>');
            
            // Add padding for the buttons on either side
            var leftButtonWidth = parseInt($('.gCarousel-btnLeft').width()) +5;
            var rightButtonWidth = parseInt($('.gCarousel-btnRight').width()) +5;
            var clipWidth = $('.gCarousel-clip').width();
            $('.gCarousel-wrap').width(clipWidth - leftButtonWidth - rightButtonWidth).css({ 
                'left' : leftButtonWidth+'px',
                'height' : $('.gCarousel-inner').outerHeight(true)+'px' });
        
            // How far should we scroll the carousel with each click?
            // - Percentage of clipping window
            if (options.scrollInterval.toString().indexOf('%') > -1) {
                var interval = clipWidth * ( parseInt(options.scrollInterval) / 100 );
            }
            // - Exact number of pixels
            else if (options.scrollInterval.toString().indexOf('px') > -1) {
                var interval = parseInt(options.scrollInterval);
            }
            // - Number of "columns" (assuming a standard width)
            else {
                var interval = parseInt($('.gCarousel-inner li:first').width()) * options.scrollInterval;
            }
                
            // Activate the buttons
            $('a.gCarousel-btnLeft').live(options.scrollEvent,function() {
                if (parseInt($('.gCarousel-inner').css('left')) + interval <= 0) {
                    $('.gCarousel-inner').animate({'left' : '+='+interval+'px' }, options.scrollSpeed);
                }
            });
            
            $('a.gCarousel-btnRight').live(options.scrollEvent,function() {
                //var first = parseInt($('.gCarousel-inner').css('left')) - interval;
                //var second = 0 - fullWidth - clipWidth;
                //alert('first:'+first+'  second:'+second);
                if ( ( parseInt($('.gCarousel-inner').css('left')) - interval ) >= ( 0 - (fullWidth - ( clipWidth / 2 )))) {
                    $('.gCarousel-inner').animate({'left' : '-='+interval+'px' }, options.scrollSpeed);
                }
            });
        
        });
    };

        
})(jQuery);