$(document).ready(function() {
	
	//animacao das frases
	$("#header #headerRight .left").cycle({ 
		fx:      'fade',
		speed:    300, 
		timeout:  4000
	});
	
	//alinhamento das frases
	var w = $("#header #menu").width();
	w = w+3;
	$("#header #headerRight").css("width",w+"px");

	//labelify
	$.getScript("inc/js/jquery.labelify.js",function(xhr){
		var aplicar_labelify = "#busca .codigo";
		try {
			$(aplicar_labelify).labelify();
		} catch(err) {
			eval(xhr);
			$(aplicar_labelify).labelify();
		}
	});

	//logos da home
	if ($("#homelogos").length>0) {
		$("#homelogos .scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 5
		});
	}
	
	//over dos imoveis
	$(".imoveis .imovel").mouseover(function(){
		$(this).addClass("active");
	});
	$(".imoveis .imovel").mouseout(function(){
		$(this).removeClass("active");
	});
	
	//arruma tooltips dos imoveis
	$(".imoveis .imovel").each(function(){
		imv = $(this);
		imv.attr("title","");
		imv.find("img").attr("alt","");
		imv.find(".ft, .tit, .desc, .label span")
			.attr("title","Ver mais detalhes")
			.addClass("tooltip")
			.css("cursor","pointer")
	});
	$(".imoveis .imovel .ft, .imoveis .imovel .tit, .imoveis .imovel .desc, .imoveis .imovel .label span").click(function(){
		href = $(this).parents(".imovel").find(".ft a").attr("href");
		window.location = href;
		return false;
	});


	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}

	//fotos do imovel
	if ($("#fotos").length>0) {
		$('#fotos .items a').lightBox();
		$("#fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}
	
	//busca
	letras_bairros();
	$("#busca .divcidades select").click(function(){
		$(this).slideUp("fast",function(){
			$("#busca .divcidades .cidades").slideDown("fast");
		})
	});
	$("#header, select, h1, h2, h3, #animacao, #footer, #leftContent, #rightContent, .imoveis, #contato, .codigo, .submit").click(function(){
		$("#busca .divcidades .cidades:visible").slideUp("fast",function(){
			$("#busca .divcidades select").slideDown("fast",function(){
				c = conta_bairros();
				if (c>0) {
					if (c==1) s = ""; else s = "s";
					$(this).html('<option value="" selected="selected">'+c+' cidade'+s+' selecionada'+s+'</option>');
				} else {
					$(this).html('<option value="" selected="selected">Todas cidades</option>');
				}
			});
		});
	});
	
	//aguarde
	$("#social li a").click(function(){
		if($(this).attr("href")=='#'){ alert("Aguarde!"); }
	});
	
	//ligamos pra você
	if ($("#ligamos").length>0) {
		$("#ligamos").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$(".menu2-3").click(function(){
			$("#ligamos").dialog('open');
			return false;
		});
	}	

	//rolagem
	$('.subir').click(function(){
		$('html, body').animate({scrollTop:0}, 1000);
	});
	
	$('.voltar').click(function(){
		history.go(-1);
	});		
	
	$.getScript("inc/js/fav.js");
	
	carregaBairros();
	$("#busca_cidade").change(function(){ carregaBairros(); });		
	
	//submit
	$("#fbusca").submit(function(){
		var livre = $.trim($(".codigo").val());
		if (isNaN(livre)) { $("#fbusca").attr('action','imoveis.php'); }
		else 			  { $("#fbusca").attr('action','detalhes.php'); }	 
	});		
	
	//busca
	$("#de_locacao").hide();
	$("#ate_locacao").hide();
	trocar_finalidade();
	$("#finalidade").change(function()	{ trocar_finalidade(); });
	
});

function mapa_imovel(l1,l2,tipo,controles,mapid) {
	if ( tipo === undefined ) tipo = 'ponto';
	if ( controles === undefined ) controles = true;
	if ( mapid === undefined ) mapid = "map";
	if ($("#"+mapid).length>0) {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById(mapid));
			map.setCenter(new GLatLng(l1,l2), 15);
			var latlng = new GLatLng(l1,l2);
			if (controles) {
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());
			}
			if (tipo=='circulo') {
				var marcador = GPolygon.Circle(latlng, 500,"#4D6C6B",3,1,"#000",0.2)
				map.addOverlay(marcador);
			} else {
				var icone = new GIcon(G_DEFAULT_ICON);
				icone.image = "img/design/gmaps_icon.png";
				icone.iconSize = new GSize(21, 31);
				markerOptions = { icon:icone };
				map.addOverlay(new GMarker(latlng, markerOptions));
			}
		}
	}
}

function letras_bairros() {
	if($.browser.msie && $.browser.version=="6.0") {
		$("#busca .divcidades .cidades p").hide();
	} else {
		$("#busca .divcidades .cidades").prepend("<p></p>");
		letras = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		letras_usadas = "";
		$("#busca .divcidades .cidades label").each(function(){
			str = $(this).html();
			v = str.split("> ");
			nome_bairro = v[1];
			letras_usadas += nome_bairro.substr(0,1);
		});
		letras_links = '<a href="#" class="todos">Todas</a>&nbsp;';
		for (i=0;i<=letras.length-1;i++) {
			tem = letras_usadas.indexOf(letras.substr(i,1));
			if (tem>=0) {
				letras_links += '<a href="#" class="letra">'+letras.substr(i,1)+'</a>';
			} else {
				letras_links += letras.substr(i,1);
			}
		}
		$("#busca .divcidades .cidades p").html(letras_links);
		$("#busca .divcidades .cidades p a.letra").click(function(){
			letra = $(this).html();
			$("#busca .divcidades .cidades label").each(function(){
				str = $(this).html();
				v = str.split("> ");
				nome_bairro = v[1];
				bletra = nome_bairro.substr(0,1);
				if (bletra==letra) $(this).show(); else $(this).hide();
			});
			return false;
		});
		$("#busca .divcidades .cidades p a.todos").click(function(){
			$("#busca .divcidades .cidades label").show();
			return false;
		});
	}
}

function conta_bairros(){
	c = 0;
	$("#busca .divcidades .cidades input").each(function(){
		if ($(this).is(":checked")) c++;
	});
	return c;
}

function carregaBairros() {
	//var cidade = $('#busca_cidade').val();
	var lista  = $('#listacidades').val();
	$(".cidades").load("inc/lib/fnc_load-busca.php?act=cidades", {act:"cidades", lista: lista},
		function(data){
		letras_bairros();
	})			
}	

function trocar_finalidade() {
	if ($("#finalidade").val()=='aluguel') {
		$("#de_venda").css('display','none');
		$("#ate_venda").css('display','none');
		$("#de_locacao").css('display','block');
		$("#ate_locacao").css('display','block');
	}else{
		$("#de_locacao").css('display','none');
		$("#ate_locacao").css('display','none');
		$("#de_venda").css('display','block');
		$("#ate_venda").css('display','block');
	}	
}
