// requer jquery
$(function(){

	var ftsize = 12;
	var h2size = 18;
	var h3size = 14;
	var ndsize = 10;
	
	// adiciona o google chrome a lista de navegadores
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
	
	// fix background imagem on ie
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	
	// links com href="#"
	$('a[href=#]').click(function(){
		alert('Este recurso está em desenvolvimento.');
		return false;
	});
	
	// anima o banner 
	trocaBanner(1);
	
	// clique no banner 
	$('#banner ul li a').click(function(){
		clearTimeout(bcount);
		bid = $(this).attr('href').replace('#b','');
		trocaBanner(bid);
		return false;
	});

	// tamanho da fonte
	$('#fontsize a').click(function(){
		if ($(this).attr('class')=='small') {
			if (ftsize > 10) {
				ftsize = ftsize-2;
				h2size = h2size-2;
				h3size = h3size-2;
				ndsize = ndsize-2;
			}
		} else {
			if (ftsize < 16) {
				ftsize = ftsize+2;
				h2size = h2size+2;
				h3size = h3size+2;
				ndsize = ndsize+2;
			}
		}
		$('#content').css({'font-size':ftsize+'px'});
		$('#content h2').css({'font-size':h2size+'px'});
		$('#content h3').css({'font-size':h3size+'px'});
		$('div.notData').css({'font-size':ndsize+'px'});
		return false;
	});

	// fancybox
	$("a.zoom").each(function(){
		$(this).fancybox({ 
			'frameWidth':640,
			'frameHeight':480,
			'hideOnContentClick': false
		}); 
	});
	
	// popups indicar, corretor e newsletter
	$("a.indicar, a.corretor, #sidebar a.news").each(function(){
		$(this).fancybox({ 
			'frameWidth':520,
			'frameHeight':380,
			'hideOnContentClick': false
		}); 
	});
	
	// popups previsao do tempo
	$("a.tempo").each(function(){
		$(this).fancybox({ 
			'frameWidth':360,
			'frameHeight':300,
			'hideOnContentClick': false
		}); 
	});
	
	// atendimento online
	$('a.online').click(function(){
		window.open($(this).attr('href'), 'chat', 'width=550,height=550,top=50,left=100,status=1,location=1');
		return false;
	});
	
	// busca pela referencia desabilita as outras opcoes
	$('#referencia').focus(function(){
		$(this).parent('div').parent('form').find('select,input[type=radio],input[type=checkbox]').attr('disabled','disabled');
	});
	
	// verifica se digitou um numero na referencia
	$('#referencia').blur(function(){
		num = $.trim($(this).val());
		if (num=='') {
			$(this).parent('div').parent('form').find('select,input[type=radio],input[type=checkbox]').attr('disabled',false);
		} else if (!numero(num)) {
			alert('Informe uma referência válida (somente números).');
			$(this).val('');
			$(this).focus();
		}
	});
	
	// album de fotos dos imoveis
	$('div.fotos a, #fotos a').fancybox();
	
	// simulador caixa
	$("a.caixa").each(function(){
		$(this).fancybox({ 
			'frameWidth':500,
			'frameHeight':530,
			'hideOnContentClick': false
		}); 
	});
	
	// opcoes de preço de venda e locacao
	$('input.operacao').click(function(){
		preco_options($(this).val());
	});

	// opcoes de preço de venda e locacao
	$('input.operacao:radio:checked').each(function(){
		preco_options($(this).val());
	});
	
	// hover na lista de imoveis
	$('div.c3 div.item, div.lista div.imovel, div.c1 div.destaque, div.noticias div.item').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});

	// clique na lista de imoveis
	$('div.c3 div.item, div.lista div.imovel, div.c1 div.destaque, div.noticias div.item').click(function(){
		location.href = $(this).find('a').attr('href');
	});
	
	// rola a lista para o imovel em destaque
	$('div.lista').each(function(){
		var x = $(this).find('div.active').offset().top - 306;
		$(this).animate({scrollTop: x}, 0);
	});
	
	// clique nas abas da home
	$('#tabs li a').click(function(){
		aba = $(this).parent('li').attr('class');
		$('#content div.text div[id^=aba]').addClass('hidden');
		$('#content div.text div#'+aba).removeClass('hidden');
		$('#tabs li a').removeClass('active');
		$('#tabs li.'+aba+' a').addClass('active');
		return false;
	});
	
	// abas do hotsite
	$('#hotsite ul li a').click(function(){
		tab = $(this).attr('href').replace('#','');
		$('#hotsite div.texto, #hotsite ul li a').removeClass('active');
		$('#hotsite div#'+tab+', #hotsite ul li a.'+tab).addClass('active');
		if ($('#hotsite div#'+tab).hasClass('mapa')) {
			$('#hotsite div.fotos').hide();
			$('#hotsite div.mapa iframe').each(function(){
				$(this).attr({
					src: $(this).attr("src")
				});

			});
		} else {
			$('#hotsite div.fotos').show();
		}
		return false;
	});
	
});

// opcoes de preço de venda e locacao
function preco_options(v){
	if (v=='L') {
		$('select.preco_locacao').show();
		$('select.preco_venda').hide();
	} else {
		$('select.preco_venda').show();
		$('select.preco_locacao').hide();
	}
}

// filtro referencia
function filtroReferencia(){
	ref = parseInt($('form.filter input.referencia').val());
	if(numero(ref)){
		location.href = './?i='+ref;
	} else {
		alert('Informe uma referência válida (somente números)');
	}
}

// valida somente numeros
function numero(str) {
	var reDigits = /^\d+$/;
	return (reDigits.test(str));
} 

// animando o banner principal
function trocaBanner(banner) {
	var btotal = $('#banner').children('a.item').size(); // total de banners
	if (banner > btotal) banner=1;
	$('#banner a.item').fadeOut('slow');
	$('#banner a.b'+banner).each(function(){
		$(this).fadeIn('slow');
		$(this).find('div').hide().show('slow');
	});
	$('#banner ul li a').removeClass('active');
	$('#banner ul li.b'+banner+' a').addClass('active');
	banner++;
	bcount = setTimeout('trocaBanner('+banner+')', 5000);
}

