$(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 = "#aol a img";
			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, #footer #footer1 input:text, #footer #footer1 input:password";
		try {
			$(aplicar_labelify).labelify();
		} catch(err) {
			eval(xhr);
			$(aplicar_labelify).labelify();
		}
	});
	
	$.getScript("inc/js/fav.js");
	
	//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>');
				}
			});
		});
	});

	//animação home
	if ($('#animacao').length>0) {
		$('#anibase').Horinaja({
			capture: 'anibase',
			delai: 0.3,
			duree: 4,
			pagination: true,
			nCell: 100
		});
	}
	
	
	//twitter
	if ($("#twitter").length>0) {
		$("#twitter .tweets").getTwitter({
			userName: "sigavista",
			numTweets: 2,
			loaderText: "Aguarde, carregando...",
			slideIn: false,
			showHeading: false,
			showProfileLink: false,
			showTimestamp: false,
			beforeEachTweet: "<strong>Astor Imóveis: </strong>"
		});
	}
	
	//hover dos imóveis
	if ($('#leftContent').length==0) {
		$('.imoveis .imovel .base').mouseover(function(){
			$(this).addClass("active");
			$(this).css("cursor","pointer");
			$(this).click(function(){
				if ($(this).find('.foto a').length>0) {
					lnk = $(this).find(".foto a").attr("href");
					window.location = lnk;
				}
				return false;
			});
		});
		$('.imoveis .imovel .base').mouseout(function(){
			$(this).removeClass("active");
		});
	}

	//logos da home
	if ($("#homelogos").length>0) {
		$("#homelogos .scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 5
		});
	}

	//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
		});
		$(".label .email").click(function(){
			$("#enviar-por-email").dialog('open');
			return false;
		});
		$(".icones .email").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").click(function(){
			$("#ligamos").dialog('open');
			return false;
		});
	}	


	//video modal
	if ($("#video").length>0) {
		$("#video").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 695,
			resizable: false
		});
		$(".label .video").click(function(){
			$("#video").dialog('open');
			return false;
		});
	}

	//fotos do imovel na listagem
	if ($(".imoveis .imovel .maisfotos").length>0) {
		$(".imoveis .imovel").each(function(){
			if ($(this).find(".maisfotos a").length>0) {
				foto1 = $(this).find(".maisfotos a:first");
				$(this).find("li .fotos").attr("href",foto1.attr("href"));
				$(this).find("li .fotos").addClass("lightbox");
				foto1.remove();
				$(this).find(".lightbox").lightBox({ overlayOpacity: 0.6 });
			}
		});
	}

	//fotos do imovel
	if ($("#fotos").length>0) {
		$('#fotos .items a').lightBox();
		$("#fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}

	carregaBairros();
	$("#busca_cidade").change(function(){ carregaBairros(); });		
	
	//busca
	$("#preco_locacao").hide();
	$("#finalidade").change(function()	{ trocar_finalidade(); });
	trocar_finalidade(); 
	
	//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'); }	 
	});	
	
});

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,"#B1231C",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));
				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();
	})			
}	


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');
	}
}



