// JavaScript Document


$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
        $slider = $wrapper.find('> ul'),
        $items = $slider.find('> li'),
        //$single = $items.filter(':first'),
        //wp_wdt = $wrapper.outerWidth();
        //wp_wdt_fix = $wrapper.outerWidth();
        arr_pag = new Array();
        arr_pag_aux = new Array();
        cont_page=1;
			
        tm= 840;
        tmv=tm;
        soma_tam=0;
			
        qnt_first=0;
        qnt_last=0;
						 
        $items.each(function(i) {
			    
            tmv = tmv - $(this).outerWidth();  // tamanho disponivel = tamanho que possuia livre menos o tamanho da largura da foto atual
            if(tmv>=0){
				  
                arr_pag_aux.push($(this));  // array da pagina atual recebe a foto
            }
            else {
                soma_tam=0;  // variavel da soma das fotos da pagina atual
                // for(var f in arr_pag_aux)
                for(f=0;f<arr_pag_aux.length;f++){
                    soma_tam = (soma_tam + $(arr_pag_aux[f]).outerWidth());
                }
                var t = soma_tam;
                soma_tam=((tm-soma_tam)/(arr_pag_aux.length * 2))+5 ;
								
                for(var f in arr_pag_aux){
                    $(arr_pag_aux[f]).css({
                        'padding-left' : soma_tam+'px' ,
                        'padding-right' : soma_tam+'px'
                    });
                }
                if(cont_page==1){
                    qnt_first=arr_pag_aux.length;
                }
                cont_page++;
                arr_pag_aux = new Array();
                arr_pag_aux.push($(this));
                tmv=tm-$(this).outerWidth();
            }
        });

        qnt_last=arr_pag_aux.length;
			  
        soma_tam=0;
        for(var f in arr_pag_aux){
            soma_tam = soma_tam + $(arr_pag_aux[f]).outerWidth();
        }
				   
        var t =  soma_tam;
        soma_tam=((tm-soma_tam)/(arr_pag_aux.length * 2))+5 ;
			
        for(var f in arr_pag_aux){
            $(arr_pag_aux[f]).css({
                'padding-left' : soma_tam+'px' ,
                'padding-right' : soma_tam+'px'
            });
        }
        // singleWidth = $single.outerWidth(),
        //   visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
        currentPage = 1,
        pages = cont_page;//Math.ceil($items.length / visible);


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        //  if (($items.length % visible) != 0) {
        //      $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
        //      $items = $slider.find('> li');
        //   }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- qnt_last).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, qnt_first).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(tm);
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
            n = Math.abs(currentPage - page),
            left =  dir * tm * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 1000, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(tm * pages );
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(tm);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back"></a><a class="arrow forward"></a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
		
        $(this).bind('next', function () {
            gotoPage(currentPage + 1);
        });
    });  
};
