var fotos = new Array(), fotosthumbs = new Array(), descricoes = new Array(), autores = new Array(), linkcarrega =new Array(), exibir = 0, thumbs = 0, vazio = '';

window.onload = function(){MM_preloadImages('/galerias/geradas/imagens/ajax-loader.gif')};
window.onload = function(){MM_preloadImages(fotosthumbs)};  
//window.onload = function(){MM_preloadImages(fotos)};      

var objInter = self.setInterval("imgAvancar()", 12000);
//12000

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function carregaThumb(mostra){             
	var i, mostrainicial = mostra;
	for (i=0;i<fotos.length;i++){	  
	    if(mostra > fotos.length-1){      
			mostra = 0;
		}     
		document.getElementById('thumbs'+i).src = fotosthumbs[mostra];
		eval("document.getElementById('thumbs"+i+"').onclick=function(){exibir="+mostra+";loadImage("+mostra+");carregaThumb("+mostra+");carregaClasse("+i+");}");
		document.getElementById('thumbs'+i).className = vazio;       
		mostra++;	
	}
	document.getElementById('thumbs0').className = 'selecionada';  
}        

function carregaClasse(contador){         
	paraFoto();
	for (i=0;i<fotos.length;i++){        
		document.getElementById('thumbs'+i).className = vazio;  
	}   
	//document.getElementById('thumbs'+contador).className = 'selecionada';
	document.getElementById('thumbs0').className = 'selecionada';
}    

function imgVoltar(){      
	if (exibir > 0){                  
		exibir--; 
		carregaThumb(exibir);            
		loadImage(exibir);
	}             
	else{       
		exibir= fotos.length-1;        
		carregaThumb(exibir);
		loadImage(exibir);
	}            
}      
  
function imgAvancar(){
	if (exibir < fotos.length-1){                    
		exibir++;      
		carregaThumb(exibir);
		loadImage(exibir);
	}       
	else {   
		exibir=0;
		carregaThumb(exibir);   
		loadImage(exibir);
	}
}        

function loadImage(contador){ 
	document.getElementById('recebe-foto').innerHTML='<div id="loadc" style="padding-top:77px;"><img src="/galerias/geradas/imagens/ajax-loader.gif" /></div>';
	var loader = new ImageLoader(fotos[contador]);
	loader.loadEvent = function(url, image){
		if (document.getElementById('loadc')){
			document.getElementById('loadc').style.display="none";
		}
		removeChilds('recebe-foto');
		image.style.cursor = "pointer";
		//eval("image.onclick=function(){document.location='"+linkcarrega[contador]+"';}");
		eval("image.onclick=function(){window.open('"+linkcarrega[contador]+"','_blank');}");
		document.getElementById('recebe-foto').appendChild(image);
	}
	loader.load();
	document.getElementById('legenda').innerHTML= descricoes[contador];  
	document.getElementById('credito').innerHTML= autores[contador]; 
}  
 
function paraFoto(){
	objInter=window.clearInterval(objInter);
}

function addListener(element, type, expression, bubbling){
  bubbling = bubbling || false;
  if(window.addEventListener)	{ // Standard
    element.addEventListener(type, expression, bubbling);
    return true;
  } else if(window.attachEvent) { // IE
    element.attachEvent('on' + type, expression);
    return true;
  } else return false;
}

var ImageLoader = function(url){
  this.url = url;
  this.image = null;
  this.loadEvent = null;
};

ImageLoader.prototype = {
  load:function(){
    this.image = document.createElement('img');
    var url = this.url;
    var image = this.image;
    var loadEvent = this.loadEvent;
    addListener(this.image, 'load', function(e){
      if(loadEvent != null){
        loadEvent(url, image);
      }
    }, false);
    this.image.src = this.url;
  },
  getImage:function(){
    return this.image;
  }
};

function removeChilds(id){
	var element = document.getElementById(id);
	while (element.lastChild)
		element.removeChild(element.lastChild);
	element=null;
}