/* domain default coordinates */
var countryLat;
var countryLon;
var spotMap;
var markersLayer = new SAPO.Maps.Markers('markerLayer');; 

switch(serverDomain) {
	case 'cv': /* cabo verde */
		countryLat= 15.6;
		countryLon= -24.3;
		break;    
	case 'ao': /* angola*/
		countryLat= -9.0;
		countryLon= 16.5;
		break;
	case 'mz': /*moçambique */
		countryLat= -23.0;
		countryLon= 31.9;
		break;
	default: /*pt */
		countryLat= 39.5;
		countryLon= -8.3;
}

/* function to the "apagar ponto" link  */
function removePoints() {	
	markersLayer.removeMarkers();
	document.getElementById('localizacao').value = '';
}

/* callback of the map mouse click */
function activateLocator() {
	spotMap.events.register('click', this, mapClicked);
}

function mapClicked(evt) {
	var lonlat = spotMap.getLonLatFromContainerPixel(evt.xy);
	var lon = lonlat.lon; 
	var lat = lonlat.lat;
	document.getElementById('localizacao').value = lat + "," + lon;
	
	/*markersLayer = new SAPO.Maps.Markers('markerLayer');*/
	markersLayer.removeMarkers();
	
	/*spotMap.addMarkers(markersLayer);*/
	userMarker = new SAPO.Maps.Marker(new OpenLayers.LonLat(lon, lat), {draggable: false});
	markersLayer.addMarker(userMarker);
	spotMap.events.unregister('click', this, mapClicked);
	spotMap.addMarkers(markersLayer);
	
	if (typeof(autoSubmit) != 'undefined' ) {
		if (autoSubmit == true) {
			document.getElementById('formMapa').submit();
		}
	}	
}

function spotMapInit() {
	/* generates the map */    
	spotMap = new SAPO.Maps.Map('div_mapa');

	spotMap.addControl(new SAPO.Maps.Control.MapType());
	spotMap.addControl(new SAPO.Maps.Control.Navigation());
	
	/* centrar o mapa de acordo com o dominio */
	if (typeof(locationLat) != 'undefined') {
		spotMap.setMapCenter(new OpenLayers.LonLat(locationLon, locationLat), 5);
	}
	else {
		spotMap.setMapCenter(new OpenLayers.LonLat(countryLon, countryLat), 5);
	}
	
	/*add markers to markers layer*/
	if (typeof(mapExtraMarkers != 'undefined')) {
		for (i=0; i < mapExtraMarkers.length; i++) {
			markerOptions = {draggable: false,
					 nickname: mapExtraMarkers[i].nickname,
					 picture:  mapExtraMarkers[i].picture,
					 userID:   mapExtraMarkers[i].userID}
			marker = new SAPO.Maps.Marker(new OpenLayers.LonLat(mapExtraMarkers[i].lon, mapExtraMarkers[i].lat), markerOptions);
			marker.registerEvent('click', this, openMarkerPopup);
			markersLayer.addMarker(marker);
		}
	}
	
	/*add marker layer to map*/
	spotMap.addMarkers(markersLayer);
}

function openMarkerPopup(evt) {
	var htmlContent;
	htmlContent = '<divstyle="font-size:11px" align="center">';
	if (typeof(evt.picture) != 'undefined') {
		if (evt.picture != '') {
			htmlContent += '<a href="perfilamigo.html?uid=' + evt.userID + '"><img border="0" src="'+ evt.picture + '" /></a><br/>';
		}
	}
	htmlContent += '<a href="perfilamigo.html?uid=' + evt.userID + '"><strong>' + evt.nickname + '</strong></a></div>';
	evt.openPopup(htmlContent);
}

window.onload = spotMapInit();





