/*

    Empresa: InMeta Agência Digital
    Autor: Ismael Tiago Pereira
    Criado em 27/05/2010

    girar(
        trilho  : trilho,
        duracao : valor numérico em milisegundos (1000 = 1 segundo)
        direcao : 'anterior' ou 'proximo'
        numero  : valor numérico do carrossel que usará a função
    )

*/

$(document).ready(function(){

//    tempoGirar = 800; // Tempo que o carrossel leva para trocar de um vagão para o próximo ou anterior
//    tempoAguardar = 1200; // Tempo que o vagão fica parado quando o giro é automático

    var estacao = new Array(); // id ou class da estacao
    var tempoGirar = new Array(); // Tempo que o carrossel leva para trocar de um vagão para o próximo ou anterior
    var tempoAguardar = new Array(); // Tempo que o vagão fica parado quando o giro é automático
    var btPause = new Array(); // "src" do botão pause
    var btPlay = new Array(); // "src" do botão play
    var clique = new Array(); // Padrão inicia false
    var girando = new Array(); // Padrão inicia false
    var iniciaGirando = new Array(); // true = carrossel inicia girando ou false = carrossel inicia parado
    
    estacao[0] = '#estacao_cases';
    tempoGirar[0] = 1300;
    tempoAguardar[0] = 5000;
    btPause[0] = IMAGE_PATH+'bt/cross_content_interno_pause.png';
    btPlay[0] = IMAGE_PATH+'bt/cross_content_interno_play.png';
    iniciaGirando[0] = true;

    estacao[1] = '#estacao_clientes';
    tempoGirar[1] = 1300;
    tempoAguardar[1] = 5000;
    btPause[1] = IMAGE_PATH+'bt/cross_content_interno_pause.png';
    btPlay[1] = IMAGE_PATH+'bt/cross_content_interno_play.png';
    iniciaGirando[1] = false;

    estacao[2] = '#estacao_parceiros';
    tempoGirar[2] = 1300;
    tempoAguardar[2] = 5000;
    btPause[2] = IMAGE_PATH+'bt/cross_content_interno_pause.png';
    btPlay[2] = IMAGE_PATH+'bt/cross_content_interno_play.png';
    iniciaGirando[2] = false;

    estacao[3] = '#estacao_master';
    tempoGirar[3] = 1300;
    tempoAguardar[3] = 5000;
    btPause[3] = IMAGE_PATH+'bt/cross_content_interno_pause.png';
    btPlay[3] = IMAGE_PATH+'bt/cross_content_interno_play.png';
    iniciaGirando[3] = false;


    qtdEstacao = estacao.length;

    // Ao carregar a página o carrossel envia um 'vagao' para frente do 'trem' e o arrasta para frente na mesma largura do 'vagao'
    $('.trem').each(function(){
        if($(this).children().length > 2){
            var elementoClonado = $(this).children('.vagao:last').clone();
            $(this).prepend(elementoClonado);
            $(this).children('.vagao:last').remove();
            var larguraVagao = $(this).children('.vagao').width() + parseInt($(this).children('.vagao').css('margin-right'));
            $(this).css('margin-left','-'+(larguraVagao)+'px');
        }
    });

    // inicialização de variáveis
    girando = new Array();
    var giroParado = new Array();
    var carrossel = new Array();

    // 00000000000000000000000000000000000000000000000000000000
    if(iniciaGirando[0]){
        carrossel[0] = window.setInterval(function(){
            clique[0] = false;
            $('.bt_pause',estacao[0]).attr('src', btPause[0]);
            girar($(estacao[0]), tempoGirar[0], 'proximo', 0)
        }, tempoAguardar[0]);
    }else{
        $('.bt_pause',estacao[0]).attr('src', btPlay[0]);
    }

    
    $('.bt_pause',estacao[0]).click(function(){
        clique[0] = false;
        if(!giroParado[0]){
            $(this).attr('src', btPlay[0]);
            clearInterval(carrossel[0]);
            giroParado[0] = true;
            girando[0] = false;
        }else{
            $(this).attr('src', btPause[0]);
            carrossel[0] = window.setInterval(function(){
                girar($(estacao[0]), tempoGirar[0], 'proximo', 0);
            }, tempoAguardar[0]);
            giroParado[0] = false;
        }
        
    });
    
    $('.bt_proximo',estacao[0]).click(function(){
        if(!giroParado[0] && !girando[0]){
            $('.bt_pause',estacao[0]).attr('src', btPlay[0]);
            clearInterval(carrossel[0]);
            giroParado[0] = true;
            girando[0] = false;
        }
        clique[0] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[0], 'proximo', 0);
    });
    

    $('.bt_anterior',estacao[0]).click(function(){
        if(!giroParado[0] && !girando[0]){
            $('.bt_pause',estacao[0]).attr('src', btPlay[0]);
            clearInterval(carrossel[0]);
            giroParado[0] = true;
            girando[0] = false;
        }
        clique[0] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[0], 'anterior', 0);
    });


    // 11111111111111111111111111111111111111111111111111111111
    if(iniciaGirando[1]){
        carrossel[1] = window.setInterval(function(){
            clique[1] = false;
            $('.bt_pause',estacao[1]).attr('src', btPause[1]);
            girar($(estacao[1]), tempoGirar[1], 'proximo', 1)
        }, tempoAguardar[1]);
    }else{
        $('.bt_pause',estacao[1]).attr('src', btPlay[1]);
    }


    $('.bt_pause',estacao[1]).click(function(){
        clique[1] = false;
        if(!giroParado[1]){
            $(this).attr('src', btPlay[1]);
            clearInterval(carrossel[1]);
            giroParado[1] = true;
            girando[1] = false;
        }else{
            $(this).attr('src', btPause[1]);
            carrossel[1] = window.setInterval(function(){
                girar($(estacao[1]), tempoGirar[1], 'proximo', 1);
            }, tempoAguardar[1]);
            giroParado[1] = false;
        }
    });

    $('.bt_proximo',estacao[1]).click(function(){
        if(!giroParado[1] && !girando[1]){
            $('.bt_pause',estacao[1]).attr('src', btPlay[1]);
            clearInterval(carrossel[1]);
            giroParado[1] = true;
            girando[1] = false;
        }
        clique[1] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[1], 'proximo', 1);
    });


    $('.bt_anterior',estacao[1]).click(function(){
        if(!giroParado[1] && !girando[1]){
            $('.bt_pause',estacao[1]).attr('src', btPlay[1]);
            clearInterval(carrossel[1]);
            giroParado[1] = true;
            girando[1] = false;
        }
        clique[1] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[1], 'anterior', 1);
    });

    // 22222222222222222222222222222222222222222222222222222222
    if(iniciaGirando[2]){
        carrossel[2] = window.setInterval(function(){
            clique[2] = false;
            $('.bt_pause',estacao[2]).attr('src', btPause[2]);
            girar($(estacao[2]), tempoGirar[2], 'proximo', 2)
        }, tempoAguardar[2]);
    }else{
        $('.bt_pause',estacao[2]).attr('src', btPlay[2]);
    }


    $('.bt_pause',estacao[2]).click(function(){
        clique[2] = false;
        if(!giroParado[2]){
            $(this).attr('src', btPlay[2]);
            clearInterval(carrossel[2]);
            giroParado[2] = true;
            girando[2] = false;
        }else{
            $(this).attr('src', btPause[2]);
            carrossel[2] = window.setInterval(function(){
                girar($(estacao[2]), tempoGirar[2], 'proximo', 2);
            }, tempoAguardar[2]);
            giroParado[2] = false;
        }

    });

    $('.bt_proximo',estacao[2]).click(function(){
        if(!giroParado[2] && !girando[2]){
            $('.bt_pause',estacao[2]).attr('src', btPlay[2]);
            clearInterval(carrossel[2]);
            giroParado[2] = true;
            girando[2] = false;
        }
        clique[2] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[2], 'proximo', 2);
    });


    $('.bt_anterior',estacao[2]).click(function(){
        if(!giroParado[2] && !girando[2]){
            $('.bt_pause',estacao[2]).attr('src', btPlay[2]);
            clearInterval(carrossel[2]);
            giroParado[2] = true;
            girando[2] = false;
        }
        clique[2] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[2], 'anterior', 2);
    });





    // 3333333333333333333333333333333333333333333333
    if(iniciaGirando[3]){
        carrossel[3] = window.setInterval(function(){
            clique[3] = false;
            $('.bt_pause',estacao[3]).attr('src', btPause[3]);
            girar($(estacao[3]), tempoGirar[3], 'proximo', 3)
        }, tempoAguardar[3]);
    }else{
        $('.bt_pause',estacao[3]).attr('src', btPlay[3]);
    }


    $('.bt_pause',estacao[3]).click(function(){
        clique[3] = false;
        if(!giroParado[3]){
            $(this).attr('src', btPlay[3]);
            clearInterval(carrossel[3]);
            giroParado[3] = true;
            girando[3] = false;
        }else{
            $(this).attr('src', btPause[3]);
            carrossel[3] = window.setInterval(function(){
                girar($(estacao[3]), tempoGirar[3], 'proximo', 3);
            }, tempoAguardar[3]);
            giroParado[3] = false;
        }

    });

    $('.bt_proximo',estacao[3]).click(function(){
        if(!giroParado[3] && !girando[3]){
            $('.bt_pause',estacao[3]).attr('src', btPlay[3]);
            clearInterval(carrossel[3]);
            giroParado[3] = true;
            girando[3] = false;
        }
        clique[3] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[3], 'proximo', 3);
    });


    $('.bt_anterior',estacao[3]).click(function(){
        if(!giroParado[3] && !girando[3]){
            $('.bt_pause',estacao[3]).attr('src', btPlay[3]);
            clearInterval(carrossel[3]);
            giroParado[3] = true;
            girando[3] = false;
        }
        clique[3] = true;
        var trilho = ($(this).parent().parent());
        girar(trilho, tempoGirar[3], 'anterior', 3);
    });


    


    //var girando = false;
    function girar(trilho, duracao, direcao, numero){
        if(clique[numero]){
            //alert(girando[numero]);
        }
        if((girando[numero]) && (clique[numero])) {
            return false;
        }
        girando[numero] = true;
        var trem = (trilho).children('.trilho');
        
        var quantidade = trem.children('.trem').children('.vagao').length;

        if(quantidade == 1){
            return false;
        }

        var larguraVagao = trem.children('.trem').children('.vagao').width() + parseInt(trem.children('.trem').children('.vagao').css('margin-right')) + parseInt(trem.children('.trem').children('.vagao').css('margin-left'));
        
        var margemEsquerdaTrem = parseInt($(trem.children('.trem')).css('margin-left'));

        var sinal = '+';
        if(direcao == 'proximo'){
            sinal = '-';
        }

        var elementoClonado = '';
        var larguraTrem = 0;

        if((direcao == 'proximo') && (quantidade > 2)){
            elementoClonado = $(trem.children('.trem')).children('.vagao:first').clone();
            $(trem.children('.trem')).append(elementoClonado);
            $(trem.children('.trem')).children('.vagao:first').remove();
            larguraTrem = parseInt($(trem.children('.trem')).css('margin-left'));
            $(trem.children('.trem')).css('margin-left',(larguraVagao+larguraTrem)+'px');
        }
        
        if(direcao == 'anterior'){
            elementoClonado = $(trem.children('.trem')).children('.vagao:last').clone();
            $(trem.children('.trem')).prepend(elementoClonado);
            $(trem.children('.trem')).children('.vagao:last').remove();
            larguraTrem = parseInt($(trem.children('.trem')).css('margin-left'));
            $(trem.children('.trem')).css('margin-left','-'+(larguraVagao * 2)+'px');
        }
        
        $(trem.children('.trem')).animate({
            marginLeft: sinal+'='+larguraVagao+'px'
        },
        {
            duration: duracao,
            complete: function(){
                if(clique[numero]){
                    girando[numero] = false;
                }
                if((direcao == 'proximo') && (quantidade == 2)){
                    elementoClonado = $(trem.children('.trem')).children('.vagao:first').clone();
                    $(trem.children('.trem')).append(elementoClonado);
                    $(trem.children('.trem')).children('.vagao:first').remove();
                    larguraTrem = parseInt($(trem.children('.trem')).css('margin-left'));
                    $(trem.children('.trem')).css('margin-left',(larguraVagao+larguraTrem)+'px');
                }
            }
        }
        );
            
        return true;
    }

    
});
