//Setup
var cacheImage;
var cacheTime;
var scrollStep=5;
var timerLeft="";
var timerRight="";

function verificaCarregamento(obj,txtLegenda,index,totalThumb) {
	if(cacheImage.complete)    {        

        clearTimeout(cacheTime);

        //Determinando medidas
        if (cacheImage.height>=350) {
            document.getElementById(obj).style.height = cacheImage.height+'px';
        }
        else {
            document.getElementById(obj).style.height = '350px';
        }
        
        document.getElementById(obj).style.background='url(' + cacheImage.src + ') no-repeat #000000';
        
        //Posicionando a legenda
        document.getElementById(obj+'Legenda').innerHTML = txtLegenda;
        
        var posHori=0;
        posHori = ((630 - cacheImage.width)/2);
        document.getElementById(obj).style.backgroundPosition=posHori+'px 0px';
        
        if (index>0) {
            //Limpando seleção anterior
            clearThumbSel('boxThumbAlbum',totalThumb);
            
            //Definindo o box de seleção do thumb
            document.getElementById('boxThumbAlbum'+index).className='imagemThumbAlbum imagemThumbAlbumSel';
        }
	}    
	else    {        
		cacheTime = setTimeout("verificaCarregamento('" + obj + "','" + txtLegenda + "'," + index + "," + totalThumb + ")", 100 );
	}
}

function trocaImagem(obj,srcImage,txtLegenda,index,totalThumb) {
    //Verificando a necessidade de trocar a imagem
    if (document.getElementById(obj).style.background.indexOf(srcImage)<0) {
    
        //Colocando em cache
        cacheImage = new Image();
        cacheImage.src = srcImage;

        //Carregando...
        document.getElementById(obj).style.background='url(/canais/redacao/surfaceDigital/images/carregando.gif) no-repeat #000000 115px';
        document.getElementById(obj).style.height='350px';
    	
	    cacheTime = setTimeout("verificaCarregamento('" + obj + "','" + txtLegenda + "'," + index + "," + totalThumb + ")", 100 );
    
    }
}

function preLoadImages(lstImagem)  {
    var myimages = new Array();
    for (x=0; x<preLoadImages.arguments.length; x++) {
        myimages[x] = new Image();
        myimages[x].src = preLoadImages.arguments[x];
    }
}

function clearThumbSel(obj,totalThumb) {
    for(i=1; i<=totalThumb; i++) {
        if (document.getElementById(obj+i)!=null) {
            document.getElementById(obj+i).className='imagemThumbAlbum';
        }
    }
}

function scrollDivLeft(id,scrollLimitMax){
    if (document.getElementById(id).scrollLeft<=scrollLimitMax) {
        document.getElementById(id).scrollLeft+=scrollStep;
        timerRight=setTimeout("scrollDivLeft('"+id+"'," + scrollLimitMax + ")",10);
    }
}

function scrollDivRight(id,scrollLimitMin){
    if (document.getElementById(id).scrollLeft>=scrollLimitMin) {
        document.getElementById(id).scrollLeft-=scrollStep;
        timerLeft=setTimeout("scrollDivRight('"+id+"'," + scrollLimitMin + ")",10);
    }
}

function scrollDivUp(id,scrollLimitMax){
    if (document.getElementById(id).scrollTop<=scrollLimitMax) {
        document.getElementById(id).scrollTop+=scrollStep;
        timerLeft=setTimeout("scrollDivUp('"+id+"'," + scrollLimitMax + ")",10);
    }
}

function scrollDivDown(id,scrollLimitMin){
    if (document.getElementById(id).scrollTop>=scrollLimitMin) {
        document.getElementById(id).scrollTop-=scrollStep;
        timerLeft=setTimeout("scrollDivDown('"+id+"'," + scrollLimitMin + ")",10);
    }
}

function stopScroll() {
    clearTimeout(timerRight);
    clearTimeout(timerLeft);
}

function paginar(url,QtdImagemExibic,pagina,criterioPesq,organizarPesq) {
    document.getElementById('ExibirPagina').value=pagina;
    document.getElementById('go').action = url;
    document.getElementById('go').submit();
}

function valida() {
    if (document.getElementById('txtBusca').value=="" || document.getElementById('txtBusca').value=="Localizar imagem") {
        alert('Preencha o campo de busca');
        document.getElementById('txtBusca').focus();
        return false;
    }
    
    document.getElementById('go').submit();
}

