// Monta os itens q serao mostrados nos destaques da home
function montaArrayItem(texto, imagem, url) {
	var arrItem = new Array(3);
	arrItem[0] = texto;
	arrItem[1] = imagem;
	arrItem[2] = url;
	return arrItem;
}

function mudaMaisHome(avanca, secao) {
	var arr = _arrMais[secao];
	if (arr.length <= 0) {
		return false;
	}
	if (avanca > 0) {
		_indiceMais[secao] += 2;
	} else {
		_indiceMais[secao] -= 2;
	}
	if (_indiceMais[secao] >= arr.length) {
		_indiceMais[secao] = 0;
	} else if (_indiceMais[secao] < 0) {
		if ((arr.length % 2) == 0) {
			_indiceMais[secao] = (arr.length - 2);
		} else {
			_indiceMais[secao] = (arr.length - 1);
		}
	}
	for (var i = 0; i <= 1; i++) {
		var texto = $id("mais" + secao + "_" + i);
		if (arr[_indiceMais[secao] + i]) {
			texto.innerHTML = arr[_indiceMais[secao] + i][0];
		} else {
			texto.innerHTML = '';
		}
	}
}

function mudaDestaqueHome(secao, indice) {
	var arr = _arrDestaque[secao];
	if (arr.length <= 0) {
		return false;
	}
	if (indice >= arr.length) {
		indice = 0;
	} else if (indice < 0) {
		indice = (arr.length - 1);
	}
	var texto = $id("dest" + secao + "_texto");
	texto.innerHTML = arr[indice][0];
	var link_imovel = $id("dest" + secao + "_link");
	link_imovel.onclick = function(){ document.location.href=arr[indice][2]; }
	var imagem = $id("dest" + secao + "_imagem");
	imagem.style.backgroundImage = "url(" + arr[indice][1] + ")";
	var ico;
	for (var i = 0; i < arr.length; i++) {
		ico = $id("dest" + secao + "_pag" + i);
		if (i == indice) {
			ico.className = "ativo";
		} else {
			ico.className = "";
		}
	}
}

function mudaMaisRodape(avanca, secao) {
	var arr = _arrRodape[secao];
	if (arr.length <= 0) {
		return false;
	}
	if (avanca > 0) {
		_indiceRodape[secao] += 3;
	} else {
		_indiceRodape[secao] -= 3;
	}
	if (_indiceRodape[secao] >= arr.length) {
		_indiceRodape[secao] = 0;
	} else if (_indiceRodape[secao] < 0) {
		if ((arr.length % 3) == 0) {
			_indiceRodape[secao] = (arr.length - 3);
		} else if ((arr.length % 2) == 0) {
			_indiceRodape[secao] = (arr.length - 2);
		} else {
			_indiceRodape[secao] = (arr.length - 1);
		}
	}
	
	for (var i = 0; i <= 2; i++) {
		var texto = $id("rodape" + secao + "_" + i);
		if (arr[_indiceRodape[secao] + i]) {
			texto.innerHTML = arr[_indiceRodape[secao] + i][0];
		} else {
			texto.innerHTML = '';
		}
	}
}


// Muda as abas de destaques
function mudarAbaDestaque(pcAba) {
	var conteudo = $id("divDestaque");
	var botao = $id("bt" + pcAba.toUpperCase());
	
	$id("btLANCAMENTO").src = "img/index_home/qd_bt_lancamento.jpg";
	$id("btREVENDA").src = "img/index_home/qd_bt_revenda.jpg";
	$id("btALUGUEL").src = "img/index_home/qd_bt_aluguel.jpg";
	$id("btRURAL").src = "img/index_home/qd_bt_rural.jpg";
	$id("btCIDADE").src = "img/index_home/qd_bt_outrac.jpg";
	$id("btTEMPORADA").src = "img/index_home/qd_bt_temporada.jpg";
	
	botao.src = botao.src.replace(".jpg", "1.jpg"); //"img/index_home/" + pcImg;
	CarregaDestaque("ajax/ajax_destaques.php?aba=" + pcAba, "divDestaque");
}

// Carrega um conteudo dentro de uma camada via Ajax
function CarregaDestaque(pcPagina, pcCamada) {
	var req = null;
	// Procura por um objeto nativo (Mozilla/Safari)
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", pcPagina, true);
		req.send(null);
	// Procura por uma versao ActiveX (IE)
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", pcPagina, true);
			req.send();
		}
	}
	
	$id(pcCamada).innerHTML = '<div align="center" style="width:50px; height:50px; padding-top:10px; background-image:url(img/bg_loadingA.gif); background-repeat:no-repeat;"><img src="img/loadingA.gif" border="0"></div>';
	
	function processReqChange() {
		if (req.readyState == 4) {
			if ((req.status == 200) && ((req.responseText) != "UNKNOWN")) {
				if ($id(pcCamada)) {
					$id(pcCamada).innerHTML = req.responseText;
					fillup();
				}
			}
		}
	}
}

// Carrega os valores quando muda o tipo
function mudarTipoImovel(idTipo, poForm) {
	var f = poForm;
	f.val.length = 0;
	cria_option(f.val, "Carregando...", 0, false);
	setInfoAjaxSemBloco(_url_site + "/ajax/ajax_valor.php?txtTipo=" + idTipo, "setValores", poForm);
}
// Seta os valores no campo Select
function setValores(obj, poForm) {
	if ((obj != null) && (typeof(obj) != "undefined")) {
		var f = poForm;
		f.val.length = 0;
		cria_option(f.val, "Valor", 0, false);
		for (var i = 0; i < obj.length; i++) {
			cria_option(f.val, obj[i].nome, obj[i].valor, false);
		}
	}
}

// Carrega as cidade quando muda o estado
function mudarEstado(uf, poForm) {
	var f = poForm;
	f.cd.length = 0;
	cria_option(f.cd, "Carregando...", 0, false);
	setInfoAjaxSemBloco(_url_site + "/ajax/ajax_cidade.php?txtEstado=" + uf, "setCidades", poForm);
}
// Seta as cidades no campo Select
function setCidades(obj, poForm) {
	if ((obj != null) && (typeof(obj) != "undefined")) {
		var f = poForm;
		f.cd.length = 0;
		cria_option(f.cd, "Cidade", 0, false);
		for (var i = 0; i < obj.length; i++) {
			cria_option(f.cd, obj[i].nome, obj[i].idCidade, false);
		}
		f.ba.length = 0;
		cria_option(f.ba, "Setor", 0, false);
	}
}

// Carrega os bairros quando muda a cidade
function mudarCidade(idCidade, poForm) {
	var f = poForm;
	f.ba.length = 0;
	cria_option(f.ba, "Carregando...", 0, false);
	setInfoAjaxSemBloco(_url_site + "/ajax/ajax_bairro.php?txtCidade=" + idCidade, "setBairros", poForm);
}
// Seta os bairros no campo Select
function setBairros(obj, poForm) {
	if ((obj != null) && (typeof(obj) != "undefined")) {
		var f = poForm;
		f.ba.length = 0;
		cria_option(f.ba, "Setor", 0, false);
		for (var i = 0; i < obj.length; i++) {
			cria_option(f.ba, obj[i].nome, obj[i].idBairro, false);
		}
	}
}




var _objMarcadores = new Object;
var _mapa = null;
var _geocoder = null;
var _mostrarCentro = true;
var _mostrarInfo = true;
var _registroUnico = false;

function loadMap() {
	_mapa = new GMap2(document.getElementById("divMapa"));
	_mapa.setCenter(new GLatLng(0, 0), 5);
	_mapa.addControl(new GLargeMapControl());
	_mapa.addControl(new GMapTypeControl());
	_mapa.setMapType(G_HYBRID_MAP);
	
	_geocoder = new GClientGeocoder();
	
	gerar();
}

function gerar() {
	var x_min = 0, x_max = 0, x = 0; // Longitude
	var y_min = 0, y_max = 0, y = 0; // Latitude
	var cont = 0;
	var marcador = null;
	
	for (i in _objMarcadores) {
		var obj = _objMarcadores[i];
		if ((obj != null) && (obj != "undefined")) {
			cont++;
			if ((obj.latitude != "") && (obj.longitude != "")) {
				marcador = criarMarcadorPorLocalizacao(obj, obj.latitude, obj.longitude);
				obj.marcador = marcador;
				// Verifica os pontos mais extremos do mapa
				if ((obj.longitude > x_max) || (x_max == 0)) { x_max = parseFloat(obj.longitude); }
				if ((obj.longitude < x_min) || (x_min == 0)) { x_min = parseFloat(obj.longitude); }
				if ((obj.latitude > y_max) || (y_max == 0)) { y_max = parseFloat(obj.latitude); }
				if ((obj.latitude < y_min) || (y_min == 0)) { y_min = parseFloat(obj.latitude); }
				
				// Gera um ponto medio caso haja mais de um
				if (x_max > x_min) {
					x = (x_max + x_min)/2;
				} else {
					x = x_max;
				}
				if (y_max > y_min) {
					y = (y_max + y_min)/2;
				} else {
					y = y_max;
				}
				
			} else {
				marcador = criarMarcadorPorEndereco(obj);
				obj.marcador = marcador;
			}
		}
	}
	
	if ( (_mostrarCentro) && (marcador != null) && (marcador != "undefined") && (cont <= 1) ) {
		_mapa.setCenter(marcador.getLatLng(), 17);
	} else {
		if ((y == "0") || (x == "0")) {
			y = "-16.657901";
			x = "-49.246387";
		}
		var ponto = new GLatLng(y, x);
		_mapa.setCenter(ponto, 12);
	}
}

function criarMarcadorPorEndereco(obj) {
	if (_geocoder) {
    	_geocoder.getLatLng(
      	obj.endereco,
      	function (point) {
			if (!point) {
				//
			} else {
				obj.latitude = point.y;
				obj.longitude = point.x;
				
				var ponto = new GLatLng(point.y, point.x);
				if (_registroUnico) {
					_mapa.setCenter(ponto, 17);
				}
				return criarMarcadorPorLocalizacao(obj, point.y, point.x);
			}
    	}
    	);
  	}
}

function criarMarcadorPorLocalizacao(obj, lat, lng) {
	var ponto = new GLatLng(lat, lng);
	
	if (_registroUnico) { _mapa.clearOverlays(); }
	
	/*
	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://www.imoveisemgoiania.com.br/portal/thumbMap.php?image=img/bluemark.png&t=" + obj.id;
	blueIcon.iconSize = new GSize(32, 32);
	// Set up our GMarkerOptions object
	markerOptions = { icon:blueIcon };
	
	var marker = new GMarker(ponto, markerOptions);
	*/
	
	var marker = new GMarker(ponto);
	_mapa.addOverlay(marker);
	
	GEvent.addListener(marker, 'click', function(){marker.openInfoWindowHtml(obj.html)});
	if (_mostrarInfo) {
		marker.openInfoWindowHtml(obj.html);
	}
	
	//if (_mostrarCentro) { _mapa.setCenter(marker.getLatLng(), 17); }
	if (marker != "undefined") {
		obj.marcador = marker;
		_objMarcadores[obj.id] = obj;
	}
	return marker;
}

function verMarcador(obj) {
	if ((obj.marcador != null) && (obj.marcador != null)) {
		_mapa.setCenter(obj.marcador.getLatLng(), 17);
		if (obj.html != "") {
			obj.marcador.openInfoWindowHtml(obj.html);
		}
	}
}