var offset = 0;
var grande = 1;
$(function(){
	offset = 0;
	grande = 1;
	r_ativo = false;
	l_ativo = false;
	//$('.status_imoveis').removeClass('ativo');
	//$(this).addClass('ativo');
	$('#listaImoveis').css('left','0px').fadeOut(100,function(){
		//$('#listaImoveisLoader').fadeIn(100,function(){
			$.getJSON('ajax.php', { 'do': 'get_casamentos' }, function(data){	 
				var numero_imoveis = data.length;
				var text = '';
				var depoimento = '';
				for (var i = 0; i < numero_imoveis; i++) {
					if (i == 1) {
						//depoimento += '<br><h3 class="L">' + data[i].data_casamento + ' ' + data[i].titulo + '</h3><a class="ver-t" href="casamentos/">ver tudo</a><h4 style="clear: both; padding-top:3px;">Depoimento da noiva:</h4><p><a href="casamentos/' + data[i].nome + '/' + data[i].id_casamento + '/">' + data[i].texto + '</a></p>';
						text += '<li><a href="casamentos/' + data[i].nome + '/' + data[i].id_casamento + '/"><img class="med" src="upload/casamento/redimensionada/' + data[i].arquivo + '" alt="' + data[i].legenda + '" title="' + data[i].legenda + '" style="width:240px;" /></a></li>';
					} else {
						text += '<li><a href="casamentos/' + data[i].nome + '/' + data[i].id_casamento + '/"><img class="peq" src="upload/casamento/thumb/' + data[i].arquivo + '" alt="' + data[i].legenda + '" title="' + data[i].legenda + '" /></a></li>';
					}
					
				}
				
				//$('#listaImoveisLoader').fadeOut(100,function(){
					$('#listaImoveis').html(text).fadeIn(100,function(){
						$('#listaImoveisL').css('backgroundPosition','left bottom');
						//if (numero_imoveis < 4) {
						if (numero_imoveis < 0) {
							$('#listaImoveisR').css('backgroundPosition','left bottom');
							$('#listaImoveisR, #listaImoveisL').bind('click',function(){ return false; });
							r_ativo = false;
							l_ativo = false;
						} else {
							$('#depo').html(depoimento);
							r_ativo = true;
							l_ativo = true;
							$('#listaImoveisR, #listaImoveisL').css('backgroundPosition','left top');
							$('#listaImoveisL').unbind().bind('click',function(){
								if (l_ativo) {
									offset += 140;
									$($('#listaImoveis img')[grande]).animate({ width: '108px', marginTop: '20px' },200,function(){
										$(this).attr('src','upload/casamento/thumb/'+data[grande].arquivo);
										$('#listaImoveis').animate({ left: offset+'px' },200,function(){
											grande--;
											//depoimento = '<br><h3 class="L"> ' + data[grande].data_casamento + ' ' + data[grande].titulo + '</h3><a class="ver-t" href="casamentos/">ver tudo</a><h4 style="clear: both; padding-top:3px;">Depoimento da noiva:</h4><p><a href="casamentos/' + data[grande].nome + '/' + data[grande].id_casamento + '/">' + data[grande].texto + '</a></p>';
											//$('#depo').html(depoimento);
											r_ativo = true;
											$('#listaImoveisR').css('backgroundPosition','left top');
											//if (grande > 1) {
											if (grande > 0) {
												$('#listaImoveisL').css('backgroundPosition','left top');
												l_ativo = true;
											} else {
												$('#listaImoveisL').css('backgroundPosition','left bottom');
												l_ativo = false;
											}
											$($('#listaImoveis img')[grande]).animate({ width: '240px', marginTop: '0px' },200,function(){
												$(this).attr('src','upload/casamento/redimensionada/'+data[grande].arquivo);
											});
										});
									});
								}
								return false;
							});
							
							if ((numero_imoveis - grande) > 1) {
									$('#listaImoveisR').css('backgroundPosition','left top');
									r_ativo = true;
							} else {
									$('#listaImoveisR').css('backgroundPosition','left bottom');
									r_ativo = false;
							}
							
							$('#listaImoveisR').unbind().bind('click',function(){
								if (r_ativo) {
									offset -= 140;
									$($('#listaImoveis img')[grande]).animate({ width: '108px', marginTop: '20px' },200,function(){
										$('#listaImoveis').animate({ left: offset+'px' },200,function(){
											grande++;
											//depoimento = '<br><h3 class="L"> ' + data[grande].data_casamento + ' ' + data[grande].titulo + '</h3><a class="ver-t" href="casamentos/">ver tudo</a><h4 style="clear: both; padding-top:3px;">Depoimento da noiva:</h4><p><a href="casamentos/' + data[grande].nome + '/' + data[grande].id_casamento + '/">' + data[grande].texto + '</a></p>';
											//$('#depo').html(depoimento);
											l_ativo = true;
											$('#listaImoveisL').css('backgroundPosition','left top');
											//if ((numero_imoveis - grande) > 2) {
											if ((numero_imoveis - grande) > 1) {
												$('#listaImoveisR').css('backgroundPosition','left top');
												r_ativo = true;
											} else {
												$('#listaImoveisR').css('backgroundPosition','left bottom');
												r_ativo = false;
											}
											$($('#listaImoveis img')[grande]).animate({ width: '240px', marginTop: '0px' },200,function(){
												$(this).attr('src','upload/casamento/redimensionada/'+data[grande].arquivo);
											});
										});
									});
								}
								return false;
							});
							
						}
					});
				//});
			});
		//});
	});
return false;
});
