G = {
	mappa: null,
	geocoder: null,
	marker: null,
	icon: null,
	initZoomLevel: 5,
	/*
	 *	UnLoad Method
	*/

	unload: function(){
	 	GUnload();
	},
	/* 
	 * Pagina Registrazione	
	 *
	*/
	refresh: function(){
		setTimeout(function(){		
				$("#upd").fadeIn('slow'); 
				$("#upd").text('AGGIORNAMENTO');
				setTimeout(function(){ $("#upd").fadeOut('slow'); },5600);
				G.mappa.clearOverlays();
				var geoXml = new GGeoXml("http://metwit.net/last.kml?"+ new Date().getTime());
				G.mappa.addOverlay(geoXml);
				return G.refresh();				
		},60000);
	},
	
	meteo_diretta: function(data_cal){
		this.divMap();   
		var Glatlng = new GLatLng(41.90, 12.49);
		this.setCenter(Glatlng, 5);
		if(!data_cal){
			var data_cal = data;
		}
		var geoXml = new GGeoXml("http://metwit.net/last.kml?data="+data);
		this.mappa.addOverlay(geoXml);	
	},
	
	previsioni: function(){
		this.divMap();   
		this.geocoder.getLatLng(centro,
   			function(point){
				G.setCenter(point, Zoom);		
				var geoXml = new GGeoXml("http://metwit.net/last.kml?id="+id);
				G.mappa.addOverlay(geoXml);
			}
		);	
	},
	
	homepage: function(){
		this.divMap();   
		var Glatlng = new GLatLng(41.90, 12.49);
		this.setCenter(Glatlng, 5);
		var geoXml = new GGeoXml("http://metwit.net/last.kml?"+ new Date().getTime());
		this.mappa.addOverlay(geoXml);
		this.refresh();		 
	},
	/* Metodi per la pagina degli utenti */
	mapize: function(obj,icon,point){
		/* Effetti grafici */
		$("a.mapize").text('MapIT');
		obj.text("...");
		setTimeout(function(){obj.text('Mapped'); G.centraLocByPoint(icon,point);},300);
		/* Setto il punto nella mappa */
	},
	
	centraLocByPoint: function(icon,point){
		if(this.marker){
			this.marker.remove();
		}
   		this.marker = this.createMarkerIconHome(icon,point);
   		this.setCenter(point, 11);
   		this.mappa.addOverlay(this.marker);	
	},
	
	segnalazione_meteo: function(){
		this.divMap();
		this.centraLocByPoint(icon, GLatlng);	//crea un marker (this.marker) e lo centra.
		this.mappa.openInfoWindow(GLatlng, commento);
		GEvent.addListener(this.marker, "click", function(){
   			G.mappa.openInfoWindow(GLatlng, commento);
  		}); 
		this.setCenter(GLatlng, 11);
	},
	
	pageuserSegn: function(){
		if($("#map").length > 0){
			this.divMap();
			G.marker = this.createMarker(GLatlng);	
			G.setCenter(GLatlng, 11);
			G.mappa.addOverlay(G.marker);	
		}
	},	
	
	registrazione: function(){
		this.divMap();
		var Glatlng = new GLatLng(41.90, 12.49);
		G.setCenter(Glatlng, 6);
		this.marker = this.createMarker(Glatlng,1);
		this.mappa.addOverlay(this.marker);
		this.moveMarker();
	},
	
	setCenter: function(GLatLng, zoom){
		this.mappa.setCenter(GLatLng, zoom);
	},
	//pannello-profilo
	profilo: function(){
		this.divMap();
   		G.setCenter(GLatlng, 11);
   		G.marker =  G.createMarker(GLatlng, 1);   					
		G.mappa.addOverlay(G.marker);
		G.moveMarker();
	},
	
	//pannello-segnala
	segnala: function(){
		this.divMap();
     	G.setCenter(GLatlng, 11);
   		G.marker =  G.createMarker(GLatlng, 1);   					
		G.mappa.addOverlay(G.marker);
		G.moveMarker();
	},

	//``costruttore` (Utilizzati in quasi tutti i metodi)
	divMap : function(){
		if(GBrowserIsCompatible()){			
			this.mappa = new GMap2(document.getElementById("map"));
			this.mappa.setUIToDefault();
			this.geocoder = new GClientGeocoder();
		} else {
			alert('Non è possibile istanziare la classe js');
		}
	},
	
	//Restituisce la località in stringa dalle cordinate in numero.	
	getLocByLatLng: function(LatLng){
		this.geocoder.getLocations(LatLng, function(response){
        	if(response.Status.code == 200){
        		$("#localita").val(response.Placemark[0].address);        		
        	 }else{
				$("#localita").val('Località non trovata.');
			 }
     	});
	},
	getPointByLatLng: function(loc){},
	
	/* 
	 *	Da la possibilità di muovere il marker cliccando sulla mappa.
	*/	
	moveMarker: function(){
        GEvent.addListener(this.marker, "dragend", function(overlay,LatLng) {
      	  if(overlay){
      	     G.setCenter(overlay,G.mappa.getZoom());
 	  		 G.getLocByLatLng(overlay);
       	  }
        });
	},
	
	/* 
	 *	Centra la località nella mappa ricevendo l'indirizzo in string
	*/	
	centraLoc: function(address){
		this.geocoder.getLatLng(address,
   			function(point){
    			if(!point){
					$("#localita").val('Indirizzo non trovato.');
      				$("#localita").focus();
      			}else{
					G.getLocByLatLng(point);	//setto l'indirizzo meglio nella text della localita'
					G.marker.setLatLng(point);	//setto la mappa sulla localita'
					G.setCenter(point,G.mappa.getZoom());	//setto lo zoom sulla localita ed il 
      			}
    		}
    	);
	},
	//chiamata da previsioni.js
		
	centraLoc2: function(address,zoom){
		this.geocoder.getLatLng(address,
   			function(point){
    			if(!point){
     				if($("#response").is(":hidden")){
     					$("#response").slideDown();
     				}else{
     					$("#response").text('Scegli una regione.');
     				}
     				setTimeout(function(){ $("#response").slideUp(); },2000);
      			}else{
					if(!G.marker){
						G.marker = G.createMarker(point);
					}
					G.marker.setLatLng(point);	//setto la mappa sulla localita'
					G.mappa.addOverlay(G.marker);
					G.setCenter(point,zoom);	//setto lo zoom sulla localita ed il 
      			}
    		}
    	);
	},
	
	//correggere dv viene chiamata....
	createMarkerIconHome: function(icona,LatLng){
  	  var icon = null;
  	  var point = null;
  	  icon = new GIcon();
	  icon.image = "/icons/"+icona+".png";
	  icon.iconAnchor = new GPoint(16, 16);
	  icon.shadow = "/icons/"+icona+"shadow.png";  
	  icon.shadowSize = new GSize(59, 32);
 	  icon.iconSize = new GSize(32, 32);
 	  return new GMarker(LatLng, { icon: icon });	
	},
	/* 
	 *	Crea un marker per l'homepage, senza rimuovere i precedenti
	*/	
	createMarkerIcon: function (point){
		return new GMarker(point, { icon: this.icon, draggable: true } );
	},
	
	createMarkerIconHome2: function(icona,LatLng){
  	  var icon = null;
  	  var point = null;
  	  icon = new GIcon();
	  icon.image = "/icons/"+icona+".png";
	  icon.iconAnchor = new GPoint(16, 16);
	  icon.shadow = "/icons/"+icona+"shadow.png";  
	  icon.shadowSize = new GSize(59, 32);
 	  icon.iconSize = new GSize(32, 32);
 	  return new GMarker(LatLng, { icon: icon , draggable: true });	
	},
	
	/* 
	 *	Crea un marker nella mappa con o senza icona.
	*/	
	/* 	- registrazione()
	 *	- profilo()
	*/	
	createMarker: function (point,opt){
		if(!opt)
			return new GMarker(point);
		else
			return new GMarker(point, { draggable: true } );
	}
};

	$(document).ready(function(){ 
		 $("body").attr("unload","G.unload();");
	});
	
	function checkLock(id_form,testo,button_disable){
		if(button_disable){
		 	$("#"+button_disable).attr("disabled",true);
		 	$("#"+button_disable).val('Loading..');
		}
		var loc = escape($('#localita').val());			
		$.get('/api.php?loc='+loc, function(response){
			var obj = eval('(' + response + ')');
			if(obj.status == 200){
				if(obj.codiceStato == "IT"){
					$("#"+id_form).attr("disabled",false);
					// $("#"+id_form).append('<input type="hidden" name="toPhpByJs" value="' + escape(obj.arrayPhpToJs) +'">');
					// $("#localita").val(obj.indirizzo);
					 document.getElementById(id_form).submit();				 
					 return true;
				}else{
					if(confirm(testo)){
						$("#"+id_form).attr("disabled",false);
					// 	$("#"+id_form).append('<input type="hidden" name="toPhpByJs" value="' + escape(obj.arrayPhpToJs) +'">');
						//$('#localita').val(obj.indirizzo);
						document.getElementById(id_form).submit();
						return true;
	    	    	} else { 
	    	 			$('#localita').focus();
	    	 			 $("#"+button_disable).attr("disabled",false);
						$("#"+button_disable).val('Invia richiesta');
	    	 			return false;
	    	 		}
				}
			}else{
				$('#localita').val('Località non trovata.');
				$('#localita').focus();
				$("#"+id_form).attr("disabled",false);
				$("#"+id_form).val('Invia richiesta');
				return false;
			}
		});
	}
