function abrirMapa(mypage, myname) {
	var w = screen.width;
	var h = screen.height;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=0,';
	settings += 'left=0,';
	settings += 'toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes';
	win = window.open(mypage, myname, settings);
}

function MostrarOcultarResultados(b_mostrar) {
	document.getElementById('containnerpesquisa').style.visibility = (b_mostrar ? 'visible'
			: 'hidden');
}

function Mapear() {
	var parametros = {
		setDados : 'IMOVEL',
		urlRedirecionamento : document.form_pesq_imoveis.pesq_link_redirect.value,
		serch_tpNegocio : document.form_pesq_imoveis.pesq_tpNegocio.value,
		serch_tpImovel : document.form_pesq_imoveis.pesq_tpImovel.value,
		serch_cidade : document.form_pesq_imoveis.pesq_cidades.value,
		serch_bairro : document.form_pesq_imoveis.pesq_bairros.value,
		serch_valor : document.form_pesq_imoveis.pesq_preco.value
	};

	$
			.ajax( {
				type : "POST",
				url : 'ajax.mapas.php',
				data : parametros,
				dataType : 'json',
				success : function(imoveis) {
					mapa.LimpaMapa();
					if (imoveis) {
						$
								.each(
										imoveis,
										function(i, imovel) {
											setTimeout(
													function() {
														mapa.addImovel(imovel);
														document
																.getElementById('qtdRegistros').innerHTML = i + 1;
													}, i * 200);
										});
						var lista = '';
						$.each(imoveis, function(i, imovel) {
							lista = lista + imovel.lista;
						});
						document.getElementById('listaImoveis').innerHTML = lista;
					} else {
						document.getElementById('listaImoveis').innerHTML = '<p style="padding:10px;">Nenhum im&oacute;vel encontrado para a pesquisa selecionada!</p>'
					}
					MostrarOcultarResultados(true);
				},
				error : function(msg) {
					alert("Erro: " + msg.responseText);
				},
				beforeSend : function() {

				}
			});
}

var mapaImovel = function(lat, lng) {
	var ImoveisArray = [];

	var latlng = new google.maps.LatLng(lat, lng);
	var myOptions = {
		zoom : 14,
		center : latlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("mapa"), myOptions);

	this.addImovel = function(imovel) {

		var cssAtual = '';

		var infowindow = new google.maps.InfoWindow( {
			content : imovel.textoBalao
		});

		var novaPosicao = new google.maps.LatLng(imovel.lat, imovel.lng);
		var marcador = new google.maps.Marker( {
			position : novaPosicao,
			draggable : false,
			animation : google.maps.Animation.DROP,
			title : imovel.tituloMarcador,
			map : map,
			icon : 'images/marcador_' + imovel.nrImovel + '.png'
		});

		google.maps.event.addListener(marcador, 'click', function() {
			infowindow.open(map, marcador);
		});

		google.maps.event
				.addListener(
						marcador,
						'mouseover',
						function() {
							cssAtual = document
									.getElementById('lista_' + imovel.nrImovel).style.backgroundColor;
							document.getElementById('lista_' + imovel.nrImovel).style.backgroundColor = '#FEF6D1';
							marcador
									.setIcon('images/marcadorOver_' + imovel.nrImovel + '.png');
						});

		google.maps.event
				.addListener(
						marcador,
						'mouseout',
						function() {
							document.getElementById('lista_' + imovel.nrImovel).style.backgroundColor = cssAtual;
							marcador
									.setIcon('images/marcador_' + imovel.nrImovel + '.png');
						});

		ImoveisArray.push(marcador);
	};

	this.LimpaMapa = function() {
		if (ImoveisArray) {
			for (i in ImoveisArray) {
				ImoveisArray[i].setMap(null);
			}
			ImoveisArray.length = 0;
		}
	};

	this.setIconMarcador = function(icone, marcador) {
		ImoveisArray[marcador - 1].setIcon(icone);
	};
};
