/*
 * autor: Ciro Feitosa - http://cirofeitosa.com.br
 * julho/2008
 */

var atributos;
var foto_total;
var foto_atual;
var coords;
var offset;

foto_proxima_anterior = function(acao)
{
	if (acao == '+')
	{
		foto_atual++;
		if (foto_atual >= foto_total)
			foto_atual = 0; // vai para o inicio
	}
	else
	{
		foto_atual--;
		if (foto_atual < 0)
			foto_atual = foto_total - 1; // vai para o final
	}
	foto_mostra(foto_atual);
}

foto_mostra = function(fid)
{
	foto_atual = fid;
	
	$('a.ant,a.pro').show();
	$('.galeriaInterna li a').removeClass('ativo');
	$('.galeriaInterna li a:eq('+fid+')').addClass('ativo');
	
	atributos = $('.galeriaInterna li a:eq('+fid+')');
	offset = atributos.offset();
	
	$('p#foto').css('height', '500px');

	$('p#foto img, p#foto span').hide('normal', function()
	{
		$('p#foto img').load(function()
		{
			$('p#foto span').text(atributos.attr('title'));
			$('p#foto img, p#foto span').show();
			$('p#foto').css('height', 'auto');
			$('a.ant,a.pro').height($('p#foto').height());
		}).attr('src', atributos.attr('href'));
	});	


	/**
	 * altera posicao do jcarousel 
	 */
	if (foto_atual == 0)
	{
		$('a.ant').hide(); // retira botao voltar
	}
	else if (foto_atual == (foto_total - 1))
	{
		$('a.pro').hide(); // retira botao avancar
	}
	else if (offset.left > 830)
	{
		$('.jcarousel-next').click();
	}
	else if (offset.left < 350)
	{
		$('.jcarousel-prev').click();
	}
}

foto_botoes = function()
{
	/**
	 * posiciona os botoes avancar e voltar na foto
	 */
	coords = $('#foto').offset();
	$('a.ant').css('left', coords.left);
	
	coords = parseInt (($(window).width() - $('#site').width()) / 2);
	if (coords < 0)
		coords = 0;
	$('a.pro').css('right', coords);
}

$(document).ready(function()
{
	foto_total = $('.galeriaInterna li a').length;
	$('.galeriaInterna li a').each(function(i)
	{
		$(this).click(function()
		{
			foto_mostra(i);
			return false;
		});
	});
	
	$(window).resize(function()
	{
		foto_botoes();
	});
	
	foto_botoes();
	if (foto_atual > 0)
		$('a.ant').show();
	if (foto_atual < (foto_total - 1))
		$('a.pro').show();
});