function MappaMessaggi() {
    this.loaded = false;
    
    this.latLngCenter = new google.maps.LatLng(41.895466,12.482324);
    this.startZoom = 5;
    
    this.markers = Array();
    this.infoWindows = Array();
    this.markerIndex = 0;
    this.numMarkers = 0;
    this.curMarker = -1;
    
    this.init = function() {
    	
		var myOptions = {
			zoom: this.startZoom,
			center: this.latLngCenter,
			mapTypeId: google.maps.MapTypeId.TERRAIN,
			backgroundColor: "#aabbcc",
			mapTypeControl: true,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
			}
			
		};
		this.map = new google.maps.Map(document.getElementById("mappaHP"), myOptions);
		
		google.maps.event.addListener(this.map, 'click', this.stopAnim );
		google.maps.event.addListener(this.map, 'dblclick', this.stopAnim );
		google.maps.event.addListener(this.map, 'dragstart', this.stopAnim );
		google.maps.event.addListener(this.map, 'rightclick', this.stopAnim );
		google.maps.event.addListener(this.map, 'zoom_changed', this.stopAnim );
		
		
		this.loaded = true;
		
		this.loadMessaggi();
	
    };
    
    this.loadMessaggi = function() {
        var ts = new Date();
        $.getJSON("messaggi.php?ts="+ts.getTime(), window.mappa.handlerMessaggio);
    };
    
    this.handlerMessaggio = function(data) {
        window.mappa.drawMarkers(data);
    };
    
    this.stopAnim = function() {
    	window.clearTimeout(window.mappa.timer);
    }
    
    this.drawMarkers = function(data) {
        
        for(var i = 0; i < data.length; i++){
			
			var obj = data[i];
			var point = new google.maps.LatLng(obj.lat, obj.lng);
			
			var marker = new google.maps.Marker({
				position: point, 
				map: this.map
				
			});
			
			window.mappa.markers[window.mappa.numMarkers] = marker;

			var contentString = "";
			
			if(obj.immagine == "null" || obj.immagine == null)
				contentString = "<div style='position:relative; width:260px;height:110px;padding:4px;overflow:auto;'><img src='squelettes/images/guantoinfowindow.jpg' style='float:right;' /><div style='font-weight:bold;padding-bottom:10px;'>" + obj.nome + ", " + obj.citta + "</div><div style='font-size:90%'>" + obj.messaggio + "</div></div>";
			else
				contentString = "<div style='position:relative; width:260px;height:110px;padding:4px;overflow:auto;'><a onclick='openImage(\"" + obj.immagine + "\");' href='javascript:void(0);'><img src='img_mappa/mini_" + obj.immagine + "' style='float:right; max-width:260px; max-height:260px;' /></a><div style='font-weight:bold;padding-bottom:10px;'>" + obj.nome + ", " + obj.citta + "</div><div style='font-size:90%'>" + obj.messaggio + "</div></div>";         
			
			var iw = new google.maps.InfoWindow({
				content: contentString
			});
			
			
			google.maps.event.addListener(iw, 'closeclick', function() {
				window.mappa.stopAnim();
			});
			
			window.mappa.infoWindows[window.mappa.numMarkers] = iw;
			
			google.maps.event.addListener(marker, 'click', window.mappa.getOpenInfoWindowHandler(window.mappa.numMarkers) );
			
			window.mappa.numMarkers++;

	
			//var infoWinOpt = {noCloseOnClick:true};
			//var infoWindow = this.map.openInfoWindowHtml(point,content,infoWinOpt);
			
        }
        
        this.cambiaMessaggio();

    };
    
    this.getOpenInfoWindowHandler = function(idx) {
    	return function() {
	    	window.clearTimeout(window.mappa.timer);
	    	window.mappa.openInfoWindow(idx);
    	};
    };
    
    this.cambiaMessaggio = function() {
    	this.openInfoWindow(this.markerIndex);
    	
    	this.markerIndex = (this.markerIndex + 1) % this.numMarkers;
    	
	    this.timer = window.setTimeout("window.mappa.cambiaMessaggio()", 6000);
    }
    
    this.openInfoWindow = function(idx) {
    	if (this.curMarker != -1) {
    		this.infoWindows[this.curMarker].close();
    	}
    
    	this.infoWindows[idx].open(this.map, this.markers[idx]);
    	this.curMarker = idx;
    };

}
