var descricaoAberta;
var heightTamanhoImagemReal = 900;
var widthTamanhoImagemReal = 1440;
var hhh, www, wwwImg, heightCalculado, widthCalculado, leftPalcoThumb, widthPalcoThumb, heightPalcoThumb, disponivelPalcoTumbsHeight, disponivelPalcoTumbsWidth;
var topPalcoThumb = 113;
var paddingPalcoThumb = 50;
var widthThumbnail = 132;
var paddingThumbnail = 24;
var palcoThumbAberto = true;
var totalFotos;
var menuAberto = false;
var minWidth = 150;
var imagemMomento = 1;
var abertoEsquerda = false;
var abertoDireita = false;

var paginasThumbnails, qtdThumbnailsPorLinha, qtdLinhasThumbnails, qtdThumbnailsPorPagina, qtdTotalThumbnails, paginaSelecionadaThumbnails;

widthCalculado = f_clientWidth();
heightCalculado = f_clientHeight();

/*
function toogleSetaEsquerda(){
	if(abertoEsquerda){
		var myFx2 = new Fx.Tween('transparenteEsquerda');
		myFx2.start('opacity', '1.0', '0.5');
		
	}else{
		var myFx = new Fx.Tween('transparenteEsquerda');
		myFx.start('opacity', '0.5', '1.0');
	}
	abertoEsquerda = !abertoEsquerda;
}
function toogleSetaDireita(){
	if(abertoDireita){
		var myFx2 = new Fx.Tween('transparenteDireita');
		myFx2.start('opacity', '1.0', '0.5');
		
	}else{
		var myFx = new Fx.Tween('transparenteDireita');
		myFx.start('opacity', '0.5', '1.0');
	}
	abertoDireita = !abertoDireita;
}
*/
function actualizaTamanhosPalcoThumbs(){
	document.getElementById('palcoThumbnails').style.width = widthPalcoThumb+"px";
	document.getElementById('palcoThumbnails').style.height = heightPalcoThumb+"px";
	
	document.getElementById('contentorInteriorThumbs').style.width = disponivelPalcoTumbsWidth+"px";
	//document.getElementById('contentorInteriorThumbs').style.height = disponivelPalcoTumbsHeight+"px";
}
function actualizaPosicoesPalcoThumbs(){
	document.getElementById('palcoThumbnails').style.top = topPalcoThumb-paddingPalcoThumb+"px";
	document.getElementById('palcoThumbnails').style.left = leftPalcoThumb+"px";
}
function mostraProxima(){
	if(imagemMomento+1<=totalFotos){
		mostraFoto(imagemMomento+1);
	}
}
function mostraAnterior(){
	if(imagemMomento-1>=1){
		mostraFoto(imagemMomento-1);
	}
}
function mostraFoto(posicaoImagem){
	document.getElementById('descricaoNavegacaoNumeros').innerHTML = (posicaoImagem)+ "/" + totalFotos;
	
	actualizaTamanhos();
	posMom = (imagemMomento-1)*widthCalculado;
	novaPos = (posicaoImagem-1)*widthCalculado;
	imagemMomento = posicaoImagem;
	novaPos = Math.abs(novaPos);
	
	var excedente = novaPos-posMom;
	
	
	$('#contentorGaleria').animate({
		marginLeft: '-='+excedente
	}, 500);
}
function toggleMenu(controlo){
	switch(controlo){
		case 1:
			if(!menuAberto){
				$('#fakeSlide').slideUp('slow');
				menuAberto = !menuAberto;
			}
		break;
		case 2:
			if(!menuAberto){
				$('#fakeSlide').slideUp('slow');
			}else{
				$('#fakeSlide').slideDown('slow');
			}
			menuAberto = !menuAberto;
		break;
		default:
			menuAberto = menuAberto;
	}
}
function actualizaTamanhos(){
	hhh = f_clientHeight();
	www = f_clientWidth();
	//Tamanhos Palco Thumbnail
	leftPalcoThumb = ((www*0.02)+160)-paddingPalcoThumb;
	widthPalcoThumb = (www-(leftPalcoThumb+(www*0.08)))-(2*paddingPalcoThumb);
	heightPalcoThumb = hhh-topPalcoThumb;
	disponivelPalcoTumbsWidth = widthPalcoThumb;
	disponivelPalcoTumbsHeight = heightPalcoThumb-30-50-25-100;
	
	
	wwwImg = www;
	hhhImg = hhh;
	if(wwwImg<minWidth){
		wwwImg = minWidth;
	}
	heightCalculado = (wwwImg*heightTamanhoImagemReal)/widthTamanhoImagemReal;
	widthCalculado = wwwImg;
	if(heightCalculado<hhh){
		widthCalculado = (widthTamanhoImagemReal*hhh)/heightTamanhoImagemReal;
		document.getElementById('contentorGaleria').style.width = ((totalFotos+1)*widthCalculado) + "px";
		for(i=1;i<=totalFotos;i++){
			document.getElementById('contFoto'+i).style.width = widthCalculado + "px";
			document.getElementById('contFoto'+i).style.height = hhh + "px";
			document.getElementById('foto'+i).height = hhh;
			document.getElementById('foto'+i).width = widthCalculado;
		}
	}else{
		document.getElementById('contentorGaleria').style.width = ((totalFotos+1)*widthCalculado) + "px";
		for(i=1;i<=totalFotos;i++){
			
			document.getElementById('contFoto'+i).style.width = widthCalculado + "px";
			document.getElementById('contFoto'+i).style.height = hhh + "px";
			document.getElementById('foto'+i).width = widthCalculado;
			document.getElementById('foto'+i).height = heightCalculado;
		}
	}
}
function actualizaTamanhosFake(){
	hhh = f_clientHeight();
	www = f_clientWidth();
	
	wwwImg = www;
	hhhImg = hhh;
	if(wwwImg<minWidth){
		wwwImg = minWidth;
	}
	heightCalculado = (wwwImg*heightTamanhoImagemReal)/widthTamanhoImagemReal;
	widthCalculado = wwwImg;
	if(heightCalculado<hhh){
		widthCalculado = (widthTamanhoImagemReal*hhh)/heightTamanhoImagemReal;
		document.getElementById('contentorGaleriaFake').style.width = ((totalFotos+1)*widthCalculado) + "px";
		for(i=1;i<=totalFotos;i++){			
			document.getElementById('contFotoFake'+i).style.width = widthCalculado + "px";
			document.getElementById('contFotoFake'+i).style.height = hhh + "px";
			document.getElementById('fotoFake'+i).height = hhh;
			document.getElementById('fotoFake'+i).width = widthCalculado;
		}
	}else{
		document.getElementById('contentorGaleriaFake').style.width = ((totalFotos+1)*widthCalculado) + "px";
		for(i=1;i<=totalFotos;i++){
			
			document.getElementById('contFotoFake'+i).style.width = widthCalculado + "px";
			document.getElementById('contFotoFake'+i).style.height = hhh + "px";
			document.getElementById('fotoFake'+i).width = widthCalculado;
			document.getElementById('fotoFake'+i).height = heightCalculado;
		}
	}
	
}
function fazResize(){
	actualizaTamanhos();
	posMom = (imagemMomento-1)*widthCalculado;
	document.getElementById('contentorGaleria').style.marginLeft = "-" + posMom + "px";
}

function verificaLoader(a, b){
	if(a == b){
		//$('#contentorGaleria').hide();
		
		teste = "";
		for(i=0; i<b; i++){
			teste += "<div class=\"fotoIndividual\" id=\"contFotoFake" + (i+1) + "\"><img id=\"fotoFake" + (i+1) + "\" src=\"" + galeriaPortefolio[clienteId]['imagens'][i] + "\"/></div>";	
		}
		document.getElementById('contentorGaleriaFake').innerHTML = teste;
		document.getElementById('contentorGaleriaFake').style.marginTop = "-"+2*hhh + "px";
		
		actualizaTamanhosFake();
		$('#contentorGaleriaFake').animate({
				marginTop: '+='+hhh
			}, 500);
		$('#contentorGaleria').animate({
			marginTop: '+='+hhh
		}, 500, function() {
		// Animação Completa
		teste = "";
		for(i=0; i<b; i++){
			teste += "<div class=\"fotoIndividual\" id=\"contFoto" + (i+1) + "\"><img id=\"foto" + (i+1) + "\" src=\"" + galeriaPortefolio[clienteId]['imagens'][i] + "\"/></div>";	
		}
			
			document.getElementById('contentorGaleria').innerHTML = teste;
			document.getElementById('contentorGaleriaFake').innerHTML = "";
			document.getElementById('contentorGaleria').style.marginTop = "0px";
			imagemMomento = 1;
			actualizaTamanhos();
			posMom = (imagemMomento-1)*widthCalculado;
			
			document.getElementById('contentorGaleria').style.marginLeft = "-" + posMom + "px";
			document.getElementById('loader').style.visibility = "hidden";
		});
		
			
			
		
		
		
		
		
	}else{
		document.getElementById('loader').style.visibility = "visible";
	}
}

function mostraOutroTrabalho(idCli){
	if(clienteId != idCli){
		if(descricaoAberta){
			$('#sliderDescricao').animate({
				width: '-=' + 260
			}, 500, function() {
				document.getElementById('descricaoCliente').innerHTML = galeriaPortefolio[idCli]['cliente'];
				document.getElementById('descricaoDescricao').innerHTML = galeriaPortefolio[idCli]['descricao'];
				document.getElementById('descricaoNavegacaoNumeros').innerHTML = "1/" + galeriaPortefolio[idCli]['imagens'].length;		
				$('#sliderDescricao').animate({
					width: '+=' + 260
				}, 500);		
			});
		}else{
			document.getElementById('descricaoCliente').innerHTML = galeriaPortefolio[idCli]['cliente'];
			document.getElementById('descricaoDescricao').innerHTML = galeriaPortefolio[idCli]['descricao'];
			document.getElementById('descricaoNavegacaoNumeros').innerHTML = "1/" + galeriaPortefolio[idCli]['imagens'].length;		
			$('#sliderDescricao').animate({
				width: '+=' + 260
			}, 500);
		}
		descricaoAberta=true;
		
		imagensPreloadadas = 0;
		//alert(imagensArr.length);
		clienteId = idCli;
		totalFotos = galeriaPortefolio[clienteId]['imagens'].length;
		for(i=0; i<galeriaPortefolio[clienteId]['imagens'].length; i++){
			var img = new Image();
			$(img).load(function () {
				//$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
				//$(this).hide();
				//$(this).fadeIn();
				imagensPreloadadas++;
				verificaLoader(imagensPreloadadas, galeriaPortefolio[clienteId]['imagens'].length);
			}).error(function () {
				// notify the user that the image could not be loaded
			}).attr('src', galeriaPortefolio[idCli]['imagens'][i]);	
			//alert("Momento: " + imagensPreloadadas);
			
		}
	}
	
	
}
function fechaPalcoThumbs(){
	$('#palcoThumbnails').animate({
		marginTop: '+='+hhh
	}, 500, function() {
		// Animação Completa
		document.getElementById('btaoAbrirPalcoThumbs').innerHTML = "<a href=\"javascript:abrePalcoThumbs();\"><img src=\"img/btAbrePalcoThumbs.gif\" width=\"26\" height=\"20\" border=\"0\" /></a>";
		palcoThumbAberto = false;
	});
	if(!descricaoAberta){
		$('#sliderDescricao').animate({
			width: '+=' + 260
		}, 500);	
	}
	descricaoAberta=true;
}
function abrePalcoThumbs(){
	actualizaTamanhosPalcoThumbs();
	criaQuantidadesThumbnails();
	paginaThumbnailsGaleria();
	document.getElementById('btaoAbrirPalcoThumbs').innerHTML = "";
	$('#palcoThumbnails').animate({
		marginTop: '-='+hhh
	}, 500, function() {
		// Animação Completa
		palcoThumbAberto = true;
	});	
	if(descricaoAberta){
		$('#sliderDescricao').animate({
			width: '-=' + 260
		}, 500);	
	}
	descricaoAberta=false;
}
function mudaPaginaThumbnails(pg){
	paginaSelecionadaThumbnails = pg;
	actualizaTamanhos();
}

