(function($) {

    $.fn.imageslider = function(options) {

        return this.each(function() {

            var settings = {};
            for (var prop in $.fn.imageslider.defaults) {
                settings[prop] = $.fn.imageslider.defaults[prop];
            }

            if (options) {
                $.extend(settings, options);
            }

            var $slider = $(this),
                currPosition = 0,
                maxPosition = $slider.find('li').length - 1,
                $allBoxes = $slider.find('li'),
                visibleAreaWidth = $('#mainContent').width();

            // garante que a esteira estará zerada quando for exibida
            $slider.find('.slider_crop').scrollTo({ top: 0, left: 0 });
            
            if (settings.orientation == 'horizontal') {
                if ($slider.find('li').length > 1) {
                    // Calcula a largura de todos li's
                    var listWidth = [];
                    $slider.find('.lista > li').each(function(){
                        listWidth.push($(this).outerWidth(true));
                    });
                    var itemsTotalWidth = 0;
                    for (width in listWidth) {
                        itemsTotalWidth += parseInt(listWidth[width]);
                    }
                    
                    var itemWidth = $slider.find('ul.lista > li:nth-child(2)').outerWidth(true);
                    
                    $slider.find('ul.lista').css({ 'width': itemsTotalWidth+'px' });
                    
                    $slider.find('.slider_previous a').click(function(){                    
                        if (settings.slideStyle == 'default') {
                            $slider.find('.slider_crop').scrollTo({ top: 0, left: '-=' + (itemWidth * settings.step) }, settings.speed);
                        } else if (settings.slideStyle == 'itemPerItem') {
                            if (currPosition > 0) {
                                currPosition--;
                                $slider.find('.slider_crop').scrollTo( $slider.find('li').eq(currPosition), settings.speed);
                            }
                        }
                    });
                    
                    $slider.find('.slider_next a').click(function(){
                        if (settings.slideStyle == 'default') {
                            $slider.find('.slider_crop').scrollTo({ top: 0, left: '+=' + (itemWidth * settings.step) }, settings.speed);
                        } else if (settings.slideStyle == 'itemPerItem') {
                            var $nextVisibleBoxes = $allBoxes.filter(':gt('+currPosition+')').add($slider.find('li').eq(currPosition));
                            var nextVisibleBoxesTotalWidth = 0;
                            $nextVisibleBoxes.each(function() {
                                nextVisibleBoxesTotalWidth += $(this).width();
                            });
                            if (currPosition < maxPosition && nextVisibleBoxesTotalWidth > visibleAreaWidth) {
                                currPosition++;
                                $slider.find('.slider_crop').scrollTo( $slider.find('li').eq(currPosition), settings.speed);
                            }
                        }
                    });
                }
            }

            else { // vertical
                if ($slider.find('li').length > 1) {
                    itemHeight = $slider.find('li:nth-child(2)').outerHeight(true);

                    $slider.find('.slider_previous a').click(function() {
                        $slider.find('.slider_crop').scrollTo({ top: '-=' + (itemHeight * settings.step), left: 0 }, settings.speed);
                    });

                    $slider.find('.slider_next a').click(function() {
                        $slider.find('.slider_crop').scrollTo({ top: '+=' + (itemHeight * settings.step), left: 0 }, settings.speed);
                    });
                }
            }

            if ($.isFunction(settings.onReady)) {
                settings.onReady.call();
            }
        });
    };

})(jQuery);

jQuery.fn.imageslider.defaults = {
    step: 2,
    speed: 550,
    orientation: 'horizontal',
    slideStyle: 'default',
    onReady: function() { }
}

