/**
 * ImageSlider plugin.
 * Copyright (C) Tiki Web Inteligente 2010 - http://www.tiki.com.br
 */
jQuery.fn.imageslider = (function(options) {

    var $ = jQuery;

    var options = $.extend({
        step: 1,
        speed: 500,
		orientation: 'horizontal',
        onReady: function() {}
    }, options);

    return this.each(function() {

        var $slider = $(this);
        
        if (options.orientation == 'horizontal') { // 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);
                var sliderCropWidth = $slider.find('.slider_crop').width();
                
                //var itemsTotalWidth = itemWidth * $slider.find('ul.lista > li').length;
                //var itemMargin = new Number($slider.find('ul.lista > li:nth-child(2)').css('marginLeft').replace(/px/, ''));
                //var firstItemMargin = new Number($slider.find('ul.lista > li:nth-child(1)').css('marginLeft').replace(/px/, ''));
                
                $slider.find('ul.lista').css({ 'width': itemsTotalWidth+'px' });
                
                
                // Desativa o botao Previous se necessario
                if( $slider.find('ul.lista li:first').position().left == '0' ) {
                    $slider.find('.slider_previous a').addClass('disabled');
                }
                // Desativa o botao Next se necessario
                if( $slider.find('ul.lista li:last').position().left == (sliderCropWidth - itemWidth)) {
                    $slider.find('.slider_next a').addClass('disabled');
                }
                
                $slider.find('.slider_previous a').click(function(){
                    if( !$(this).is('.disabled') ){
                        $slider.find('.slider_next a').removeClass('disabled');
                        $slider.find('.slider_crop').scrollTo({ top: 0, left: '-=' + (itemWidth * options.step) }, options.speed, function(){
                            // Se chegar ao inicio, desabilita o botão
                            if( $slider.find('ul.lista li:first').position().left == '0' ) {
                                $slider.find('.slider_previous a').addClass('disabled');
                            }
                        });
                    }
                });
                
                $slider.find('.slider_next a').click(function(){
                    if( !$(this).is('.disabled') ){
                        $slider.find('.slider_previous a').removeClass('disabled');
                        $slider.find('.slider_crop').scrollTo({ top: 0, left: '+=' + (itemWidth * options.step) }, options.speed, function(){
                            // Se chegar ao fim, desabilita o botão
                            if( $slider.find('ul.lista li:last').position().left == (sliderCropWidth - itemWidth)) {
                                $slider.find('.slider_next a').addClass('disabled');
                            }
                        });
                    };
                });
            }
		}
        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 * options.step), left: 0 }, options.speed);
				});
				
				$slider.find('.slider_next a').click(function(){
					$slider.find('.slider_crop').scrollTo({ top: '+=' + (itemHeight * options.step), left: 0 }, options.speed);
				});
			}
		}

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





//console.log(listWidth)
//console.log(sum)
