$(document).ready(function() {
	
	//home
	if ($("#home").length>0) {
		var i = 0;
		$("#home .mask li").each(function(){
			var obj = $("#home .menu li:eq("+i+") a");
			var lnk = obj.attr("href");
			var txt = obj.html();
			$(this).append('<a href="'+lnk+'">Abrir</a>');
			i++;
		});
		$("#home .mask li").hover(function(e){
			var lihtml = $(this).html();
			var lipos = $(this).offset();
			var x = lipos.left;
			var y = lipos.top;
			var dir = "left";
			if ($(this).hasClass("last")) {
				dir = "right";
				x = x-(142*6);
				$("#home .ovr").css("left","auto");
			}
			$("#home .ovr").stop().html('<span>'+lihtml+'</span>').css({"width":"142px", "top":y+"px", "display":"block"}).css(dir,x+"px")
			.hover(function(e){
				e.stopPropagation();
				$(this).stop().animate({width: '284px'},500);
			},function(){
				$(this).stop().css("width","142px").hide();
			})
			.click(function(){
				var lnk = $(this).find("a").attr("href");
				window.location = lnk;
			});
		});
	}
	
	//animacao
	if ($("#animacao").length>0) {
		$("#animacao .slides").after('<div id="anav">').cycle({
			fx:		'cover,uncover,scrollVert,scrollHorz,growX,growY', //http://jquery.malsup.com/cycle/browser.html
			randomizeEffects: true,
			pager:	'#anav'
		});
		$("#animacao .slide").click(function(){
			var lnk = $(this).find("a").attr("href");
			window.location = lnk;
		});
		setTimeout("anav()",15000);
	}
	
	//estilização do filtro
	if ($("#filtro").length>0) {
		$("#filtro>li").each(function(){
			$(this).wrapInner('<div class="base" />');
			
		});
	}
	
	//"topo" nos Hs
	$("h3.topo").each(function(){
		var h = $(this).html();
		$(this).html('<span>'+h+'</span><a href="#" class="topo">Topo ^</a>');
	});
	
	//rolagem até o topo
	$('.topo').live('click',function(){
		$('html, body').animate({scrollTop:0}, 1000);
		return false;
	});

	//campo bairros na busca
	letras_bairros();
	$("#busca .divbairros select").click(function(){
		$(this).html('');
		$(this).slideUp("slow",function(){
			$("#busca .divbairros .bairros").slideDown("slow");
		});
	});
	$("#busca select, #busca .codigo, #busca .direita, #header, #mainContent").click(function(){
		$("#busca .divbairros .bairros:visible").slideUp("slow",function(){
			$("#busca .divbairros .bairros label").show();
			$("#busca .divbairros select").slideDown("slow",function(){
				c = conta_bairros();
				if (c>0) {
					if (c==1) s = ""; else s = "s";
					$(this).html('<option value="" selected="selected">'+c+' selecionado'+s+'</option>');
				} else {
					$(this).html('<option value="" selected="selected">Nenhum bairro selecionado</option>');
				}
			});
		});
	});
	function conta_bairros(){
		c = 0;
		$("#busca .divbairros .bairros input").each(function(){
			if ($(this).is(":checked")) c++;
		});
		return c;
	}
	//busca
	carregaBairros();
	$("#busca_cidade").change(function(){ carregaBairros(); });		
	if ($("#cidades").length>0) {
		carregaBairrosAlerta();
		$("#cidades").change(function(){ carregaBairrosAlerta(); });	
	}
	//submit
	$("#fbusca").submit(function(){
		var livre = $.trim($("#placa").val());
		if (isNaN(livre)) { $("#fbusca").attr('action','imoveis.php'); }
		//else 			  { $("#fbusca").attr('action','detalhes.php'); }	 
	});		
	//valores
	$("#de_locacao").hide();
	$("#ate_locacao").hide();
	trocar_finalidade();
	$("#finalidade").change(function()	{ trocar_finalidade(); });
	
	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}

	//fotos do imovel
	if ($("#fotos").length>0) {
		$('#detalhes .ifoto a').click(function(){ $('#fotos .items a:first').click(); return false; });
		$('#fotos .items a').lightBox();
		$("#fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}
	
	//imprimir
	$("#detalhes .links .print").click(function(){
		lnk = $(this).attr("href");
		window.open(lnk,'print','width=600,height=500,scrollbars=yes');
		return false;
	});
	
	//imoveis da direita
	if ($("#imoveisRight").length>0) {
		$("#imoveisRight .imovel:odd").addClass("odd");
	}

	//favoritos
	$(".numfav").load('inc/lib/fnc_favoritos.php?act=count_imo');
	$("a.fav").click(function(event){
		var codigo = $(this).attr("rel");
		var fav = $(this);
		if ($(this).hasClass("favon")) {
			$.get("inc/lib/fnc_favoritos.php?act=del_imo&delid="+codigo,
				function(data){ fav.removeClass("favon").html("Adicionar aos favoritos"); $(".numfav").load('inc/lib/fnc_favoritos.php?act=count_imo'); }
			);
		} else {
			 $.get("inc/lib/fnc_favoritos.php?act=add_imo&addid="+codigo,				
				function(data){ fav.addClass("favon").html("Remover dos favoritos"); $(".numfav").load('inc/lib/fnc_favoritos.php?act=count_imo'); }
			);
		}
		event.stopPropagation();
		return false;
	});

	//ligamos para voce
	if ($("#ligamos").length>0) {
		$("#ligamos").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$("#menu2 a.m2-2").click(function(){
			$("#ligamos").dialog('open');
			return false;
		});
	}
	
	//enviar por email
	if ($("#enviar-por-email").length>0) {
		$("#enviar-por-email").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$("#detalhes .links .email").click(function(){
			$("#enviar-por-email").dialog('open');
			return false;
		});
	}
	
	//restrita
	if ($("#restrita").length>0) {
		$("#restrita").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$("#servicos .restrita").click(function(){
			$("#restrita").dialog('open');
			return false;
		});
	}	
	
	//imoveis lateral
	if ($("#imoveisRight").length>0) {
		$("#imoveisRight .imovel:odd").addClass("odd");
		$("#imoveisRight .imovel:last").addClass("last");
	}
	
	//submit
	$("#busca").submit(function(){
		var livre = $.trim($("#placa").val());
		if (isNaN(livre)) { $("#busca").attr('action','imoveis.php'); }
		else 			  { $("#busca").attr('action','detalhes.php'); }	 
	});		
		
	
});

function anav() {
	i = 0;
	$("#animacao .slide").each(function(){
		var title = $(this).attr("title");
		$(this).attr("title","");
		$("#anav a:eq("+i+")").html(title);
		i++;
	});
}


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,"#193E8C",2,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(28, 31);
				markerOptions = { icon:icone };
				map.addOverlay(new GMarker(latlng, markerOptions));
				//map.addOverlay(new GMarker(latlng));
			}
		}
	}
}

function letras_bairros() {
	if($.browser.msie && $.browser.version=="6.0") {
		$("#busca .divbairros .bairros p").hide();
	} else {
		$("#busca .divbairros .bairros").prepend("<p></p>");
		letras = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		letras_usadas = "";
		$("#busca .divbairros .bairros 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">Todos</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 .divbairros .bairros p").html(letras_links);
		$("#busca .divbairros .bairros p a.letra").click(function(){
			letra = $(this).html();
			$("#busca .divbairros .bairros 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 .divbairros .bairros p a.todos").click(function(){
			$("#busca .divbairros .bairros label").show();
			return false;
		});
	}
}

function carregaBairros() {
	var cidade = $('#busca_cidade').val();
	var lista  = $('#listabairros').val();
	$(".bairros").load("inc/lib/fnc_load-busca.php?act=bairros", {cidade: cidade, lista: lista},
		function(data){
		letras_bairros();
	})			
}		


function trocar_finalidade() {
	if ($("#finalidade").val()=='locacao') {
		$("#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');
	}	
}


function carregaBairrosAlerta() {
	var cidade = $('#cidades').val();

	$("#bairros-alerta").html('<div id="bairros-alerta">Carregando...</div>');

	 $("#bairros-alerta").load("inc/lib/fnc_load-alerta.php?act=bairros", {cidade: cidade},
		function(data){
			$("#bairros-alerta").html(data)
		})			
}	

