$(document).ready(function(){
	var fotos = new Array( 'Fab1.jpg', 
				   		   'Fabrica-Nissin.jpg');
	$('ul#thumbs').empty();
	carregaImagem(fotos[0]);
	
	count = 0;
	
	for(i=0; i < fotos.length; i++){
		$('ul#thumbs').append('<li><a class="thumbInst" href="javascript:void(0)" title="'+i+'"><img src="/img/institucional/thumb/'+fotos[i]+'" border="0"></a></li>');
	}
	
	$('.voltar').live("click", function(){
		if(count == 0) {
			count = (fotos.length - 1);
		} else {
			count--;
		}		
		carregaImagem(fotos[count]);
	});
	
	$('.avancar').live("click", function(){
		if(count == (fotos.length - 1)) {
			count = 0;
		} else {
			count++;
		}
		carregaImagem(fotos[count]);
	});
	
	$('.thumbInst').click(function(){
		count = $(this).attr('title');
		carregaImagem(fotos[count]);
	})
	
	
})

function carregaImagem($count){
	$('#imgBigger').html('<img class="imgLoading" src="/img/loading.gif" alt="Carregando" />').animate({opacity: 1.0}, 1000, function(){
		$img = new Image();
		$($img).load(function(){
	   		$(this).css('display', 'none');
			$('#imgBigger').html(this).find('img').fadeIn();
		}).error(function(){
	      	$('#imgBigger').append('<p>Ocorreu um erro durante o carregamento da imagem.</p>');
	    }).attr('src', '/img/institucional/' + $count).height('385px').width('512px');
	});	
}