//---------------------------------------------------------------//
// Còdigo escrito por Sidney Benetti 
// Fevereiro / 2009
//
// Sistema para rotação de slides, quantidade fixa.
// Customizado para o site "encontrocomcristo.com.br"
//---------------------------------------------------------------//

var tempo;
var intervalo   = 5;                    //--- Quantos segundos cada slide é exibido
var maxSlide    = 20;                    //--- Máximo de slides exibidos - é alterado pelo código automaticamente
var thisSlide   = 1;                    //--- Slide atual
var adIDRoot    = 'capa_banner';        //--- Raiz ID para as divs que exibem os anúncios
var vetAds;
var timerOn     = 1;                    //--- Indica se o TIMER está ativado 
var timerCont   = 0;


//Função adaptada da original de Christian Heilmann, em 
//http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
function addEvent(obj, evType, fn) { 
    if (typeof obj == "string") {
        if (null == (obj = document.getElementById(obj))) {
            throw new Error("Elemento HTML não encontrado. Não foi possível adicionar o evento.");
        }
    }

    if (obj.attachEvent) {
        return obj.attachEvent(("on" + evType), fn);
    } else if (obj.addEventListener) {
        return obj.addEventListener(evType, fn, true);
    } else {
        throw new Error("Seu browser não suporta adição de eventos.");
    }
}

            
function init() {
    vetAds = new Array(maxSlide);
    var maxOk = 0;
    for(i=1; i<=maxSlide; i++) {
        vetAds[i-1] = document.getElementById(adIDRoot + i);
        //--- Desativa os blocos inexistentes
        if (vetAds[i-1] == undefined) {

        } else {
            maxOk++;
        }
    }
    maxSlide = maxOk;

    //--- Roda o intervalo a cada segundo
    tempo = setInterval('passaSlide()', 1000);
}


function passaSlide() {
    if (timerOn == 1) {
        timerCont++;
        if (timerCont >= intervalo) {            
            thisSlide ++;
            if (thisSlide > maxSlide) { thisSlide = 1; }
            exibeSlide(thisSlide);
            timerCont = 0;
        }
    }
}


function exibeSlide(numero) {
    var n = parseInt(numero);
    
    for(i=1; i<=maxSlide; i++) {
        vetAds[i-1].style.display = 'none';
    }
    
    vetAds[n-1].style.display = 'block';
    thisSlide = n;
}


function setTimerOn()   { timerOn = 1; timerCont = 0; }
function setTimerOff()  { timerOn = 0; }


addEvent(window, 'load', init);
