// JavaScript Document
var indice=0;
var aFoto=new Array();

$(function(){
			  
	var len=aFoto.length - 1;
	
	// abre a primeira foto
	$('#thumbs a:first').each(function(){
		trocaFoto($(this).attr('href'), $(this).attr('title'), $(this).attr('id'));
	});
	
	// qdo clica no link da foto
	$('.thumb a').click(function(){
		return trocaFoto($(this).attr('href'), $(this).attr('title'), $(this).attr('id'));
	});
	
	// link das setinhas
	$('a.next').click(function(){
		if (indice < len) {
			indice++;
			strIndice = indice;
			$('a[@id='+strIndice+']').each(function(){
				trocaFoto($(this).attr('href'), $(this).attr('title'), $(this).attr('id'));
			});
		}
		return false;
	});
	$('a.back').click(function(){
		if (indice > 0) {
			indice--;
			strIndice = indice;
			$('a[@id='+strIndice+']').each(function(){
				trocaFoto($(this).attr('href'), $(this).attr('title'), $(this).attr('id'));
			});
		}
		return false;
	});
	
	// combo como link
	$('select').change(function(){
		location.href = $(this).val();
	});
	
	// proxima
	$('a[@class=proxima]').click(function(){
		nAlbum = parseInt($('input[@name=album_id]').val());
		nLimit = parseInt($('input[@name=limit]').val()) + 8;
		proxAnt(nLimit);
		$('#rolagem').html('carregando...');
		$('#rolagem').load('../fotos/_load.php', {limit:nLimit, album_id:nAlbum});
		$('input[@name=limit]').val(nLimit);
		return false;
	});

	// anterior
	$('a[@class=anterior]').click(function(){
		nAlbum = parseInt($('input[@name=album_id]').val());
		nLimit = parseInt($('input[@name=limit]').val()) - 8;
		proxAnt(nLimit);
		$('#rolagem').html('carregando...');
		$('#rolagem').load('../fotos/_load.php', {limit:nLimit, album_id:nAlbum});
		$('input[@name=limit]').val(nLimit);
		return false;
	});
	
	proxAnt(parseInt($('input[@name=limit]').val()));
});


// mostras/esconder links proxima e anterior
function proxAnt(nLimit){
	if ( nLimit<=0 ) {
		$('a[@class=anterior]').css({ 'visibility':'hidden' });
	} else {
		$('a[@class=anterior]').css({ 'visibility':'visible' });
	}
	
	total = $('input[@name=total]').val();
	if ( (nLimit+8) >= total ) {
		$('a[@class=proxima]').css({ 'visibility':'hidden' });
	} else {
		$('a[@class=proxima]').css({ 'visibility':'visible' });
	}
}

// funcao para trocar a foto
function trocaFoto(img, txt, id){
	indice = id;
	if(!txt) txt="";
	$('#fotoContent').html('<div style="width:507px; height:335px; overflow:hidden; background:url('+ img +') center no-repeat; "></div>');
	$('#legenda').html(txt);
	$('#thumbs div').removeClass('thumbOver');
	$('a[@href='+img+']').parent('div').addClass('thumbOver');
	// atualiza o time da secao
	$.ajax({
		type: "GET",
		url: "_setTime.php",
		success: function(msg){
			if (msg=='expirou') {
				alert('Seção expirada por inatividade.');
				self.close();
			}
		}
	});
	return false;
}

