function ufcid() {
	var uc = $("#busca #ufcidade").val();
	if (uc=='') {
		$("#busca #uf").val('');
		$("#busca #cidade").val('');
		carrega_bairros();
	} else {
		var v = uc.split('-');
		var uf = v[0];
		var cidade = v[1];
		$("#busca #uf").val(uf);
		$("#busca #cidade").val(cidade);
		carrega_bairros(cidade);
	}
}

function isiPad(){
    return (navigator.platform.indexOf("iPad") != -1);
}

$(document).ready(function() {
	
	//aol pop
	if ($("#aol-pop").length>0) {
		var w = $(window).width();
		var c = (w/2)-126;
		$("#aol-pop").css("left",c+"px");
		$("#aol-pop a").click(function(){
			$("#aol-pop").hide();
			//return false;
		});
	}
	
	$("#aol-pop").animate({ top:350, opacity:1 },2500);
	
	//header fones
	$("#menu2 li:last a").hover(function(){
		$("#telefones").fadeIn(100);
	},function(){
		$("#telefones").fadeOut(100);
	});
	
	if (isiPad()) {
		$("body").addClass("ipad");
		$("#twitter h5").css("background-position","180px 0");
		$("#menu li ul li").css("width","220px");
		$("#menu>li:eq(1)>a").click(function(){
			var ul = $(this).parents("li").find("ul");
			if (ul.is(":visible")) ul.hide(); else ul.show();
			return false;
		});
		$("#menu2 a.fones").click(function(){
			var ul = $("#telefones");
			ul.css("margin-left","810px");
			if (ul.is(":visible")) ul.hide(); else ul.show();
			return false;
		});
	}
	
	//marca newsletter por padrão
	$(".newsletter-check input[type=checkbox]").attr("checked",true);
	
	//busca checkbox
	$("#busca .fin input:checked").parents("label").addClass("active");
	$("#busca .fin input").change(function(){
		$("#busca .fin label").removeClass("active");
		if ($("#busca .fin input:checked").val()=='venda') {
			$("#busca .fin label:eq(0)").addClass("active");
		} else {
			$("#busca .fin label:eq(1)").addClass("active");
		}
	});
	
	//busca cidade/estado
	if ($("#busca #ufcidade").length>0) {
		ufcid();
		$("#busca #ufcidade").change(function(){
			ufcid();
		});
	}
	
	//abas da home
	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).attr("title","");
			$(this).wrapInner('<div class="abas-imoveis" />');
			if ($(this).find(".abas-imoveis .imovel").length>4) {
				$(this).find(".abas-imoveis").after('<div class="abas-nav"><a href="#" class="btn p">P</a><a href="#" class="btn n">N</a></div>');
				var lis = $(this).find(".abas-imoveis .imovel");
				for (var i = 0; i < lis.length; i+=4) { lis.slice(i, i+4).wrapAll('<div class="set"></div>'); }
				$(this).find(".abas-imoveis").cycle({
					fx:		'scrollHorz',
					pauseOnPagerHover: true,
					pause:	true,
					prev:	'#aba_'+abacount+' a.p',
					next:	'#aba_'+abacount+' a.n',
					timeout:0
				});
			}
		});
		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" />');
		});
	}
	
	//muda registros por pagina
	$("select#imoveispp").change(function(){
		var lnk = $("#url-retorno").val();
		window.location = lnk+'&ipp='+$(this).val();
	});
	
	
	//indicar este imovel
	if ($("a.open-sendfav").length>0) {
		$("body").append('<div id="frame-sendfav">');
		var url_retorno = $("#url-retorno").val();
		$("#frame-sendfav").load("enviar_favoritos.php",function(){
			$(this).find("input[name=url_retorno]").val(url_retorno);
			$("#sendfav").dialog({
				autoOpen: false,
				bgiframe: true,
				minHeight: 140,
				modal: true,
				width: 340,
				resizable: false
			});
			$("#envio-email").hide();
			$("input[name=enviar]").click(function(){
				if ($("input[name=enviar]:checked").val()=='c') {
					$("#envio-corretor").show();
					$("#envio-email").hide();
				} else {
					$("#envio-corretor").hide();
					$("#envio-email").show();
				}
			});
		});
		$("a.open-sendfav").click(function(){
			$("#sendfav").dialog('open');
			return false;
		});
	}
	
	//abrir simulador
	$("a.open-caixa").click(function(){
		window.open("http://www8.caixa.gov.br/siopiinternet/simulaOperacaoInternet.do?method=inicializarCasoUso","caixa","width=480,height=550");
		return false;
	});
	
	//newsletter
	$("#nws #newsletter").hide();
	$("#nws a.assine").click(function(){
		if (!$("#nws #newsletter").is(":visible")) {
			$("#nws #newsletter").slideDown(300);
		} else {
			$("#nws #newsletter").slideUp(300);
		}
		return false;
	});
	
	
});

