$(document).ready(function() {

	//aol flutuante
	$("#bn-aol").makeFloat({x:"current",y:"current"});
	$("#bn-ligamos").makeFloat({x:"current",y:"current"});

	/*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;
	});*/
	
	//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;
	});	

	//labelify
	$.getScript("inc/js/jquery.labelify.js",function(xhr){
		var aplicar_labelify = "##codigo input, #newsletter .text";
		try {
			$(aplicar_labelify).labelify();
		} catch(err) {
			eval(xhr);
			$(aplicar_labelify).labelify();
		}
	});
	
	//animacao
	if ($("#animacao").length>0) {
		var i = -1;
		$("#animacao .slides .slide").each(function(){
			i++;
			$(this).attr('id','slide_'+i);
			var h = $(this).find("a").html();
			//oculta o nome do banner
			//$(this).find("a").html('<big>'+$(this).attr("title")+'</big><br />'+h);
		});
			$("#animacao .slides").after('<div id="anav">');
			$("#animacao .slides").cycle({
				fx:		'scrollHorz',
				speed:	2000,
				timeout: 10000,
				before: function(currSlideElement, nextSlideElement, options, forwardFlag)  {
					if (currSlideElement.id!=nextSlideElement.id) {
						var s1 = $("#"+currSlideElement.id);
						s1.find("a").animate({marginTop: '-80px'},1000);
					}
					var s2 = $("#"+nextSlideElement.id);
					s2.find("a").stop().css("margin-top", '-80px');
				},
				after: function(currSlideElement, nextSlideElement, options, forwardFlag)  {
					var s2 = $("#"+nextSlideElement.id);
					s2.find("a").animate({marginTop: '0px'},300);
				},
				pause:	true,
				pauseOnPagerHover: true,
				pager:	'#anav'
			});
			$("#animacao .box").css("cursor","pointer");
			$("#animacao .box").click(function(){
				lnk = $(this).find(".mais").attr("href");
				window.location = lnk;
			});
			$("#animacao .slide").click(function(){
				var l = $(this).find("a:first").attr("href");
				window.location = l;
			});
			setTimeout("animacao_nomes()",1000);
	}
	
	//busca
	if ($("#busca").length>0) {
		$("#busca .campos fieldset select").each(function(){
			$(this).css("opacity","0").addClass("masked").before('<div class="mask" />');
			var v = $(this).find("option:selected").html();
			$(this).parents("fieldset").find(".mask").html(v);
		});
		$("#busca .campos .masked").change(function(){
			var v = $(this).find("option:selected").html();
			$(this).parents("fieldset").find(".mask").html(v);
		});
	}
	

	//ligamos para voce
	if ($("#ligamos").length>0) {
		$("#ligamos").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$("a.ligamos").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 a.email").click(function(){
			$("#enviar-por-email").dialog('open');
			return false;
		});
	}
	
	//imprimir
	$("#detalhes .print").click(function(){
		lnk = $(this).attr("href");
		window.open(lnk,'print','width=600,height=500,scrollbars=yes');
		return false;
	});
	
	//imoveis
	$(".imoveish .imovel, .imoveis .imovel, #imoveisRight .imovel").live('click',function(){
		href = $(this).find("a.mais").attr("href");
		window.location = href;
		return false;
	});
	
	//"voltar" nos Hs
	$("h1.voltar, h2.voltar").each(function(){
		var h = $(this).html();
		$(this).html('<span>'+h+'</span><a href="#" onclick="history.go(-1);return false;" class="voltar">&laquo; Voltar</a>');
	});
	
	//"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;
	});
	
	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}

	//fotos do imovel
	if ($(".fotos").length>0) {
		$('.fotos').each(function(){
			$(this).find('.items a').lightBox();
		});
		$('#detalhes .ifoto a.foto').click(function(){ $(".fotos:first div.scrollArea a:first").click(); return false; });
		$(".fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}
	

	//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, h1, h2, h3, #rightContent, #leftContent, .imoveis, #detalhes, .paginacao").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).parents(".divbairros").find(".mask").html(c+' selecionado'+s);
				} else {
					$(this).parents(".divbairros").find(".mask").html('Todos');
				}
			});
		});
	});
	function conta_bairros(){
		c = 0;
		$("#busca .divbairros .bairros input").each(function(){
			if ($(this).is(":checked")) c++;
		});
		return c;
	}

	//campo bairros na busca
	carregaBairros();
	$("#busca_cidade").change(function(){ carregaBairros(); });		
	
	
	
	
	
});

function animacao_nomes() {
	var i = 0;
	$("#animacao .slides .slide").each(function(){
		var t = $(this).attr("title");
		$("#anav a:eq("+i+")").html(t);
		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,"#B90F2C",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(30, 31);
				markerOptions = { icon:icone };
				map.addOverlay(new GMarker(latlng, markerOptions));
				//map.addOverlay(new GMarker(latlng));
			}
		}
	}
}

function add_mailing(email)
{
	 $.get("inc/lib/fnc_news.php?act=cadnews", {email:email, nome:email},
		function(data){
			alert(data);
		})
}					

function validatemailing(formname)
{
	Exp = /(^[A-Za-z0-9_.-]+@([A-Za-z0-9_]+\.)+[A-Za-z]{2,4}$)/; // express&agrave;o pra validar email
	msg  = ""; // mensagem de erro
	erro = false;
	
	if(formname.email.value==""){ erro = true; alert('Digite seu E-mail.\n'); return false;}
	else if(!Exp.test(formname.email.value)){ erro = true; alert('O Email informado não é válido.\n'); }
	
	if(!erro){ 
		add_mailing(formname.email.value);
	}
	return false; 
}


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 ChatOpen() {
	window.open("http://web.atendimentoaovivo.com.br/chat.asp?idc=5569&pre_empresa=1358&pre_depto=1612&vivocustom=&vivocustom2=&vivocustom3=&vivocustom4=&idtemplate=0","atendimentoaovivo","width=510,height=580,scrollbars=no,status=no,location=no,menubar=no,toolbar=no");
}

