/**
 * funcoes para cobertura 
 */
function checkFotoHash(){
	hshf=window.location.hash;
	fotoarr=hshf.split("#");
	foto=fotoarr[1];
	if(foto>=0)
		if(document.getElementById('formFotoIdx').value!=foto){
			pag = document.getElementById("paginaAtual");
			pagina = parseInt(foto/9)+1;
			paginaChangeFoto = 0;
			getCobPagina(pagina,0);
			getCobFoto(foto);
		}
}
function getCobPagina(pagina,anterior){
	var foto_fim = (pagina*thPagina)-1;
	var foto_ini = (foto_fim-thPagina)+1;
	numeroPagina = pagina;
	
	if(totalFotos%thPagina > 0) {
		paginaFinal = parseInt((totalFotos/thPagina)+1);
	} else {
		paginaFinal = totalFotos/thPagina;
	}
	
	for(i=0;i<totalFotos;i++){
		var f = document.getElementById("thumb"+fotos[i][0]);
		if(f)
			if(i>=foto_ini && i<=foto_fim) {
				f.style.display = "block";
			} else {
				f.style.display = "none";
			}
	}
	pag = document.getElementById("paginaAtual");

	if(pag)
		pag.innerHTML = pagina;			

	pagVoltar = document.getElementById("paginaVoltar");
	pagAvancar = document.getElementById("paginaAvancar");	
	pagVoltar.style.display = "inline";
	pagAvancar.style.display = "inline";	
	pagVoltar.onclick = function() {getCobPagina((pagina-1),0);}
	pagAvancar.onclick = function() {getCobPagina((pagina+1),0);}
		
	if(pagina == "1"){	
		pagVoltar.onclick = function() {void(null);}
	}
				
	if(pagina >= paginaFinal){	
		pagAvancar.onclick = function() {void(null);}
	}
		
	if(paginaChangeFoto)	
		if(anterior)
			getCobFoto(foto_fim);
		else
			getCobFoto(foto_ini);
	
	paginaChangeFoto = 1;
}
	
function getCobFoto(foto){
	document.getElementById('fotoCobertura').src = "../../../templates/site/default/image/p.gif";
	setTimeout( "changeCobFoto("+foto+");", 150 );
}

function changeCobFoto(foto){

	var foto_id = fotos[foto][0];
	var width = fotos[foto][2];
	var height = fotos[foto][3];
	var imagemSrc = fotos[foto][5];
	var nclicks = fotos[foto][6];
	var album = fotos[foto][7];
	var hash = hashes[foto][0];
	nclicks++;
	fotos[foto][6] = nclicks;
	if((foto-1) >= 0)
		var imagemAnteriorSrc = fotos[foto-1][5];
	if((foto+1) < totalFotos)
		var imagemProximaSrc = fotos[foto+1][5];

	if( width > 500 ) {
		widthOr = width;
		width = 500;
		height = width / (widthOr/height); 
	}

	
	document.getElementById('nomeFotografado').innerHTML = hashes[foto][1];
					
	if( hashes[foto][2] > 0 ) 
		trocarIconeAlbum();
	else
		trocarIconeAlbumIncluir();
	
	document.getElementById('formAlbum_id').value = hashes[foto][2];
	
	if( hashes[foto][3] == '1' ) {
		document.getElementById('liEstouFoto').className = "nafotoli-sel";
		document.getElementById('linkEstouFoto').className = "nafoto-sel";
	} else { 
		document.getElementById('liEstouFoto').className = "";
		document.getElementById('linkEstouFoto').className = "nafoto";
	}
			
	document.getElementById('nclicks').innerHTML = nclicks;
	document.getElementById('labelFoto_id').innerHTML = foto_id;
	document.getElementById('formFoto_id').value = foto_id;
	document.getElementById('formFotoIdx').value = foto;
	document.getElementById('imagemAtual').innerHTML = foto + 1;
	document.getElementById('formImageAlbum').value = album;
	
	document.getElementById('fotoDenuncia').onclick = function() {showFotoJanela( hash, 'Denuncia' );}
	document.getElementById('fotoFavorito').onclick = function() {showFotoJanela( hash,'Favorito' );}
	document.getElementById('fotoRecomendarEmail').onclick = function() {showFotoJanela( hash, 'RecomendarEmail' );}
	document.getElementById('fotoRecomendarUsuario').onclick = function() {showFotoJanela( hash, 'RecomendarUsuario' );}
	document.getElementById('urlFoto').value = urlFoto + foto_id;
	
	document.getElementById('div-foto').style.height = height+"px";
	document.getElementById('linkSalvarFoto').href = imagemSrc;
	document.getElementById('fotoCobertura').style.visibility = 'hidden';
	refreshThumbs(foto_id,cobertura_id,(foto+1),(foto-1),width,height);
	var corp = document.getElementById('corpoSite');
	corp.style.height = 'auto';
	
	setTimeout( "document.getElementById('fotoCoberturaAnterior').src = '"+imagemAnteriorSrc+"';", 100 );
	setTimeout( "document.getElementById('fotoCoberturaProxima').src = '"+imagemProximaSrc+"';", 100 );
	setTimeout( "document.getElementById('fotoCobertura').src = '"+imagemSrc+"';", 200 );
	setTimeout( "document.getElementById('fotoClk').src = '/scripts/shm_clk.php?f="+foto_id+"&d="+dm+"';", 100 );
	//document.getElementById('fotoCobertura').onload = function(){hideLoading();}

	document.getElementById('anterior').style.top = "-"+height+"px";
	document.getElementById('proxima').style.top = "-"+height+"px";
	document.getElementById('anterior').style.height = height+"px";
	document.getElementById('proxima').style.height = height+"px";
	if( (foto+1) < totalFotos ) {
		document.getElementById('proxima').style.display = "block";
	} else {
		document.getElementById('proxima').style.display = "none";
	}
	if( foto > 0 ) {
		document.getElementById('anterior').style.display = "block";
	} else {
		document.getElementById('anterior').style.display = "none";
	}
	//window.location.hash = "#" + foto;
		
	if(contadorBanner < changeMedia) {
		contadorBanner = contadorBanner + 1;
	}
	else {
		if(fotoHidden!=1) {
			getMidiasCobertura();
			contadorBanner = 0;
		} else {
			fotoHidden = 0;
		}
	}

}
	
function trocarIconeAlbum(){
	document.getElementById('fotoAlbum').title =  'Alterar dados no Álbum';
	document.getElementById('fotoAlbum').innerHTML =  'Alterar dados no &Aacute;lbum';
	document.getElementById('fotoAlbum').onclick = function() { xajax_changeFoto( xajax.getFormValues('formFoto'),'0' ); }
	document.getElementById('formAlbumAdd').value = "1";
}

function trocarIconeAlbumIncluir(){
	document.getElementById('fotoAlbum').title = 'Incluir no Álbum';
	document.getElementById('fotoAlbum').innerHTML = 'Incluir no &Aacute;lbum';
	document.getElementById('fotoAlbum').onclick = function(){ setHash(2);xajax_changeFoto( xajax.getFormValues('formFoto'),'1' ); }
	document.getElementById('formAlbumAdd').value = "0";
}
	
function setHash(idx) {
	foto = document.getElementById('formFotoIdx').value;
	hashes[foto][idx] = "1";
}	

function unsetHash(idx) {
	foto = document.getElementById('formFotoIdx').value;
	hashes[foto][idx] = "0";
}	

function updateAlbum(album_id){
	foto = document.getElementById('formFotoIdx').value;
	hashes[foto][2] = album_id;	
	document.getElementById('fotoAlbum').title =  'Alterar dados no Álbum';
	document.getElementById('fotoAlbum').innerHTML =  'Alterar dados no &Aacute;lbum';
}

function updateFotografado(){
	foto = document.getElementById('formFotoIdx').value;
	hashes[foto][1] = document.getElementById('nomeFotografado').innerHTML;	
}

function refreshThumbs(foto_id,cobertura_id,proximaFoto,anteriorFoto,largura,altura){
	var e=document.getElementsByTagName("a");
	for(var i=0;i<e.length;i++){
		if(e[i].className=='thumbMarcado'||e[i].className=='thumb') 
			e[i].className = 'thumb';
	}
	var at=findDOM('thumbImg'+foto_id);
	var prox = findDOM('proxima');
	var ant = findDOM('anterior');
	if (at!=null) {
		at.className = 'thumbMarcado';	
		if(proximaFoto!=null) {
			if(proximaFoto < totalFotos) {
				var pr=findDOM('thumb'+fotos[proximaFoto][0]);
				if (pr.style.display != "none") {
					proxAction = "getCobFoto("+proximaFoto+");";
					prox.onclick = function() {getCobFoto(proximaFoto);}
				}				
				else {
					proxAction = "getCobPagina(("+numeroPagina+"+1),0);";
					prox.onclick = function() {getCobPagina((numeroPagina+1),0);}			
				}
			}
		} 
		else {
			proxAction = "void(null);";
			prox.onclick = function() {void(null);}			
		}
		if(anteriorFoto!=null) {
			if(anteriorFoto >= 0) {
				var an=findDOM('thumb'+fotos[anteriorFoto][0]);
				if (an.style.display != "none") {
					antAction = "getCobFoto("+anteriorFoto+");";
					ant.onclick = function() {getCobFoto(anteriorFoto);}				
				} 
				else {
					antAction = "getCobPagina(("+numeroPagina+"-1),1);";
					ant.onclick = function() {getCobPagina((numeroPagina-1),1);}
				}
			}
		} 
		else {
			antAction = "void(null);";
			ant.onclick = function() {void(null);}			
		}
	}

	setTimeout( "document.getElementById('fotoCobertura').style.width = '"+ largura +"px';", 100 );
	setTimeout( "document.getElementById('fotoCobertura').style.height = '"+ altura +"px';", 100 );
	if(fotoHidden!=1)
		setTimeout( "document.getElementById('fotoCobertura').style.visibility = 'visible';", 750 );
	setTimeout( "document.getElementById('imagemAnterior').style.height = '"+ altura +"px';", 100);
	setTimeout( "document.getElementById('imagemProxima').style.height = '"+ altura +"px';", 100);
	
	fotoAnalytics = document.getElementById("fotoAnalytics");
	if(fotoAnalytics) {	
		var IFrameDoc;
		if (fotoAnalytics.contentDocument) {
			IFrameDoc = fotoAnalytics.contentDocument; 
		} else if (fotoAnalytics.contentWindow) {
			IFrameDoc = fotoAnalytics.contentWindow.document;
		} else if (fotoAnalytics.document) {
			IFrameDoc = fotoAnalytics.document;
		} else {
			return true;
		}
		IFrameDoc.location.replace(urlFotoCount + foto_id);
	}

	/*
	if(fotoAnalytics) {	
	if (document.all) 
			document.all.fotoAnalytics.contentWindow.location = urlFotoCount + foto_id;
		else 
			fotoAnalytics.src = urlFotoCount + foto_id;
	}
	*/
}
function flashSubmit(){		
	var Flash;
	if(document.embeds && document.embeds.length>=1 && navigator.userAgent.indexOf("Safari") == -1 &&
					navigator.appVersion.indexOf("MSIE")==-1) {
		Flash = document.getElementById("EmbedFlashFilesUpload");
	}
	else {
		Flash = document.getElementById("FlashFilesUpload");
	}
	
	var FormObj = document.getElementById("formFlash");
	
	showLoading();
	var FormValues = '';
	var list = Flash.fileList();
	if(list.length > 0){
		for (var i = 0; i<FormObj.elements.length; i++)
			FormValues += escape(FormObj.elements[i].name) + '=' + escape(FormObj.elements[i].value) + ((i!=(FormObj.elements.length-1))?'&':'');
		Flash.SetVariable("uploadUrl", document.getElementById("uploadScriptUrl").value); 
		Flash.SetVariable("SubmitFlash", FormValues);	
	} else {
		hideLoading();
		document.getElementById("msgUploadFiles").innerHTML = "Escolha as imagens que deseja subir";
	}
	return false;
}	
	
function hideRelacionado(show) {
	var video = document.getElementById('janelaVideoRelacionado');
	var foto = document.getElementById('janelaFotoRelacionada');
	var iVideo = document.getElementById('janelaInsereVideo');
	var iFoto = document.getElementById('janelaInsereFoto');
	var vFoto = document.getElementById('janelaVerFotoRel');
	
	if(foto)
		foto.style.display = 'none';
	if(video)
		video.style.display = 'none';	
	if(iFoto)
		iFoto.style.display = 'none';
	if(iVideo)
		iVideo.style.display = 'none';	
	if(vFoto)
		vFoto.style.display = 'none';			
	if(show!="") {
		var showDiv = document.getElementById(show);
		if(showDiv)
			showDiv.style.display = 'block';
	}

}	

function handleArrowKeys(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
    	var prox = findDOM('proxima');
		var ant = findDOM('anterior');
        switch (evt.keyCode) {
            case 37:
            	ant.className = 'voltar-foto-sel';
           		setTimeout("findDOM('anterior').className = 'voltar-foto';",1200);
                eval(antAction);
                break;    
            case 39:
            	prox.className = 'avancar-foto-sel';
            	setTimeout("findDOM('proxima').className = 'avancar-foto';",1200);
                eval(proxAction);
                break;    
         }
    }
}