
var programaAtual = 'btcuca';
var seq  = 1 ;
var at = "";

$(document).ready(function(){
            
    $(".conteudo").fadeOut("fast");    
        
    var id;
    var programa;
    var css;    
    
    $("#cucaSof").fadeIn("fast");
    $("#btcuca").css("background","url(public/img/tab-s.png)");
    $("#btcuca").css("color","#fff");
    
    $(".botao-programa").click(function(){
        clearTimeout(15000);
        programa = $(this).attr("id");

        if(programa != programaAtual){
            $(".conteudo").fadeOut("fast").stop(true,true);
        }        
        
        $(".botao-programa").css("background","none");
        $("#"+programaAtual).css("color","#000");
        
        switch(programa){
            case 'btcuca':
                seq = 2;
                $("#cucaSof").fadeIn("fast");                                
                break;
            case 'btdpcuca':
                seq = 3;
                $('#dpcuca').fadeIn("fast");
                break;
            case 'btgera':
                seq = 4;
                $('#issqn').fadeIn("fast");
                break;
            case 'btremoto':
                seq = 5;
                $('#remoto').fadeIn("fast");
                break;
            case 'btatendimento':
                seq = 6;
                $("#atendimento").fadeIn("fast");                
                break;
            case 'btimplantacao':
                seq = 7;
                $('#implantacao').fadeIn("fast");                
                break;
            case 'bttreinamento':
                seq = 1;
                $('#treinamento').fadeIn("fast")                
                break;
        }
        
        $(this).css("background","url(public/img/tab-s.png)");
        $(this).css("color","#fff");
        programaAtual = programa;
    });
      
    $(".botao-programa").mouseenter(function(){
        css = $(this).attr("id");
        if(css != programaAtual){
            $(this).css("background","url(public/img/tab-hover.png)");        
        }
    });
    
    $(".botao-programa").mouseleave(function(){                            
        if(css != programaAtual){
            $("#"+css).css("background","none");
        }        
    });
    
    pass = setInterval(nextPrograma, 15000);       
    
    $("#programa-conteudo .conteudo .video-produto .video").click(function(){
        var htm = $(this).html().trim();
        
        var endereco = htm.substr(htm.indexOf('<span>', this)+6,htm.indexOf("</span>", this)-(htm.indexOf('<span>', this)+6));        
        var titulo = htm.substr(htm.indexOf('<h1>', this)+4,htm.indexOf("</h1>", this)-(htm.indexOf('<h1>', this)+4));        
        var descricao = htm.substr(htm.indexOf('<h2>', this)+4,htm.indexOf("</h2>", this)-(htm.indexOf('<h2>', this)+4));        
        $("#video-demonstrativo").html('<object type="application/x-shockwave-flash" style="width:594px;height:340px;" data="'+endereco+'"><param name="allowFullScreen" value="true"></param><param name="movie" value="'+endereco+'" /></param><param name="wmode" value="transparent" /></object>');        
        $("#vitrine #conteudo-videos #janela-video #comentario-video #titulo-video").html(titulo);
        $("#vitrine #conteudo-videos #janela-video #comentario-video #texto-comentario").html(descricao);
        
        $("#escurecer-tela").fadeIn("fast");
        $("#vitrine #conteudo-videos").fadeIn("fast");
        $("#vitrine #conteudo-videos #janela-video").fadeIn("fast");                
    });

// Click no botao sair, fechar tela de videos
    $("#sair-videos").click(function(){
        $("#escurecer-tela").fadeOut("fast");
        $("#vitrine #conteudo-videos").fadeOut("fast");                
        $("#vitrine #conteudo-videos #janela-video").fadeOut("fast");                
    });
        
// Tecla ESC, fechar tela de videos
   $(document).keydown(function(e){
       if(e.which == 27){
            $("#escurecer-tela").fadeOut("fast");
            $("#vitrine #conteudo-videos").fadeOut("fast");                
            $("#vitrine #conteudo-videos #janela-video").fadeOut("fast");                
       }
   });

// Click no fundo escuro, fechar tela de videos
    $("#escurecer-tela").click(function(){
        $("#escurecer-tela").fadeOut("fast");
        $("#vitrine #conteudo-videos").fadeOut("fast");                
        $("#vitrine #conteudo-videos #janela-video").fadeOut("fast");                
    });
    
//    Botao para esconder e mostrar outros videos
    $("#botao-retrair").click(function(){
        if($("#janela-video").css('width') == '680px'){               
            $("#janela-video").animate({width: '980px'},500,function(){                
                $("#videos-relacionados").fadeIn("slow");                
            });
            
            $("#botao-retrair img").attr('src', 'public/img/setaEsq.png');
        }else{
            $("#videos-relacionados").fadeOut("fast",function(){
                $("#janela-video").animate({width: '680px'},500);
            });
            
            $("#botao-retrair img").attr('src', 'public/img/setaDir.png');
        }        
    });
    
    $(".balao-videos").click(function(){
        var htm = $(this).html();
        var endereco = htm.substr(htm.indexOf("<span>", this)+6,htm.indexOf("</span>", this)-(htm.indexOf("<span>", this)+6));        
        var titulo = htm.substr(htm.indexOf('<div class="titulo-balao">', this)+26,htm.indexOf('</div>', this)-(htm.indexOf('<div class="titulo-balao">', this)+26));        
        var descricao = htm.substr(htm.indexOf('<h2>', this)+4,htm.indexOf('</h2>', this)-(htm.indexOf('<h2>', this)+4));
        
        $("#video-demonstrativo").html('<object type="application/x-shockwave-flash" style="width:594px;height:340px;" data="'+endereco+'"><param name="movie" value="'+endereco+'" /><param name="wmode" value="transparent" /></object>');
        $("#vitrine #conteudo-videos #janela-video #comentario-video #titulo-video").html(titulo);
        $("#vitrine #conteudo-videos #janela-video #comentario-video #texto-comentario").html("<p>"+descricao+"</p>");
    });    
});

seq = 2;
function nextPrograma(pr){
    $(".conteudo").fadeOut("fast").stop(true,true);
    $(".botao-programa").css("background","none");
    $(".botao-programa").css("color","#000");

    at = $(".botao-programa:nth-child("+seq+")").attr("id");   
    seq++;
    if(seq>=8){
        seq = 1;
    }
    
    $("#"+at).css("background","url(public/img/tab-s.png)");
    $("#"+at).css("color","#fff");
    
    switch(at){
            case 'btcuca':
                $("#cucaSof").fadeIn("fast");                                
                break;
            case 'btdpcuca':
                $('#dpcuca').fadeIn("fast");
                break;
            case 'btgera':
                $('#issqn').fadeIn("fast");
                break;
            case 'btremoto':
                $('#remoto').fadeIn("fast");
                break;
            case 'btatendimento':
                $("#atendimento").fadeIn("fast");                
                break;
            case 'btimplantacao':
                $('#implantacao').fadeIn("fast");                
                break;
            case 'bttreinamento':
                $('#treinamento').fadeIn("fast")                
                break;
    }            
    programaAtual = at;    
}

