$(document).ready(function() {
	
	//abas da home
	if ($("div.abas-home").length>0) {
		var abacount = 0;
		$(".abas-home").each(function(){
			var a = $(this);
			var i = 0;
			a.prepend('<ul class="abas-tabs"></ul>')
			a.find(".abas-conteudo").each(function(){
				abacount++;
				$(this).attr("id","aba_"+abacount);
				a.find(".abas-tabs").append('<li><a href="#aba_'+abacount+'"><span>'+$(this).attr("title")+'</span></a></li>');
				$(this).wrapInner('<div class="abas-imoveis" />');
			});
			a.find(".abas-conteudo").hide();
			a.find(".abas-conteudo:first").show();
			a.find(".abas-tabs li:first a").addClass("active");
		});
		$(".abas-home .abas-tabs a").click(function(){
			var a = $(this).parents(".abas-home");
			a.find(".abas-conteudo:visible").hide();
			a.find(".abas-tabs a.active").removeClass("active");
			var id = $(this).attr("href");
			a.find(id).show();
			$(this).addClass("active");
			return false;
		});
	}

	//estilização do filtro
	if ($("#filtro-lateral").length>0) {
		$("#filtro-lateral>li").each(function(){
			$(this).wrapInner('<div class="base" />');
		});
	}

	//estilização do blog (lateral)
	if ($(".xoxo").length>0) {
		$(".xoxo>li").addClass("box").each(function(){
			$(this).wrapInner('<div class="base" />');
		});
	}

	//estilização do blog (post)
	if ($("#content .post").length>0) {
		$("#content .post:last").addClass("last");
	}
	
	//fotos do imovel
	if ($("#detalhes-foto .fotos .foto").length>0) {
		var f = $("#detalhes-foto .fotos");
		if ($("#detalhes-foto .fotos .foto").length>5) {
			f.wrapInner('<div class="slides" />');
			f.find(".slides").before('<a href="#" class="btn p">P</a>').after('<a href="#" class="btn n">N</a>');
			var lis = f.find(".foto");
			for (var i = 0; i < lis.length; i+=5) { lis.slice(i, i+5).wrapAll('<div class="set" />'); }
			f.find(".slides").cycle({
				fx:		'scrollVert',
				rev:	true,
				prev:	'#detalhes-foto a.p',
				next:	'#detalhes-foto a.n',
				timeout:	0
			});
		}
		$("#detalhes-foto .base img").hide();
		$("#detalhes-foto .base img:first").show();
		f.find(".foto:first a").addClass("active");
		f.find(".foto a").click(function(){
			var path = $(this).attr("href");
			f.find(".active").removeClass("active");
			$(this).addClass("active");
			$("#detalhes-foto .base img:visible").fadeOut("fast",function(){
				$("#detalhes-foto .base img[src="+path+"]").fadeIn("fast");
			});
			return false;
		});
	}
	
	//newsletter
	if ($("#rightContent .lateral-links").length>0) {
		$("body").append('<div id="frame-newsletter">');
		var url_retorno = $("#url-retorno").val();
		$("#frame-newsletter").load("inc/php/newsletter.php",function(){
			$(this).find("input[name=url_retorno]").val(url_retorno);
			$("#newsletter").dialog({
				autoOpen: false,
				bgiframe: true,
				minHeight: 140,
				modal: true,
				width: 330,
				resizable: false
			});
			$("div#newsletter form").submit(function(){
				ereg = /(^[A-Za-z0-9_.-]+@([A-Za-z0-9_.-]+\.)+[A-Za-z]{2,4}$)/;
				var email = $(this).find("#newsletter-email").val();
				var nome = "";
				var celular = $(this).find("#newsletter-celular").val();
				if (!ereg.test($(this).find("#newsletter-email").val())) {
					alert("Informe um e-mail válido.");
				} else {
					var ok = true;
					nome = $(this).find("#newsletter-nome").val();
					if ($(this).find("#newsletter-nome").length>0) {
						if ($(this).find("#newsletter-nome").val()=="") {
							ok = false;
							alert('Informe o seu nome.');
						}
					}
					if (ok) {
						var lnk = $(this).attr("action");
						$.get(lnk, {email:email, nome:nome, celular:celular, p:'newsletter'},function(data){
							alert(data);
							$("#newsletter").dialog('close');
						});
					}
				}
				return false;
			});
		});
		$("#rightContent .lateral-links a").click(function(){
			var href = $(this).attr("href");
			if (href=="inc/php/newsletter.php") {
				$("#newsletter").dialog('open');
				return false;
			}
		});
	}
	
	//alerta
	$(".de_locacao").hide();
	$(".ate_locacao").hide();
	$(".finalidade").change(function()	{ trocar_finalidade_alerta(); });	

});

function trocar_finalidade_alerta() {
	if ($(".de_venda").css('display')=='none') {
		$(".de_locacao").css('display','none');
		$(".ate_locacao").css('display','none');
		$(".de_venda").css('display','block');
		$(".ate_venda").css('display','block');
	}else{
		$(".de_venda").css('display','none');
		$(".ate_venda").css('display','none');
		$(".de_locacao").css('display','block');
		$(".ate_locacao").css('display','block');
	}	
}	
	
	
	

