var i = 0; 
var iFiltro = 0; 
var temporizador, tempEfeito; 

// Variaveis de controle
var img = new Array();
var titulos = new Array();
var descricoes = new Array();
var urls = new Array();
var tempoTransicao = 10000;

//Tamanho ideal para as imagens: 250 x 170 pixels;
//Para adicionar novas imagens/textos, basta seguir o padr�o dos arrays abaixo:


// FUNCAO PARA CARREGAR AS OFERTAS EM DESTAQUE
function carregaDestaque() {
    var xmlReq = createXmlRequest();
   
    // abre a conexao com o servlet
    xmlReq.open("GET", "/xml/vitrine_home.jsp", true);
    xmlReq.onreadystatechange = function() {
        if (isAjax(xmlReq)) {
             parseDestaques(xmlReq);
        }    
    };    
    
    xmlReq.send(null);
    
    
   
}

// Carrega os vetores com os destaques
function parseDestaques(xml) {
    var conteudo = xml.responseXML;
    var noticias = conteudo.getElementsByTagName("noticia");
    
    for (var j = 0; j < noticias.length; j++) {
        var noticia = noticias[j];
                        
        titulos[j]      = noticia.getElementsByTagName("titulo")[0].firstChild.data;
        img[j]          = noticia.getElementsByTagName("foto")[0].firstChild.data;
        descricoes[j]   = noticia.getElementsByTagName("descri")[0].firstChild.data;
        urls[j]         = noticia.getElementsByTagName("url")[0].firstChild.data;
    }    
    
    efeitoInicial();
    slideShow();
}    

//Fun��o para exibir os slides
function slideShow() {
        //Localizando os objetos no HTML:
        
      var objLink = document.getElementById("link_news")
      var objImg = document.getElementById("imgnews");
      var objTit = document.getElementById("titNews");
      var objDetail = document.getElementById("detailNews");
    
            
        //Criando apenas um controle, caso o valor do id seja maior do que os slides:
        if (i >= img.length) {
                i = 0;
        }
        
        //Controle dos estilos, para FF e IE:   
        objImg.style.opacity = ".0";
        objImg.style.filter = "alpha(opacity=0)";
        objImg.removeAttribute("onclick");
        objImg.setAttribute("title", urls[i]);
        objImg.setAttribute("alt", urls[i]);
        objImg.src = img[i];
           
        //Imprimindo a imagen e textos na tela:
        objLink.removeAttribute("onclick");
        objLink.setAttribute("onclick","javascript:irLink('"+urls[i]+"');");
        
        objTit.innerHTML = titulos[i];
        objDetail.innerHTML = descricoes[i];
        
        
        //Chamando a fun��o respons�vel pelos efeitos nos slides:
        efeito();
        efeitoDiv(i);
       
        //Aumentando o i de 1 em 1;
        i++;

        //Criando um temporizador para ajustar o tempo de transi��o entre os slides:
        temporizador = window.setTimeout('slideShow()',tempoTransicao);
}

//Fun��o que gera o efeito de transi��o entre os slides
function efeito() {
        var objImg = document.getElementById("imgnews");
        //Controle para as v�rias possibilidades do i:
        
        if (iFiltro > 100) {
                clearInterval(tempEfeito);
                iFiltro = 0;
        }else{
         valorOpacidade = iFiltro/100;
         valorOpacidade = valorOpacidade.toString();
                objImg.style.opacity = valorOpacidade;
                objImg.style.filter = "alpha(opacity="+iFiltro+")";
                iFiltro+=10;
                tempEfeito = window.setTimeout('efeito()',70);
        }
}


function irLink(url){
    window.open(url, "Janela","width=800,height=600,status=yes,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes");
    
}

//Controle dos bot�es: ANTERIOR, PAUSA e POSTERIOR:
function controle(c) {
        i = c;
        
        if (i > img.length) {
            i = img.length -1 ;
        }    
        
        efeitoDiv(i);
        
        clearInterval(temporizador);
        clearInterval(tempEfeito); 
        iFiltro = 0;
        
        slideShow();
        
}


function efeitoDiv(c){
      for (var h = 0; h < 5; h++){
           document.getElementById("news_"+h).className = "normal";
      }    
        
      var objdiv = document.getElementById("news_"+c);
      objdiv.className = "selecionado";
}

function efeitoInicial(){
    
    for (var g = 0; g < 5; g++) {
        document.getElementById("news_"+g).style.display = "none";
    }    
    
    for (var f = 0; f < img.length; f++){
        document.getElementById("news_"+f).style.display = "block";
    }    
    
}
