function mapa_imovel(l1,l2,tipo) {
	if ( tipo === undefined ) tipo = 'ponto';
	if ($("#map").length>0) {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(l1,l2), 15);
			var latlng = new GLatLng(l1,l2);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			if (tipo=='circulo') {
				var marcador = GPolygon.Circle(latlng, 500,"#0071BC",3,1,"#000",0.2)
				map.addOverlay(marcador);
			} else {
				map.addOverlay(new GMarker(latlng));
			}
		}
	}
}

function trocar_finalidade() {
	if ($("#finalidade").val()=='VENDA') {
		$("#preco_locacao").css('display','none');
		$("#preco_venda").css('display','block');
	}	
	if ($("#finalidade").val()=='ALUGUEL') {
		$("#preco_venda").css('display','none');
		$("#preco_locacao").css('display','block');
	}
}

function carregaBairros() {
	var cidade = $('#busca_cidade').val();

	$("#listabairros").html('<select name="busca_cidade" id="busca_cidade"><option value="">Carregando...</option></select>');

		 $.get("inc/lib/fnc_load-busca.php?act=bairros", {cidade: cidade},
			function(data){
				$("#listabairros").html(data);
			})			
}				
			

$(document).ready(function() {
	
	//favoritos
	$.getScript("inc/js/fav.js");
	
	//bairros busca
	carregaBairros();
	$("#busca_cidade").change(function(){ carregaBairros(); });					
	
	//aol flutuante
	if ($('#aol.naomover').length==0) $("#aol").makeFloat({x:"current",y:"current"});
	
	//se for ie6, carrega menu por js
	if($.browser.msie && $.browser.version=="6.0") $.getScript("inc/dropdown/jquery.dropdown.js");
	
	//busca
	$("#preco_locacao").hide();
	$("#finalidade").change(function()	{ trocar_finalidade(); });
	trocar_finalidade();

	//manter hover do menu principal ativo quando vai pro submenu
	$("#header #menu li.dir ul").mouseover(function(){
		$(this).parents("li.dir").find("a:first").addClass("active");
	});
	$("#header #menu li.dir ul").mouseout(function(){
		$(this).parents("li.dir").find("a:first").removeClass("active");
	});
	
	//link nas faixas
	$(".homeimoveis .imovel .faixa").css("cursor","pointer");
	$(".homeimoveis .imovel").click(function(){
		lnk = $(this).find(".detalhes").attr("href");
		window.location = lnk;
	});
	
	//animacao da home
	if ($('#animacao').length>0) {
		$('#animacao').Horinaja({
			capture: 'animacao',
			delai: 0.3,
			duree: 4,
			pagination: true,
			nCell: 100
		});
	}

	/*** ABAS ***/
	if ($('#homeTabs').length>0) {
		$('#homeTabs .htabs li a:first').addClass("active");
		id = $('#homeTabs .htabs li a:first').attr("href");
		$(id).show();
		$('#homeTabs .htabs li a').click(function(){
			idnew = $(this).attr("href");
			idact = $('#homeTabs .htabs li a.active').attr("href");
			if (idnew!=idact) {
				$('#homeTabs .htabs li a.active').removeClass("active");
				$(this).addClass("active");
				$(idact).fadeOut("fast",function(){
					$(idnew).fadeIn("fast");
				});
			}
			return false;
		});
	}


	//busca
	if ($('#busca2').length>0) {
		var txtcod = $("#busca2 .codigo, #busca .codigo").val();
		$("#busca2 .codigo, #busca .codigo").focus(function(){
			if ($(this).val()==txtcod) $(this).val("");
		});
		$("#busca2 .codigo, #busca .codigo").blur(function(){
			if ($(this).val()=="") $(this).val(txtcod);
		});
	}
	if ($('#busca').length>0) {
		var txtcod = $("#busca .codigo").val();
		$("#busca .codigo").focus(function(){
			if ($(this).val()==txtcod) $(this).val("");
		});
		$("#busca .codigo").blur(function(){
			if ($(this).val()=="") $(this).val(txtcod);
		});
	}

	//submit
	$("#busca form, #busca2 form").submit(function(){
		var livre = $.trim($(this).find(".codigo").val());
		if (isNaN(livre)) { $(this).attr('action','imoveis.php'); }
		else 			  { $(this).attr('action','detalhes.php'); }	 
	});

	//twitter
	if ($("#twitter").length>0) {
		$("#twitter .tweets").getTwitter({
			userName: "amagaiimoveis",
			numTweets: 3,
			loaderText: "Aguarde, carregando...",
			slideIn: false,
			showHeading: false,
			showProfileLink: false,
			showTimestamp: false,
			beforeEachTweet: "Amagai:"
		});
	}
	
	//newsletter
	if ($("#newsletter").length>0) {
		var nnome = $("#newsletter .nnome").val();
		var nemail = $("#newsletter .nemail").val();
		$("#newsletter .nnome").focus(function(){
			if ($(this).val()==nnome) $(this).val("");
		});
		$("#newsletter .nnome").blur(function(){
			if ($(this).val()=="") $(this).val(nnome);
		});
		var nemail = $("#newsletter .nemail").val();
		$("#newsletter .nemail").focus(function(){
			if ($(this).val()==nemail) $(this).val("");
		});
		$("#newsletter .nemail").blur(function(){
			if ($(this).val()=="") $(this).val(nemail);
		});
	}
	
	//label nos links da detalhes
	if ($("#detalhes").length>0) {
		$("#detalhes .foto .links ul a").mouseover(function(){
			txt = $(this).html();
			$("#detalhes .foto .links ul li.label").html(txt);
		});
		$("#detalhes .foto .links ul a").mouseout(function(){
			$("#detalhes .foto .links ul li.label").html('');
		});
	}
	
	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}

	//fotos do imovel
	if ($("#detalhes .info .fotos").length>0) {
		$('#detalhes .info .fotos .items a').lightBox();
		$("div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}

	//rolagem até o mapa
	if ($("#detalhes .foto .links a.mapa").length>0 && $("#localizacao #map").length>0) {
		$("#detalhes .foto .links a.mapa").click(function(){
			$("html, body").animate({
				scrollTop: $("#localizacao").offset().top
			}, 2000);
			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
		});
		$(".abre_enviar").click(function(){
			//$("#enviar-por-email").dialog('open');
			//return false;
		});
	}

});
