(function($) {

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

    $.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());
            
            // Run through and add some classes
            $(this).contents('li').each(function(index,element) {
                $(this).addClass('gCarousel-item');
            });

            var fullWidth = 0;
            
            // Need this fix for ArriveAlive in Opera.... stupid Opera
            if ($.browser.opera) {
                $(this).contents('li').contents('a').contents('img').each(function() {
                    $(this).one("load",function(){
                        fullWidth += parseInt($(this).outerWidth(true));
                    });
                    if (this.complete) { $(this).trigger('load'); }
                });
            } else {
                $(this).contents('li').each(function() {
                    fullWidth += parseInt($(this).outerWidth(true));
                    //fullWidth += parseInt($(this).contents('a').contents('img').outerWidth(true)) +20;
                });
            }
            
            // Wrap the thing in the clipping div, and explicitely set the width
            fullWidth = ( options.numberOfRows > 1 ? fullWidth / options.numberOfRows : fullWidth );
            
            // Add a little more
            fullWidth = fullWidth+parseInt($(this).contents('li:first').outerWidth(true));
            
            $(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();
            var wrapWidth = clipWidth - leftButtonWidth - rightButtonWidth;
            $('.gCarousel-wrap').width(wrapWidth).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($(this).contents('li:first').outerWidth(true)) * options.scrollInterval;
            }
                
            // Activate the buttons
            $('a.gCarousel-btnLeft').live(options.scrollEvent,function() {
                if (parseInt($(this).next().contents('.gCarousel-inner').css('left')) + interval <= 0) {
                    $(this).next().contents('.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 / 2);
                //if ($.browser.opera) { alert('first:'+first+'  second:'+second); }
                if ( ( parseInt($(this).prev().contents('.gCarousel-inner').css('left')) - interval ) >= ( 0 - (fullWidth - ( clipWidth / 2 )))) {
                    //if ($.browser.opera) { alert('yes'); }
                    $(this).prev().contents('.gCarousel-inner').animate({'left' : '-='+interval+'px' }, options.scrollSpeed);
                }
            });
            
            // Is there a current item? (exactly one, please)
            if ($(this).contents(options.currentItem).length == 1) {
                var scrollTo = $(this).contents('li').index($(this).contents(options.currentItem));
                if (options.numberOfRows > 1) {
                    var itemsPerRow = Math.floor($(this).contents('li').length / options.numberOfRows)+1;
                    scrollTo = (scrollTo % itemsPerRow);
                    if (scrollTo < 0) { scrollTo = itemsPerRow; }
                }
                scrollTo = scrollTo * $(this).contents('li:first').outerWidth();
                if (scrollTo > wrapWidth) {
                    $(this).animate({'left' : '-='+scrollTo+'px' }, options.scrollSpeed);
                }
            }
        
        });
    };

        
})(jQuery);
