$(function () {

	$('.album').click(function () {
	
		if ( $.browser.msie && $.browser.version == '6.0' ) {
			alert('Seu navegador não oferece suporte para a utilização desse recurso.');
			return false;
		}
	
		$('body').prepend('<div id="a_sombra"></div><div id="a_painel"></div>');
		$('#a_sombra').css('opacity','0').show().fadeTo('slow', '0.5');

		var obj = this;
		
		$('#a_painel').fadeIn().append('<div id="a_controle"><h1>'+ $(obj).attr('title') +'</h1></div><img src="'+ $(obj).attr('href') +'" id="a_conteudo" />');
		
		if ($('.album').length > 1) {
			$('#a_controle').append('<button class="fechar"></button>').append('<button class="avancar"></button>').append('<button class="voltar"></button>');
		}
		else {
			$('#a_controle').append('<button class="fechar"></button>');
		}
		
				
		var f = function (obj, c) {
			$('#a_controle h1').html($('.album:eq('+ c +')').attr('title'));
			$('#a_conteudo').attr('src', $('.album:eq('+ c +')').attr('href'));
			return $('.album').index($('.album:eq('+ c +')'));
		}
		
		var c1 = $('.album').index(obj) + 1;		
		$('.avancar').click(function () {
		
			$('#a_conteudo').hide();
			c2 = c1 -1;
			if (c1 == $('.album').length) 
				c1 = 0;
			c1 = f(obj, c1) + 1;
		});
		
		var c2 = $('.album').index(obj) - 1;
		$('.voltar').click(function () {
			$('#a_conteudo').hide();
			c1 = c2 +1;
			if (c2 < 0) 
				c2 = $('.album').length - 1;
			c2 = f(obj, c2) - 1;
		});
		
		$('#a_conteudo').load(function() {
			var w = $(this).width();
			var h = $(this).height();
			$('#a_painel').animate({ width: w+'px', height: h+'px', marginTop : '-'+h/2+'px', marginLeft: '-'+w/2+'px' }, 200);
			$(this).fadeIn('slow');
		});
		
		$('#a_conteudo').hover(function (e) {
			$('#a_controle').css('opacity','0').show().css('opacity', '0.8');
		});
		
		$('#a_sombra').hover(function () {
			$('#a_controle').fadeOut();
		});
		
		$('.fechar').click(function () { d(); });
		$('#a_sombra').click(function () { d(); });
		
		$(document).keydown(function (e) {
	        if(e.which == 27) 
	            d();
		});
		
		var d = function () {
			var x = ['#a_sombra','#a_painel','#a_conteudo','#a_controle'];
			for (var i=0; i<x.length; i++)
				$(x[i]).remove();
		}
		return false;
	});

});
