/**
 * 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;
	}

	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 openDescription() {
	var box = $("#foto-descricao");
	if (box) {
		box.show();
	}
}

function closeDescription() {
	var box = $("#foto-descricao");
	if (box)
		box.animate({height:"toggle"});
}

function getCobFoto(foto){
	document.getElementById('fotoCobertura').src = "../../../templates/site/default/image/p.gif";
	setTimeout( "changeCobFoto("+foto+");", 150 );
	if(hashes[foto][1] != "" && hashes[foto][1] != "Click Agito") {
		openDescription();
	}
}

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 = getclicks();
	var album = fotos[foto][7];
	var hash = hashes[foto][0];
	
	if((foto-1) >= 0)
		var imagemAnteriorSrc = fotos[foto-1][5];
	if((foto+1) < totalFotos)
		var imagemProximaSrc = fotos[foto+1][5];

	if( width > 620 ) {
		widthOr = width;
		width = 620;
		height = width / (widthOr/height); 
	}
	fotos[foto][6] = 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;
	
	if( document.getElementById('fotoDenuncia') )
		document.getElementById('fotoDenuncia').onclick = function() {showDenuncia( hash );}
	
	document.getElementById('urlFoto').value = urlFoto + foto_id;
	document.getElementById('div-foto').style.height = height+"px";
//	document.getElementById('linkSalvarFoto').href = pathCoberturas + imagemSrc;
	document.getElementById('fotoCobertura').style.visibility = 'hidden';
	
	refreshThumbs(foto_id,cobertura_id,(foto+1),(foto-1),width,height);
	var corp = document.getElementById('content');
	corp.style.height = 'auto';
	
	setTimeout( "document.getElementById('fotoCoberturaAnterior').src = '"+pathCoberturas+imagemAnteriorSrc+"';", 100 );
	setTimeout( "document.getElementById('fotoCoberturaProxima').src = '"+pathCoberturas+imagemProximaSrc+"';", 100 );
	setTimeout( "document.getElementById('fotoCobertura').src = '"+pathCoberturas+imagemSrc+"';", 200 );
	setTimeout( "document.getElementById('fotoClk').src = '/scripts/shm_clk.php?f="+foto_id+"&d="+dm+"';", 100 );
	setTimeout( "changeLike('" + urlFoto + foto_id + "');", 200);
	
	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 setHash(idx) {
	foto = document.getElementById('formFotoIdx').value;
	hashes[foto][idx] = "1";
}	

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

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($('#thumbImg'+foto_id).parent().parent().index() == $('#thumbImg'+fotos[proximaFoto][0]).parent().parent().index()) {
					proxAction = "getCobFoto("+proximaFoto+");";
					prox.onclick = function() {getCobFoto(proximaFoto);}
				}				
				else {
					proxAction = "getCobPagina(("+numeroPagina+"+1),0);$('#thumbs').scrollable().next(0);";
					prox.onclick = function() {getCobPagina((numeroPagina+1),0);$("#thumbs").scrollable().next(0);}			
				}
			}
		} 
		else {
			proxAction = "void(null);";
			prox.onclick = function() {void(null);}			
		}
		if(anteriorFoto!=null) {
			if(anteriorFoto >= 0) {
				var an=findDOM('thumb'+fotos[anteriorFoto][0]);
				if($('#thumbImg'+foto_id).parent().parent().index() == $('#thumbImg'+fotos[anteriorFoto][0]).parent().parent().index()) {
					antAction = "getCobFoto("+anteriorFoto+");";
					ant.onclick = function() {getCobFoto(anteriorFoto);}				
				} 
				else {
					antAction = "getCobPagina(("+numeroPagina+"-1),1);$('#thumbs').scrollable().prev(0);";
					ant.onclick = function() {getCobPagina((numeroPagina-1),1);$("#thumbs").scrollable().prev(0);}
				}
			}
		} 
		else {
			antAction = "void(null);";
			ant.onclick = function() {void(null);}			
		}
	}
	
	$('#fotoCobertura').removeAttr("width");
	$('#fotoCobertura').removeAttr("height");

	if(largura > altura) {
		setTimeout("document.getElementById('fotoCobertura').style.width = '"+ largura +"px';", 100);
		setTimeout("document.getElementById('fotoCobertura').style.height = 'auto';", 100);	
	} else {
		setTimeout("document.getElementById('fotoCobertura').style.height = '"+ altura +"px';", 100);
		setTimeout("document.getElementById('fotoCobertura').style.width = 'auto';", 100);
	}
	setTimeout("getImageWidth('"+pathCoberturas+imagemSrc+"')", 100);
	
	if(fotoHidden!=1)
		setTimeout( "document.getElementById('fotoCobertura').style.visibility = 'visible';", 750 );

	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);
	}
}

function getImageWidth(path) {
	var imagemSrc = fotos[proximaFoto-1][5];
	var t = new Image();
    t.src = path;
    largura = t.width;
    altura = t.height;	
    
    document.getElementById('div-foto').style.width = largura + 'px';
}

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:
                eval(antAction);
                break;    
            case 39:
                eval(proxAction);
                break;    
         }
    }
}
