if(typeof google.maps == 'undefined') {
	google.load("maps", "2", {"language" : "it"});

	percorso = '/sysman/';

	//coordinate del centro e zoom iniziale
	var lat  = 41.665000;
	var lng  = 12.481259;
	var zoom = 6;
}

var aMap;
var markerEffemeridi = new Object();


function createMarkerEffemeridi(options) 
{
	var default_args = {
		/* michela 'omm'            : '-',
		'icao'           : '-',
		'name'           : '-',
		'argo'           : '-', 
		'region'         : '-',
		'lat'	         : '-',
		'lng'	         : '-',
		'hpoz'           : '-',*/
		'nome' :  '-',
		'lat'	         : '-',
		'lon'	         : '-',
		'altitudine'           : '-',
		
		
		'Sorgere_Sole'   : '-',
		'Tramonto_Sole'  : '-',
		'Sorgere_Luna'   : '-',
		'Tramonto_Luna'  : '-',
		'Sorgere_Sole1'  : '-',
		'Tramonto_Sole1' : '-',
		'Sorgere_Luna1'  : '-',
		'Tramonto_Luna1' : '-',
		'Fase_Lunare'    : '-',
		'Durata'         : '-',
		'd1'             : '-'
	}
	
	for(var index in default_args) {
		if(typeof options[index] == "undefined" || options[index] == '') {
			options[index] = default_args[index];
		}
	}
	
	var aIcon              = new google.maps.Icon();
	aIcon.image            = percorso + 'images/arrow1.gif';
	aIcon.iconSize         = new google.maps.Size(13, 22);
	aIcon.iconAnchor       = new google.maps.Point(7, 22);
	aIcon.infoWindowAnchor = new google.maps.Point(7, 8);
	
	// michela var aMarker = new google.maps.Marker(new google.maps.LatLng(options['lat'], options['lng']), {icon: aIcon});//, customIcons[type]);
	var aMarker = new google.maps.Marker(new google.maps.LatLng(options['lat'], options['lon']), {icon: aIcon});//, customIcons[type]);

	var d    = new Date();
	var mese = (d.getMonth() + 1);
	var anno = d.getFullYear();	
	
	var html = '<div class="center">Effemeridi di <i>' + options['d1'] + '</i></div>'
			+ '<table class="tableEffemeridi coloreTabella">'
			// michela + '<tr><td colspan="3" class="center" style="background:#4642a4; color:#eee;"><strong>' + options['name'] + '</strong>' + ' <br/>(Lat: ' + parseFloat(options['lat']).toFixed(3) + ' - Lng: ' + parseFloat(options['lng']).toFixed(3) + ' - H Pozz.: ' + parseInt(options['hpoz']) + ' m)</td></tr>' 
			+ '<tr><td colspan="3" class="center" style="background:#4642a4; color:#eee;"><strong>' + options['nome'] + '</strong>' + ' <br/>(Lat: ' + parseFloat(options['lat']).toFixed(3) + ' - Lng: ' + parseFloat(options['lon']).toFixed(3) + ' - Alt.: ' + parseInt(options['altitudine']) + ' m)</td></tr>' 
			+ '<tr><td class="coloreIntestazioni" style="width:34%"> </td><td class="coloreIntestazioni center" style="width:33%"><strong>Sorge</strong></td><td class="coloreIntestazioni center"><strong>Tramonta</strong></td></tr>' 
			+ '<tr><td class="coloreIntestazioni"><strong>Sole</strong></td><td class="center">' + options['Sorgere_Sole1'] + '</td><td class="center">' + options['Tramonto_Sole1'] + '</td></tr>' 
			+ '<tr><td class="coloreIntestazioni"><strong>Luna</strong></td><td class="center">' + options['Sorgere_Luna1'] + '</td><td class="center">' + options['Tramonto_Luna1'] + '</td></tr>'
			+ '</table>';
			
	if(options['Durata'] != '-' || options['Fase_Lunare'] != '-') {
		html += '<table class="tableEffemeridi coloreTabella">';
		
		if(options['Durata'] != '-') {
			html += '<tr><td style="width:67%"><strong>Durata Crepuscolo</strong></td><td class="center">' + options['Durata'] + '</td></tr>';
		}
		
		if(options['Fase_Lunare'] != '-') {
			html += '<tr><td><strong>Fase Lunare</strong></td><td class="center">' + options['Fase_Lunare'] + '</td></tr>'
		}
		
		html += '</table>';
	}
			
	// michela html += '<div class="center"><a href="#visualizza_effemeridi" onclick="caricaXMLEffemeridiMese(\'' + escape(options['name']) + '\', ' + '\'' + escape(options['argo']) + '\', ' + mese + ', ' + anno + ');">Visualizza Effemeridi mese corrente</a></div>';
	html += '<div class="center"><a href="#visualizza_effemeridi" onclick="caricaXMLEffemeridiMese(\'' + escape(options['nome']) + '\', ' + '\'' + escape(options['nome']) + '\', ' + mese + ', ' + anno + ');">Visualizza Effemeridi mese corrente</a></div>';
	
	google.maps.Event.addListener(aMarker, 'click', function() {
		aMarker.openInfoWindowHtml(html);
	});
	
	return aMarker;
}


function caricaXMLeffemeridi(oMap2) 
{
	var baseUrl  = percorso + 'visualizza_effemeridi_xml.php';
	
	google.maps.DownloadUrl(baseUrl, function(data) {
		var xml     = google.maps.Xml.parse(data);
		var markers = xml.documentElement.getElementsByTagName('item');	
		
		for (var i = 0; i < markers.length; i++) {
			var options = new Array();
			for(var j = 0; j < markers[i].childNodes.length; j++) {
				if(markers[i].childNodes[j].childNodes.length) {
					options[markers[i].childNodes[j].tagName] = markers[i].childNodes[j].childNodes[0].nodeValue;
				} else {
					options[markers[i].childNodes[j].tagName] = '';
				}
			}
			
			// michela if(options['argo'] != '') {
			if(options['nome'] != '') {
				// michela markerEffemeridi[options['argo']] = createMarkerEffemeridi(options);
				// michela oMap2.addOverlay(markerEffemeridi[options['argo']]);
				markerEffemeridi[options['nome']] = createMarkerEffemeridi(options);
				oMap2.addOverlay(markerEffemeridi[options['nome']]);
			}
		}
		
		document.getElementById('waitmessage').className = 'nascondi';
	});
}


Array.prototype.search = function(V) {
  var R = false;
  for(var i=0;i<this.length;i++) {
    if(V == this[i]) {
      R = true;
      break;
    }
  }
  return R;
}

function caricaXMLEffemeridiMese(name, argo, mese, anno)
{	
	name = unescape(name);
	argo = unescape(argo);
	
	var parametri = new Array('d1', 'Sorgere_Sole1', 'Tramonto_Sole1','Durata', 'Sorgere_Luna1', 'Tramonto_Luna1', 'Fase_Lunare');
	var baseUrl  = percorso + 'visualizza_effemeridi_xml.php?stazione=' + encodeURIComponent(argo) + '&mese=' +  encodeURIComponent(mese) + '&anno=' + encodeURIComponent(anno);
	document.getElementById('intestazione_effemeridi').innerHTML = 'Effemeridi per la stazione di <strong>' + name + '</strong> per il mese di '+document.getElementById('lista_mesi_id').options[document.getElementById('lista_mesi_id').selectedIndex].text+'/'+document.getElementById('lista_anni_id').value+'<br><i>Attendere prego...</i>';
	
	//apre il tooltip sulla mappa
	google.maps.Event.trigger(markerEffemeridi[argo], 'click');
	
	// michela - problemi con l'apice jQuery('#lista_stazioni_select').attr('disabled', 'disabled').children().removeAttr('selected').filter("[value='"+argo+").attr('selected', 'selected');
	jQuery('#lista_stazioni_select').attr('disabled', 'disabled').children().removeAttr('selected');
	var stazioni_input = document.getElementById('lista_stazioni_select');
	//seleziona la stazione
	for(var i = 0; i < stazioni_input.length; i++) {
		if(stazioni_input.options[i].text == argo) {
			stazioni_input.options[i].selected = 'selected';
			break;
		}
	}
	
	jQuery('#lista_mesi_id').attr('disabled', 'disabled').children().removeAttr('selected').filter("[value='" + mese + "']").attr('selected', 'selected');
	jQuery('#lista_anni_id').attr('disabled', 'disabled').children().removeAttr('selected').filter("[value='" + anno + "']").attr('selected', 'selected');
	jQuery('#visualizza_effemeridi_button_id').attr('disabled', 'disabled');
	jQuery('#visualizzaPDF_effemeridi_button_id').attr('disabled', 'disabled');
		
	document.getElementById('visualizza_effemeridi_div').style.backgroundColor = 'transparent';
	document.getElementById('visualizza_effemeridi_div').innerHTML = '';
	document.getElementById('visualizza_effemeridi_id').style.height = '390px';
	document.getElementById('visualizza_effemeridi_id').className = 'block';
	
	google.maps.DownloadUrl(baseUrl, function(data) {
		var xml     = google.maps.Xml.parse(data);
		var markers = xml.documentElement.getElementsByTagName('item');	
		
		var html = '<div class="center" style="padding:134px 0 154px 0">Non ci sono dati disponibili per la stazione di <br /><strong>' + name + '</strong></div>';
		for (var i = 0; i < markers.length; i++) {
			if(!i) {				
				document.getElementById('intestazione_effemeridi').innerHTML = 'Effemeridi per la stazione di <strong>' + name + '</strong> per il mese di '+document.getElementById('lista_mesi_id').options[document.getElementById('lista_mesi_id').selectedIndex].text+'/'+document.getElementById('lista_anni_id').value+'<br>';
				for(var j = 0; j < markers[i].childNodes.length; j++) {
					if(markers[i].childNodes[j].tagName == 'lat') {
						var latStazione = markers[i].childNodes[j].childNodes[0].nodeValue;
						document.getElementById('intestazione_effemeridi').innerHTML += '(Lat: ' + parseFloat(latStazione).toFixed(3);
					} else if(markers[i].childNodes[j].tagName == 'lon') {
						var lngStazione = markers[i].childNodes[j].childNodes[0].nodeValue;
						document.getElementById('intestazione_effemeridi').innerHTML += ' - Lng: ' + parseFloat(lngStazione).toFixed(3);
					} else if(markers[i].childNodes[j].tagName == 'altitudine') {
						document.getElementById('intestazione_effemeridi').innerHTML += ' - Altezza : ' + markers[i].childNodes[j].childNodes[0].nodeValue + ' m)';
					} else if(markers[i].childNodes[j].tagName == 'd1') {
						if(markers[i].childNodes[j].childNodes.length) {
							if(markers[i].childNodes[j].childNodes[0].nodeValue != '') {
								theStyleTd="style='text-align: right;width:13%;background:#4642a4; color:#eee;'";
								html = '<table id="tableDettaglioEffemeridi" style=width:"100%" class="center"><tr><td style="text-align: right;background:#4642a4; color:#eee;" class="center" rowspan="2" style="text-align: right;background:#4642a4; color:#eee;"><strong>Data</strong></td><td colspan="3" style="text-align: right;width:40%; background:#4642a4; color:#eee;"><strong>Sole</strong></td><td colspan="3" style="text-align: right;width:40%; background:#4642a4; color:#eee;"><strong>Luna</strong></td></tr>' 
								+ '<tr style="background:#4642a4; color:#eee;"><td '+theStyleTd+'><strong>Sorge</strong></td><td '+theStyleTd+'><strong>Tramonta</strong></td><td '+theStyleTd+'><strong>Durata Crepuscolo</strong></td><td '+theStyleTd+'><strong>Sorge</strong></td><td '+theStyleTd+'><strong>Tramonta</strong></td><td '+theStyleTd+'><strong>Fase Lunare</strong></td></tr>';
							}
						}
					}
				}
			} 
			
			var options = new Array();
			for(var j = 0; j < markers[i].childNodes.length; j++) {
				if (parametri.search(markers[i].childNodes[j].tagName)) {
					if(markers[i].childNodes[j].tagName != 'd1') {
						if(markers[i].childNodes[j].childNodes.length) {
							if(markers[i].childNodes[j].childNodes[0].nodeValue != '') {
								options[markers[i].childNodes[j].tagName] = markers[i].childNodes[j].childNodes[0].nodeValue;
							} else {
								options[markers[i].childNodes[j].tagName] = '-';
							}
						} else {
							options[markers[i].childNodes[j].tagName] = '-';
						}
					} else {
						if(markers[i].childNodes[j].childNodes.length) {
							options[markers[i].childNodes[j].tagName] = markers[i].childNodes[j].childNodes[0].nodeValue;
						} else {
							options[markers[i].childNodes[j].tagName] = '';
						}
					}
				}
			}
			
			if(options['d1'] != '') {
				var riga = (i % 2) ? 'rigaPariTabellaEffemeridi' : 'rigaDispariTabellaEffemeridi';
				html += '<tr><td class="' + riga + '">' + options['d1'] + '</td><td class="' + riga + '">' + options['Sorgere_Sole1'] + '</td><td class="' + riga + '">' + options['Tramonto_Sole1'] + '</td><td class="' + riga + '">' + options['Durata'] + '</td><td class="' + riga + '">' + options['Sorgere_Luna1'] + '</td><td class="' + riga + '">' + options['Tramonto_Luna1'] + '</td><td class="' + riga + '">' + options['Fase_Lunare'] + '</td></tr>';
			}
			
			if(i == markers.length - 1 && options['d1'] != '') {
				html += '</table>';
			}
		}
		
		document.getElementById('visualizza_effemeridi_id').style.height = '';
		document.getElementById('visualizza_effemeridi_div').style.backgroundColor = '#e3e6f0';
		document.getElementById('visualizza_effemeridi_div').innerHTML = html;
		
		jQuery('#lista_stazioni_select, #lista_mesi_id, #lista_anni_id, #visualizza_effemeridi_button_id, #visualizzaPDF_effemeridi_button_id').removeAttr('disabled');
	});
}


function caricaXMLEffemeridiMeseButton()
{
	var name = escape(jQuery('#lista_stazioni_select').children(':selected').html());
	var argo = escape(document.getElementById('lista_stazioni_select').value);
	var mese = document.getElementById('lista_mesi_id').value;
	var anno = document.getElementById('lista_anni_id').value;
	
	caricaXMLEffemeridiMese(name, argo, mese, anno);
}


function initialize_mapEffemeridi() 
{
	if(!document.getElementById('mapEffemeridi')) {
		return;
	}
	
	zoom = 6;
		
	var aMap2 = new google.maps.Map2(document.getElementById('mapEffemeridi'));
	aMap2.addControl(new google.maps.SmallMapControl());
    aMap2.addControl(new google.maps.MapTypeControl());
	//aMap2.addControl(new google.maps.OverviewMapControl());
	aMap2.addControl(new google.maps.ScaleControl());
	aMap2.setCenter(new google.maps.LatLng(lat, lng), zoom);
	aMap2.setMapType(G_HYBRID_MAP);//G_SATELLITE_MAP
	
	
	aMap = aMap2;

	testoHelp = "Gli istanti del sorgere e del tramonto del Sole e della Luna " + 
							"sono espressi in ore e minuti del Tempo Medio Europa Centrale (T.M.E.C.);" +
							"per l'Italia tale orario corrisponde all'ora solare. " +
							"Per il periodo che va dall'ultima domenica di marzo all'ultima di ottobre, " +
							"in cui viene usata l'ora legale, agli orari indicati nella tabella &egrave; sufficiente " +
							"aggiungere un'ora. La durata del crepuscolo &egrave; relativa ai giorni 1 e 15 del mese: " +
							"con approssimazione sufficiente per gli usi pratici essa pu&ograve; essere estensibile, " +
							"rispettivamente, a tutta la prima e la seconda quindicina del mese." +
							"A causa del suo moto di rivoluzione attorno alla terra, gli istanti " +
							"del sorgere e tramontare della luna si possono verificare in qualsiasi " +
							"parte del giorno. Come casi estremi, tali orari possono anche essere assenti, " +
							"nel corso di una giornata, per una certa localit&agrave;. Tale evenienza viene " +
							"indicata apponendo un trattino al posto dell'orario.";
							
	aMap2.addControl(new ExtMapTypeControl({showHelp: true, showHelpKey: true, showHelpText: testoHelp}));
								
	caricaXMLeffemeridi(aMap2);
	
	MIN_ZOOM_LEVEL = 5;
	MAX_ZOOM_LEVEL = 10;
	
	google.maps.Event.addListener(aMap2, 'zoomend', function(oldZoomLevel, newZoomLevel) {
		if(newZoomLevel < MIN_ZOOM_LEVEL) {
			aMap2.setZoom(Math.min(oldZoomLevel, MIN_ZOOM_LEVEL));
		} else if(newZoomLevel > MAX_ZOOM_LEVEL) {
			aMap2.setZoom(Math.max(oldZoomLevel, MAX_ZOOM_LEVEL));
		}
	}); 
}



google.setOnLoadCallback(initialize_mapEffemeridi);
window.onunload = function() { google.maps.Unload(); };

