﻿
var indice = -1;


var tempo;

function carregarImagem(url, legenda) {
    //alert(indice);
    var img = new Image();
    $('#slideCarregamento')
    .children(0).fadeOut()
    .empty();

     $('#legenda')
    .fadeOut()
    .val("");

     $(img)
    .load(function() {
        $(this).hide();
        $('#slideCarregamento')
        .empty()
        .append(this);

        $(this).fadeIn();

                
        $('#legenda')
        .html(legenda)
        .fadeIn();

        tempo = setTimeout(function() {
            avancar();
        }, 5000);
    })

  .attr('src', url);

}
function avancar() {
    
    indice++;
       
    if (indice == imagens.length) {
        indice = 0;
    }
    
    carregarImagem(imagens[indice], legendas[indice]);
}
function voltar() {
    indice--;
    if (indice < 0) {
        indice = imagens.length -1;
    }
    carregarImagem(imagens[indice], legendas[indice]);
}
function irPara(i) {
    clearTimeout(tempo);
    indice = i;
    if (indice < 0) {
        indice = imagens.length - 1;
    }
    if (indice == imagens.length) {
        indice = 0;
    }
    
    carregarImagem(imagens[indice], legendas[indice]);
}


$(function() {

    avancar();

    $('#avancar').click(function() { clearTimeout(tempo); avancar(); return false; });
    $('#avancar').hide();

    $('#voltar').click(function() { clearTimeout(tempo); voltar(); return false; });
    $('#voltar').hide();
    

    $('#slide').hover(
        function() {
            $("#avancar").fadeIn();
            $("#voltar").fadeIn();
        },
        function() {

            $("#avancar").fadeOut();
            $("#voltar").fadeOut();

        });



});
