var marker = new Array();
var htm = new Array();
var select_cidade = "";
var select_uf = "";
$(document).ready(function() {
	
	//corrige menu
	$("#menu li.last a").css("padding-right",0).css("padding-left",0);
	var largura = $("#header").width();
	var lis = 0;
	$("#menu>li").each(function(){
		var w = $(this).width();
		lis = lis + w + 2;
	});
	lis = lis - 2;
	var dif = largura-lis;
	var pad = dif/2;
	$("#menu li.last a").css("padding-right",pad+"px").css("padding-left",pad+"px");
	
	//wrap no maincontent
	$("#mainContent").wrapAll('<div id="main-wrap" />');
	
	select_cidade = $("#cidade").html();
	select_uf = $("#uf").html();
	
	//busca pais
	$("#busca #pais").change(function(){
		muda_pais(); 
	});
	
	//busca uf
	$("#busca #uf").change(function(){
		muda_uf();
	});
	
	muda_pais();
	
	//estilização do filtro
	if ($("#filtro-lateral").length>0) {
		$("#filtro-lateral>li").each(function(){
			$(this).wrapInner('<div class="base" />');
			
		});
	}
	
	//clique nos imoveis
	$(".imoveis-home .imovel, #imoveis-lateral .imovel").click(function(){
		var lnk = $(this).find("img").parents("a").attr("href");
		window.location = lnk;
	});
	
	//mapa na busca
	if ($("#busca-mapa").length>0) {
		var v = new Array();
		var i = 0;
		$(this).find("input.info").each(function(){
			var info = $(this).val();
			i++;
			v[i] = info;
		});
		if (i==0) {
			$("#busca-mapa").hide();
			$("h1 .ocultar-mapa").hide();
		} else {
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			for (j=1;j<=i;j++) {
				var str = v[j];
				var c = str.split(",");
				if (j==1) map.setCenter(new GLatLng(c[0],c[1]), 11);
				map.addOverlay(createmarker(str));
			}
		}
	}
	
	$(".faq .p a").click(function(){
		var r = $(this).parents(".faq").find("div.r");
		if (r.is(":visible")) r.slideUp("slow"); else r.slideDown("slow");
		return false;
	});
	
	//alerta
	$(".de_locacao").hide();
	$(".ate_locacao").hide();
	$(".finalidade").change(function()	{ trocar_finalidade_alerta(); });	
	
});

function trocar_finalidade_alerta() {
	if ($(".de_venda").css('display')=='none') {
		$(".de_locacao").css('display','none');
		$(".ate_locacao").css('display','none');
		$(".de_venda").css('display','block');
		$(".ate_venda").css('display','block');
	}else{
		$(".de_venda").css('display','none');
		$(".ate_venda").css('display','none');
		$(".de_locacao").css('display','block');
		$(".ate_locacao").css('display','block');
	}	
}

function createmarker(str) {
	var icone = new GIcon(G_DEFAULT_ICON);
	icone.image = "img/design/gmaps_icon.png";
	icone.iconSize = new GSize(21, 31);
	markerOptions = { icon:icone };
	var c = str.split(",");
	var latlng = new GLatLng(c[0],c[1]);
	var marker = new GMarker(latlng, markerOptions);
	var htm = '<img src="'+c[2]+'" width="100" height="80" style="float:left;height:80px !important;" /><p style="float:left;margin:0 0 0 10px;width:200px;"><strong>'+c[3]+'<br />'+c[5]+', '+c[4]+'</strong><br />'+c[6]+'<br /><strong>'+c[7]+'</strong><br /><a href="'+c[8]+'">Ver mais detalhes</a></p>';
	GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(htm); });
	return marker;
}

function muda_pais() {
	var p = $("#pais").val();
	//$("#uf option").show();
	//$("#cidade option").show();
	$("#uf").html(select_uf);
	$("#cidade").html(select_cidade);
	if (p!='') {
		var i = 0;
		$("#uf option").each(function(){
			if (!$(this).hasClass(p)) $(this).remove();
			i++;
		});
		var i = 0;
		$("#cidade option").each(function(){
			if (i>0) if (!$(this).hasClass(p)) $(this).remove();
			i++;
		});
	}
	if ($("#uf option."+p).length>0) $("#uf option."+p+":first").attr("selected",true);
	//$("#uf option:first").attr("selected",true);
	$("#cidade option:first").attr("selected",true);
	carrega_bairros();
}

function muda_uf() {
	var p = $("#uf").val();
	//$("#cidade option").show();
	$("#cidade").html(select_cidade);
	if (p!='') {
		var i = 0;
		$("#cidade option").each(function(){
			if (i>0) if (!$(this).hasClass(p)) $(this).remove();
			i++;
		});
	}
	$("#cidade option:first").attr("selected",true);
	carrega_bairros(); 
}

function addmarker() {
	
}
