$(function(){

	if(!window.widgets_css)
	{
		$("head").append("<link rel='stylesheet' type='text/css' href='" + $.url(true) + "css/widgets.css' />");
		window.widgets_css = true;
	}
	
	if($("form.formBusca").length > 0)
	{
		$("#ImovelOperacao").change(function(){
			if($(this).val() == 'V')
			{
				$("#ImovelPrecoVenda").parents("div.input").show();
				$("#ImovelPrecoLocacao").parents("div.input").hide();
			}
			else
			{
				$("#ImovelPrecoVenda").parents("div.input").hide();
				$("#ImovelPrecoLocacao").parents("div.input").show();
			}
		});
		$("#ImovelOperacao").change();
		
		$("div.imovelCheckbox div.checkbox").live("click", function(e){
			
			if($target(e) == this)
			{
				$(this).find("input").click();
			}
			
			var $chk = $(this).find("input:checked");
			if($chk.length > 0)
			{
				$(this).addClass("checked");
			}
			else
			{
				$(this).remClass("checked");
			}
			
		});
		
		$("#ImovelCidade").change(function(){
			if($(this).val() == "")
			{
				$("form.formBusca div.bairro").hide();
				$("div.imovelCheckbox").html("");
			}	
			else
			{
				$("form.formBusca div.bairro").slideDown();
			}
			
			var $tpl_bairro = '<div class="checkbox">' +
			'<input type="checkbox" name="bairro[]" value="%bairro" id="ImovelBairro-%i">' +
			'<label for="ImovelBairro-%i">%bairro</label>' +
			'</div>';
			
			var bairros = $bairros[$(this).val()];
			if(bairros)
			{
				$("div.imovelCheckbox").html("");
				var i = 0;
				for(var bairro in bairros)
				{
					node_bairro = $tpl_bairro.split("%bairro").join(bairro).split("%i").join(i);
					$("div.imovelCheckbox").append(node_bairro);
					i++;
				}
			}
			else
			{
				$("div.imovelCheckbox").html("");
			}
		});
		$("#ImovelCidade").change();
	}
	
	if($("#resultados").length > 0)
	{
		$("form.formBusca").hide();
		
		$("p.order a, div.paging a").each(function(){
			$(this).attr("href", $(this).attr("href") + "?" + location.href.split("?")[1]);
		});
		
		$("#resultados a.bt-busca").click(function(){
			$("form.formBusca").slideToggle();
			return false;
		});
		
		$("#conteudo #resultados ul.lista-resultados li").click(function(e){
			var $t = $target(e);
			var $bt_lista = $(this).find("a.bt-lista").get(0);
			
			if($t != $bt_lista)
			{
				var $bt = $(this).find("a.bt-detalhes");
				window.location.href = $bt.attr("href");
			}
		});
	}
	
	$("#conteudo #imovel div.widgets").toggle();
	
	if($("#conteudo #imovel").length > 0)
	{
		$("#conteudo #imovel h3").each(function(){
			$(this).prepend('<span class="corner"></span>');
		});
	
		if($("#conteudo #imovel .caracteristicas p").length < 1)
		{
			$("#conteudo #imovel .caracteristicas").remove();
		}
		
		$("#conteudo #imovel .widgets a").each(function(){
			$(this).modal();
		});
		
		TIMER_SLIDE = setTimeout("startSlideShow()", SLIDE_SHOW_TIMER);
	}
	
});

var SLIDE_SHOW_TIMER = 6000, FOTO_INDEX = 0, TIMER_SLIDE;
function startSlideShow()
{
	var $foto = $("div.foto-principal img");
	var $fotos = $("div.galeria a");
	
	if($fotos.length > 2)
	{
		if((FOTO_INDEX+1) < $fotos.length)
		{
			FOTO_INDEX++;
		}
		else
		{
			FOTO_INDEX = 0;
		}
		
		var nextSrc = $($fotos[FOTO_INDEX]).attr("href");
		
		$foto.fadeOut("normal", function(){
			$foto.attr("src", nextSrc);
		}).unbind().load(function(){
			$(this).fadeIn();
		});
		
		TIMER_SLIDE = setTimeout("startSlideShow()", SLIDE_SHOW_TIMER);
	}
}

var $map = null;
var $geocoder = null;
var $marker = null;
	
function doMap()
{
	function setMapPosition(point)
	{
		if (!point)
		{
			$("div.localizacao").hide();
		}
		else
		{
			map.setCenter(point, 13);

			marker = new GMarker(point);
			map.addOverlay(marker);
			
			function onWindowInfo()
			{
				try { marker.openInfoWindowHtml("<b>" + address.split(".").join(".<br />") + "</b>"); } catch(ex) {};
			}		
			GEvent.addListener(marker, "click", onWindowInfo);
		}
	}
	
	map = new GMap2(document.getElementById("map-localizacao"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	
	if(typeof address == "string")
	{
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(address, setMapPosition);
	}
	else
	{
		setMapPosition(new GLatLng(address.lat, address.lng));
	}
}