/*
 * autor: Ciro Feitosa - http://cirofeitosa.com.br
 * jan/2009
 */

var tmp;
var foto_total;
var foto_atual;
var foto_atual_id;
var coords;
var offset;

function mycarousel_initCallback(carousel) {
    $('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

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($('#carrossel li a')[foto_atual], true);
}

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

foto_mostra = function(fid, param)
{
	tmp = fid;
	if ($(tmp).attr('rel') != foto_atual  ||  param == true) {
		foto_atual = parseInt($(tmp).attr('rel'));
		foto_atual_id = parseInt($(tmp).attr('foto'));
		$('a.ant,a.pro').show();
	
		$('#carrossel li a').removeClass('ativo');
		$(tmp).addClass('ativo');
		offset = $(tmp).offset();
	
		//$.post('/ajax/contador', { secao: 'fotos', id: $(tmp).attr('foto') });
		
		var height = $('#box_foto_ativa').height();
		$('#box_foto_ativa p, #box_foto_ativa h3, #box_foto_ativa img#foto_ativa').hide();
			$('#box_foto_ativa').css('background', 'url(/_img/load.gif) no-repeat center center').css('height', height + 'px');
			$('#box_foto_ativa h3').text($(tmp).attr('title').split('|')[0]);
			$('#box_foto_ativa p').text('foto: ' + $(tmp).attr('title').split('|')[1] + ' (' + (parseInt(foto_atual) + 1) + ' de ' + foto_total + ')');
			$('#box_foto_ativa img#foto_ativa').attr('src', $(tmp).attr('href'));
			$('#box_foto_ativa img#foto_ativa').load(function()
			{
				location.replace("#foto="+foto_atual);
	
				$('#box_foto_ativa img#foto_ativa').attr('alt', $(tmp).attr('title'));
				$('#box_foto_ativa p, #box_foto_ativa h3, #box_foto_ativa img#foto_ativa').show();
				$('#box_foto_ativa').css('background', 'none').css('height', 'auto');
				foto_botoes();
		});
	
	
		/**
		 * 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 > 1000)
		{
			$('#mycarousel-next').click();
		}
		else if (offset.left < 300)
		{
			$('#mycarousel-prev').click();
		}
	}
}

$(document).ready(function()
{
	foto_total = $('#carrossel li a').length;

	document.onkeydown = function(e){ 	
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 39 && foto_atual < (foto_total-1)){ // direita
			foto_proxima_anterior('+');
		}else if(keycode  == 37 && foto_atual > 0){ //esquerda
			foto_proxima_anterior('-');
		}	
	};

	$('#carrossel li a').click(function()
	{
		foto_mostra($(this));
		return false;
	});

	$('a.ant').click(function()
	{
		foto_proxima_anterior('-');
		return false;
	});

	$('a.pro').click(function()
	{
		foto_proxima_anterior('+');
		return false;
	});

	if (foto_total <= 14)
	{
		$('#rotativo a.controls').hide();
		$('#carrossel').css('margin-left', ($('#rotativo').width() - (foto_total*58))/2 + 'px');
	}
	else 
	{
		$('#carrossel').jcarousel(
		{
			scroll: 10,
			initCallback: mycarousel_initCallback,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
	}

	var foto_pre = location.href.split('#');
	if (foto_pre[1])
	{
		foto_pre = foto_pre[1].split('=');
		foto_pre = parseInt(foto_pre[1]);
		if (foto_pre != '')
			foto_mostra($('#carrossel li a:eq('+foto_pre+')'), true);
		else
			foto_mostra($('#carrossel li a')[0], true);
	}
	else
	{
		foto_mostra($('#carrossel li a')[0], true);
	}
	foto_botoes();
	
});

$(window).resize(function() { 
	foto_botoes();
});

