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 conta_bairros(){
	c = 0;
	$("#busca .divbairros .bairros input").each(function(){
		if ($(this).is(":checked")) c++;
	});
	return c;
}

$(document).ready(function() {
	
	$("body").bind("contextmenu", function() { return false; });
	
	$.getScript("inc/js/fav.js");
	
	//se for ie6, carrega menu por js
	if($.browser.msie && $.browser.version=="6.0") $.getScript("inc/dropdown/jquery.dropdown.js");

	//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");
	});
	
	//busca
	letras_bairros();
	$("#busca .divbairros select").click(function(){
		$(this).slideUp("fast",function(){
			$("#busca .divbairros .bairros").slideDown("fast");
		})
	});
	$("#header, select, h1, h2, h3, #animacao, #footer, #leftContent, #rightContent, .imoveis, #contato, .codigo, .submit").click(function(){
		$("#busca .divbairros .bairros:visible").slideUp("fast",function(){
			//$("#busca .divbairros .bairros label").show();
			$("#busca .divbairros select").slideDown("fast",function(){
				c = conta_bairros();
				if (c>0) {
					if (c==1) s = ""; else s = "s";
					$(this).html('<option value="" selected="selected">'+c+' bairro'+s+' selecionado'+s+'</option>');
				} else {
					$(this).html('<option value="" selected="selected">Nenhum selecionado</option>');
				}
			});
		});
	});
	
	//busca codigo
	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
	$("#fbusca").submit(function(){
		var livre = $.trim($(".codigo").val());
		if (isNaN(livre)) { $("#fbusca").attr('action','imoveis.php'); }
		else 			  { $("#fbusca").attr('action','detalhes.php'); }	 
	});
	
	//animacao
	if ($("#animacao").length>0) {
		$('#animacao').loopedSlider({
			autoStart: 8000,
			restart: 5000,
			containerClick: false,
			addPagination: true
		});
	}
	
	//rolagem 
	if ($(".pesquisa").length>0) {
			$("html, body").animate({
				scrollTop: $("#mainContent").offset().top
			}, 1000);
			//return false;
	}
	if ($("#detalhes").length>0) {
			$("html, body").animate({
				scrollTop: $("#mainContent").offset().top
			}, 1000);
			//return false;
	}
	
	
	//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);
		});
	}
	
	//twitter
	if ($("#twitter").length>0) {
		$("#twitter .tweets").getTwitter({
			userName: "imovelclass",
			numTweets: 3,
			loaderText: "Aguarde, carregando...",
			slideIn: false,
			showHeading: false,
			showProfileLink: false,
			showTimestamp: false,
			beforeEachTweet: "F&oacute;rum Im&oacute;veis:"
		});
	}

	//parceiros
	if ($("#parceiros").length>0) {
		$("#parceiros div.scrollArea").scrollable({
			loop: true,
			speed: 1200,
			size: 5
		});
	}

	//onde estamos
	if ($("#onde").length>0) {
		mapa_imovel(-30.02356858411827,-51.20478630065918,'ponto',false);
	}

	//fotos do imovel
	if ($("#fotos").length>0) {
		$('#fotos .items a').lightBox();
		$("#fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}
	
	//fotos do imovel
	if ($(".fotos-empresa").length>0) {
		$('.fotos-empresa a').lightBox();
	}	
	
	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}

	//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;
		});
	}

});

