
var timer;
var interval=3000; /* time in ms */
var currentIndex;
var lastIndex;
var destaques
var failCount;
var maxFail=10;

function initDestaques() {
    failCount=0;
    timer=null;
    
    
    $(".destaque_selectors").hide();
    
    $(".destaque_selectors").click(function() {
        var elemId = this.id;
        var elemIndex = elemId.substr(elemId.length-1,1);
        
        gotoPicture(elemIndex);
    });

    
    $("#destaqueAnterior").click(prevPicture);
    $("#destaqueProximo").click(nextPicture);
    $("#destaqueParar").click(switchStatus);
    
    $("#destaqueAnterior").mouseover(function() {
        this.src="img/seta_esq_destaque_over.gif";
    });
    $("#destaqueAnterior").mouseout(function() {
        this.src="img/seta_esq_destaque.gif";
    });
    
    $("#destaqueProximo").mouseover(function() {
        this.src="img/seta_dir_destaque_over.gif";
    });
    $("#destaqueProximo").mouseout(function() {
        this.src="img/seta_dir_destaque.gif";
    });
    
    
    fetchDestaques();
}

$(document).ready(function(){
            initDestaques();
});

function fetchDestaques() {
    jQuery.ajax( { url: "destaques.php",
                    success: onFetchDestaquesOK,
                    error: onFetchDestaquesFail,
                    context:document.body,
                    dataType: "xml"} )
}

function onFetchDestaquesOK(data, textStatus, XMLHttpRequest) {
    
    setIndex(0);
    destaques = new Array();
    $('info',data).each(function(i) {
        
            $("#destaque"+destaques.length).show();
        
            destaques.push({url: $(this).attr("url"),
                src: $(this).attr("src"),
                local: $(this).find("local").text(),
                titulo: $(this).find("titulo").text(),
                texto: $(this).find("texto").text()
            });
            
        });
    nextPicture();
    startSlideShow();
}


/* try again...   */
function onFetchDestaquesFail(XMLHttpRequest, textStatus, errorThrown) {

    failCount++;
    if(failCount <maxFail) {
        fetchDestaques();
    }
    else {
        alert("Os Destaques não puderam ser mostrados.");
    }
}


function startSlideShow() {
    stopSlideShow();
    $("#destaqueParar").each(function() { this.src="img/pause_destaque.gif"});
    timer = setInterval(nextPicture, interval);
}

function stopSlideShow() {
    if(timer != null) {
        clearInterval(timer);
        timer=null;
    }
    $("#destaqueParar").each(function() { this.src="img/pause_destaque_on.gif"});
}

function switchStatus() {
    
    if(null == timer) {
        startSlideShow();
    }
    else {
        stopSlideShow();
    }
}

function hidePanel() {
    $("#destaques").css("display","none");
    $(".destaquescoluna").html("ESPAÇOS");
}

function showPanel() {
    $("#destaques").css("display","block");
    $(".destaquescoluna").html("DESTAQUES");
}


function nextPicture() {
    if(null == destaques || 0 == destaques.length) {
        stopSlideShow();
        hidePanel();
        return;
    }
    else {
        showPanel();
    }
    $('#foto_destaque').attr("src",destaques[currentIndex].src);
    $('#foto_destaque_link').attr("href",destaques[currentIndex].url);
    showIndicator(currentIndex);
    
    $("#localDestaque").each(function() {
        
        this.innerHTML = destaques[currentIndex].local;
    });
    
    $("#tituloDestaque").each(function() {
        this.innerHTML = destaques[currentIndex].titulo;
    });

    
    $("#textoDestaque").each(function() {
        this.innerHTML = destaques[currentIndex].texto;
    });

                    
    
    nextIndex();
    
}

function prevPicture() {
    if(null == destaques || 0 == destaques.length) {
        return;
    }
    
    prevIndex();
    
    $('#foto_destaque').attr("src",destaques[currentIndex].src);
    showIndicator(currentIndex);
}

function gotoPicture(index) {
    setIndex(index);
    nextPicture();
}



function showIndicator(index) {
     $("#destaque"+(index)).css("border","1px solid #000000");
     hideIndicator(lastIndex);
}

function hideIndicator(index) {
    if(null!=index && undefined != index) {
        $("#destaque"+(index)).css("border","none");    
    }
    
}



function setIndex(value) {
    currentIndex = value;
}

function nextIndex() {
    lastIndex = currentIndex;
    currentIndex++;
    if(currentIndex == destaques.length) {
        currentIndex = 0;
    }
}

function prevIndex() {
    lastIndex = currentIndex;
    currentIndex--;
    if(currentIndex < 0 ) {
        currentIndex = destaques.length - 1 ;
    }
}
