
function mapa_imovel(l1,l2,tipo,controles) {
	if ( tipo === undefined ) tipo = 'ponto';
	if ( controles === undefined ) controles = true;
	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);
			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 .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 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)
		})			
}	


$(document).ready(function() {

	//se for ie6, roda o pngfix
	if($.browser.msie && $.browser.version=="6.0") {
		$.getScript("inc/js/jquery.bgiframe.js")
		$.getScript("inc/js/jquery.pngfix.js",function(xhr){
			var aplicar_pngfix = "#menu, #hbg-mask";
			try {
				$(aplicar_pngfix).pngfix();
			} catch(err) {
				eval(xhr);
				$(aplicar_pngfix).pngfix();
			}
		});
	}
	
	//labelify
	$.getScript("inc/js/jquery.labelify.js",function(xhr){
		var aplicar_labelify = "#busca .codigo, #newsletter .text, #news .text";
		try {
			$(aplicar_labelify).labelify();
		} catch(err) {
			eval(xhr);
			$(aplicar_labelify).labelify();
		}
	});	
	
	$('#hbg-animacao').crossSlide({
	  fade: 1
	}, [
	  {
		src:  'img/animacao/1.jpg',
		from: 'top center 1x',
		to:   'bottom center 1x',
		time: 3
	  }, {
		src:  'img/animacao/2.jpg',
		from: 'bottom center 1x',
		to:   'top center 1x',
		time: 4
	  }, {
		src:  'img/animacao/3.jpg',
		from: 'top center 1x',
		to:   'bottom center 1x',
		time: 3
	  }, {
		src:  'img/animacao/4.jpg',
		to: 'top center 1x',
		from:   'bottom center 1x',
		time: 3
	  }, {
		src:  'img/animacao/5.jpg',
		to: 'bottom center 1x',
		from:   'top center 1x',
		time: 4
	  }, {
		src:  'img/animacao/6.jpg',
		to: 'top center 1x',
		from:   'bottom center 1x',
		time: 3
	  }
	]);

	$.getScript("inc/js/fav.js");
	
	carregaBairros();
	$("#busca_cidade").change(function(){ carregaBairros(); });		
	
	if (("#cidades").length>0) {
		carregaBairrosAlerta();
		$("#cidades").change(function(){ carregaBairrosAlerta(); });	
	}

	//submit
	$("#fbusca").submit(function(){
		var livre = $.trim($("#codigo").val());
		if (isNaN(livre)) { $("#fbusca").attr('action','imoveis.php'); }
		else 			  { $("#fbusca").attr('action','detalhes.php'); }	 
	});	

	//ligamos para voce
	if (("#ligamos").length>0 && ("#top .t2").length>0) {
		$("#ligamos").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$("#top .t2").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
		});
		$(".abre_enviar").click(function(){
			$("#enviar-por-email").dialog('open');
			return false;
		});
	}	
	//enviar formulario
	if (("#form_contato").length>0) {
		$("#form_contato").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$(".abre_formulario").click(function(){
			$("#form_contato").dialog('open');
			return false;
		});
	}	
	
	//muda texto do campo codigo na busca
	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);
		});
	}

	//fotos do imovel
	if ($("#fotos").length>0) {
		var lbsettings = { containerResizeSpeed: 350 };
		$('#fotos .items a').lightBox(lbsettings);
		$("#fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}
	
	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}


	//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 selecionado</option>');
				}
			});
		});
	});
	function conta_bairros(){
		c = 0;
		$("#busca .divbairros .bairros input").each(function(){
			if ($(this).is(":checked")) c++;
		});
		return c;
	}
	
	//video do imovel
	if ($(".temvideo").length>0 && $(".ifoto .video").length>0) {
		$(".temvideo").click(function(){
			$(".ifoto .foto").fadeOut("fast",function(){
				$(".ifoto .video").fadeIn();
			});
			return false;
		});
		$(".ifoto .video .fechar").click(function(){
			$(".ifoto .video").fadeOut("fast",function(){
				$(".ifoto .foto").fadeIn();
			});
			return false;
		});
	}
	
});

