var GaleriaFotos = {
	
	listaFotos: null,
	listaThmbnails: null,
	numeroThumbsPorPagina: 0,
	inicioContadorThumbs: 0,
	finalContadorThumbs: 0,
	posicaoFotoAtual: 0,
	painelFotos: null,
	painelFotoAmpliada: null,
	
	init: function()
	{
		
		
		GaleriaFotos.numeroThumbsPorPagina = 16;
		GaleriaFotos.inicioContadorThumbs = 0;
		GaleriaFotos.finalContadorThumbs = 0;
		GaleriaFotos.posicaoFotoAtual = 0;
		
	},
	
	setListaFotos: function(listaFotos) 
	{
		GaleriaFotos.listaFotos = listaFotos;
	},
	
	setFotoAtual: function(posicaoFotoAtual)
	{
		GaleriaFotos.posicaoFotoAtual = posicaoFotoAtual;
	},
	
	showLoading: function()
	{
		GaleriaFotos.fotoAmpliada.innerHTML = '<img src="../../Images/Commons/loading.gif" width="16" height="16" align="absmiddle" /><br>Buscando Fotos';
	},
	
	showThumbnails: function(inicioContadorThumbs)
	{
		
		GaleriaFotos.painelFotos = document.getElementById('PainelListaFotos');
		
		var dadosAuxiliar;
		var numeroColunas = 4;
		var html = '';
		GaleriaFotos.inicioContadorThumbs = (inicioContadorThumbs >= 0) ? inicioContadorThumbs : 0;
		GaleriaFotos.finalContadorThumbs = ((inicioContadorThumbs + GaleriaFotos.numeroThumbsPorPagina) <= GaleriaFotos.listaFotos.results) ? (inicioContadorThumbs + GaleriaFotos.numeroThumbsPorPagina) : GaleriaFotos.listaFotos.results;
		
		if (GaleriaFotos.listaFotos.results > 0)
		{
		
			/* INÍCIO LISTA DE FOTOS */
			
			html += '<div style="height:170px;">';
			html += '<table cellpadding="2" cellspacing="5" border="0">';
			
			for (var i=GaleriaFotos.inicioContadorThumbs; i<GaleriaFotos.finalContadorThumbs; i++)
			{
			
				dadosAuxiliar = GaleriaFotos.listaFotos.rows[i];
				
				if (i % numeroColunas == 0)
				{
					
					if (i > 0)
						html += '</tr>';
					
					html += '<tr>';
					
				}
				
				html += '<td align="center" class="BordaFoto">';
				html += '<a href="javascript:;" onclick="GaleriaFotos.changeFoto(\'' + dadosAuxiliar.urlAmpliada + '&Width=600&Height=1000\',' + i + ');">';
				html += '<img src="' + dadosAuxiliar.url + '&Width=30&Height=30" border="0">';
				html += '</a>';
				html += '</td>';
			
			}
			
			html += '</table>';
			html += '</div>';
			
			/* FINAL LISTA DE FOTOS */
			
			/* INÍCIO PAGINAÇÃO */
			
			html += '<table width="100%" cellpadding="2" cellspacing="0" border="0" align="center">';
			html += '<tr>';
			html += '<td height="20" align="left" style="font-size:10px;">Total: ' + GaleriaFotos.listaFotos.results + ' fotos</td>';
			
			if (GaleriaFotos.inicioContadorThumbs > 0)
				html += '<td height="20" align="right" style="font-size:10px;"><a href="javascript:GaleriaFotos.showThumbnails(' + (GaleriaFotos.inicioContadorThumbs - GaleriaFotos.numeroThumbsPorPagina) + ');">anterior</a></td>';
			else
				html += '<td height="20" align="right" style="font-size:10px; color:#CCCCCC;">anterior</td>';
			
			if (GaleriaFotos.finalContadorThumbs < GaleriaFotos.listaFotos.results) 	
				html += '<td height="20" align="left" style="font-size:10px;"><a href="javascript:GaleriaFotos.showThumbnails(' + GaleriaFotos.finalContadorThumbs + ');">próximo</a></td>';
			else
				html += '<td height="20" align="left" style="font-size:10px; color:#CCCCCC;">próximo</td>';
				
			html += '</tr>';
			html += '</table>';
			
			/* FINAL PAGINAÇÃO */
		
		}
		else
		{
			html += '<table cellpadding="2" cellspacing="5" border="0" align="center">';
			html += '<tr><td height="80" align="center">Nenhuma foto foi cadastrada</td></tr>';
			html += '</table>';
		}
		
		GaleriaFotos.painelFotos.innerHTML = html;
		
	},
	
	changeFoto: function(url,posicaoFotoAtual)
	{		
		
		GaleriaFotos.posicaoFotoAtual = posicaoFotoAtual;
		GaleriaFotos.painelFotoAmpliada = document.getElementById('PainelFotoAmpliada');
		
		var imagemAnterior = null;
		var imagemPosterior = null;
		var html = '';
		
		html += '';
		
		if (GaleriaFotos.posicaoFotoAtual > 0)
			imagemAnterior = GaleriaFotos.listaFotos.rows[GaleriaFotos.posicaoFotoAtual - 1];
			
		if (GaleriaFotos.posicaoFotoAtual < (GaleriaFotos.listaFotos.results-1))
			imagemPosterior = GaleriaFotos.listaFotos.rows[GaleriaFotos.posicaoFotoAtual + 1];
		
		/* INÍCIO IMAGEM */
		
		html += '<table width="100%" cellpadding="2" cellspacing="0" border="0" align="center">';
		html += '<tr><td>';
		html += '<img id="FotoAmpliada" src="' + GaleriaFotos.listaFotos.rows[GaleriaFotos.posicaoFotoAtual].urlAmpliada + '&Width=600&Height=1000" border="0" />';
		html += '</td></tr>';
		html += '</table>';		
		
		/* FINAL IMAGEM */
		
		/* INÍCIO PAGINAÇÃO */

		var legenda = "";

		if (GaleriaFotos.listaFotos.rows[GaleriaFotos.posicaoFotoAtual].comentarios != "")
			legenda = " - " + GaleriaFotos.listaFotos.rows[GaleriaFotos.posicaoFotoAtual].comentarios;
			
		html += '<table width="100%" cellpadding="2" cellspacing="0" border="0" align="center">';
		html += '<tr>';
		html += '<td width="90%" height="20" align="left" style="font-size:10px;">' + (GaleriaFotos.posicaoFotoAtual + 1) + 'ª foto' + legenda + '</td>';
		
		if (imagemAnterior != null)
			html += '<td height="20" align="right" style="font-size:10px;"><a href="javascript:GaleriaFotos.changeFoto(\'' + imagemAnterior.urlAmpliada + '&Width=600&Height=1000\',' + (GaleriaFotos.posicaoFotoAtual - 1) + ');">anterior</a></td>';
		else
			html += '<td height="20" align="right" style="font-size:10px; color:#CCCCCC;">anterior</td>';
		
		if (imagemPosterior != null) 	
			html += '<td height="20" align="right" style="font-size:10px;"><a href="javascript:GaleriaFotos.changeFoto(\'' + imagemPosterior.urlAmpliada + '&Width=600&Height=1000\',' + (GaleriaFotos.posicaoFotoAtual + 1) + ');">próximo</a></td>';
		else
			html += '<td height="20" align="left" style="font-size:10px; color:#CCCCCC;">próximo</td>';
			
		html += '</tr>';
		html += '</table>';
		
		/* FINAL PAGINAÇÃO */
		
		
		
		// imprime a foto atual
		GaleriaFotos.painelFotoAmpliada.innerHTML = html;
		
	},
	
	loadThumbnails: function()
	{
		
		
		
	}
	
}