//funcoes -------------

/*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());
				map.addMapType(G_SATELLITE_3D_MAP);
			}
			if (tipo=='circulo') {
				marcador = new CircleOverlay(map.getCenter(), 0.4, "#336699", 1, 1, '#336699', 0.25);
				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 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,"#0071BC",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(21, 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 conta_bairros(){
	c = 0;
	$("#busca .divbairros .bairros input").each(function(){
		if ($(this).is(":checked")) c++;
	});
	return c;
}


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();
	})			
}		

$(document).ready(function() {
	
	//aol
	if ($("a.open-aol").length>0) {
		 $("a.open-aol").click(function(){
			window.open('aol/chat/chat1.php','aol_atendimento_online','height=473,width=377');
			return false;
		});
	}	
	
	//aol pop
	if ($("#aol-pop").length>0) {
		var w = $(window).width();
		var c = (w/2);
		$("#aol-pop").css("left",c+"px");
		$("#aol-pop a").click(function(){
			$("#aol-pop").hide();
			//return false;
		});
	}	
	
	$("#aol-pop").animate({ top:350, opacity:1 },2500);
	
	$.getScript("inc/js/fav.js");
	
	carregaBairros();
	
	$("#busca_cidade").change(function(){ carregaBairros(); });		
	
	//onde estamos
	//if ($("#onde").length>0) {
	//	mapa_imovel(-30.03172419884693,-51.23120069503784,'ponto',false);
	//}
	
	//se for ie6, carrega menu por js e roda o pngfix
	if($.browser.msie && $.browser.version=="6.0") {
		$.getScript("inc/dropdown/jquery.dropdown.js");
		$.getScript("inc/js/jquery.pngfix.js",function(xhr){
			var aplicar_pngfix = "#logo img, #animacao, #busca, .homeimoveis .imovel, .homeimoveis .imovel .faixa img, .imoveis .imovel, .imoveis .imovel .faixa img, #detalhes .label";
			try {
				$(aplicar_pngfix).pngfix();
			} catch(err) {
				eval(xhr);
				$(aplicar_pngfix).pngfix();
			}
		});
		
	}

	//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>');
				}
			});
		});
	});
	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 da home
	if ($('#animacao').length>0) {
		$('#animacaoBase').Horinaja({
			capture: 'animacaoBase',
			delai: 0.3,
			duree: 4,
			pagination: true,
			nCell: 100
		});
	}
	
	//link total nos imoveis
	if ($("#leftContent .imoveis, #leftContent .homeimoveis").length>0) {
		$("#leftContent .imoveis .imovel, #leftContent .homeimoveis .imovel").mouseover(function(){
			$(this).addClass("ovr");
		});
		$("#leftContent .imoveis .imovel, #leftContent .homeimoveis .imovel").mouseout(function(){
			$(this).removeClass("ovr");
		});
		$("#leftContent .imoveis .imovel .foto, #leftContent .imoveis .imovel .info, #leftContent .imoveis .imovel .desc, #leftContent .homeimoveis .imovel").click(function(){
			lnk = $(this).parents(".imovel").find(".foto a").attr("href");
			if (lnk!='' && lnk!=undefined)
			window.location = lnk;
		});
	}
	
	//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);
		});
	}
	
	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}

	//fotos do imovel na listagem
	if ($(".imoveis .imovel .imenu .fotos").length>0) {
		$(".imoveis .imovel").each(function(){
			if ($(this).find(".imenu .fotos").length>0 && $(this).find(".imenu .maisfotos a").length>0) {
				foto1 = $(this).find(".imenu .maisfotos a:first");
				$(this).find(".imenu .fotos").attr("href",foto1.attr("href"));
				foto1.remove();
				$(this).find(".lightbox").lightBox({ overlayOpacity: 0.6 });
			}
		});
	}

	//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 .imenu a.enviar").click(function(){
			$("#enviar-por-email").dialog('open');
			return false;
		});
		$(".imoveis .imovel .imenu a.enviar").click(function(){
			$("#enviar-por-email input[name=codigo]").val($(this).find("input").val());
			$("#enviar-por-email").dialog('open');
			return false;
		});		
	}
	
	//ligamos pra você
	if ($("#ligamos").length>0) {
		$("#ligamos").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$("#hlinks .hl1, .hl4").click(function(){
			$("#ligamos").dialog('open');
			return false;
		});
	}	

	//fotos do imovel
	if ($("#fotos").length>0) {
		$('#fotos .items a').lightBox();
		$("#fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}

	//rolagem até as fotos
	if ($("#detalhes .imenu .fotos").length>0) {
		$("#detalhes .imenu .fotos").click(function(){
			$("html, body").animate({
				scrollTop: $("#fotos").offset().top
			}, 2000);
			return false;
		});
	}
	
});



