/// GOOGLEMAP
///
/// Encapsulação do API do Google Maps.
///
/// NOTA: Usa 'document.write' para referenciar o código Google e não parece haver
///       alternativa fiável.
///
/// NOTA: No seguimento da nota acima, este ficheiro tem de ser referenciado no <HEAD>
///       do documento.
///
///
// Inicialização.
function GoogleMap (gkey, optLang) {
	this._lang = optLang || DROOT.getAttribute ("xml:lang");
	if (top.DEBUGGING) {
		// Emular a presença do código verdadeiro.
        W.GUnload = W.GBrowserIsCompatible = function () {};
	} else {
		// Referenciar o código.
		D.write ('<script type="text/javascript" src="http://maps.google.com/maps?file=api&v=2&sensor=true&key=' + gkey + '"></script>')
	}
}
GoogleMap.prototype = {
///
///
/// PROPRIEDADES
///
msgLoading : "A carregar o mapa...",
///
///
/// MÉTODOS
///
// Dá início ao carregamento do mapa, embora ele só termine após todos os outros elementos.
create : function (mapEl, lat, lng, optZoomLevel, optHintText) {
	var me = this, translateFunc = function(){}, ndx = 0, btnNames, i;

	me._mapEl = mapEl;
	me._lat = lat;
	me._lng = lng;
	me._zl = optZoomLevel || 10;
	me._hintText = optHintText;
	// Traduzir os nomes dos botões.
	switch (me._lang) {
	case "en":
		btnNames = ['Map', 'Satellite', 'Both']
		break;
	default:
		btnNames = ['Mapa', 'Satélite', 'Ambos']
	}
	if (btnNames) {
		translateFunc = function () {
			GMapType.prototype.getName = function (optI) {
				i = optI != null ? optI : ndx++;
				if (i < 0 || i >= btnNames.length) i = 0;
				return btnNames [i]
			}
		}
	}
	// Instalar eventos.
	go (function () {	me._load (translateFunc)	})
},

// Centra o mapa no ponto LAT:LNG.
setCenter : function (optLat, optLng, optZL) {
	var me = this, lat = optLat ? optLat : me._lat, lng = optLng ? optLng : me._lng, zl = optZL ? optZL : me._zl, map = me._map;
	if (!map) return;

	map.setCenter (new GLatLng (lat, lng), zl);
},

// Disparado no momento em que o mapa acaba de ser carregado em EL.
onLoad : function (el) {
	el.lastChild.style.display = "none";
	el.firstChild.style.visibility = "visible";
},
///
///
/// MÉTODOS INTERNOS
///
// Carregar.
_load : function (prefunc) {
	var me = this, el = me._mapEl, w, map, p, m, s;
	if (!W.GUnload) return setTimeout (function () {	me._load (prefunc)	}, 128);
	// Inicializar.
	on (W, "unload", GUnload);
	if (!GBrowserIsCompatible ()) return;
	prefunc ();
	// Preparar 'el'.
	w = el.offsetWidth, h = el.offsetHeight;
	el.innerHTML = "";
	/// Criar um DIV posicionado, para permitir alguma flexibilidade.
	div1 = D.createElement ("div");
	st = div1.style;
	st.width = w + "px";
	st.height = h + "px";
	st.position = "relative";
	/// Finalmente, criar um DIV para o mapa.
	div2 = D.createElement ("div");
	st = div2.style;
	st.width = w + "px";
	st.height = h + "px";
	div2.className = "gmap-cx";

	div1.appendChild (div2);
	el.appendChild (div1);
	me._loading (el = div1);
	// Criar o mapa.
	map = el._map = new GMap2 (el.firstChild);
	GEvent.addListener (map, "load", function () {	me.onLoad (el)	});
	map.setCenter (p = new GLatLng (me._lat, me._lng), me._zl);
	// Colocar marcador.
	m = new GMarker (p);
	if (s = me._hintText) GEvent.addListener (m, "click", function () {	m.openInfoWindowHtml (s)	});
	map.addOverlay (m);
	// Configurar.
	map.addControl (new GMapTypeControl);
	map.addControl (new GLargeMapControl);
},

// Mostra uma mensagem enquanto o mapa carrega em EL.
_loading : function (el) {
	var p = D.createElement ("P");
	el.firstChild.style.visibility = "hidden";
	p.innerHTML = this.msgLoading;
	p.className = "gmap-msg";
	el.appendChild (p);

	this._center (el, p);
},

// Centra E em EL tanto vertical como horizontalmente.
_center : function (el, e) {
	var elw = el.offsetWidth, elh = el.offsetHeight, ew = e.offsetWidth, eh = e.offsetHeight, st;
	st = e.style;
	st.W = Math.ceil ((elh - eh) / 2) + "px";
	st.left = Math.ceil ((elw - ew) / 2) + "px";
	st.marginTop = st.paddingTop = 0
},
///
///
/// VARIÁVEIS
///
_lang		: "",
_mapEl		: 0,
_map		: 0,
_lat		: 0,
_lng		: 0,
_zl			: 0,
_hintText	: ""

}
