// Google Maps api by Eduardo
// versione 2.1
// uso della mappa
/*----------------------------------------*/
// all'onload del documento 
// usare la funzione: GoogleMapsApi.init();
// per richiamare un indirizzo nella pagina usare:
//
// per un indirizzo usare 
// showAddress('via roma 1, napoli italia',true,'nome nuvola','dati testuali',false);
//comandi 
//indirizzo di default | attivare il segnaposto | testo della nuvoletta (richiede che activemarker sia true) | apertura della nuvoletta in automatico (richiede che activemarker sia true) | Apertura della nuoletta (richiede che activemarker sia true)
//
// per delle latutudini e longitudini usare
// GoogleMapsApi.showlatlng(latitudine,longitudine);
// espresse in questo modo 37.4419 (utilizzare maps.google.it per ricavarle precisamente)
// 
/*----------------------------------------*/

var isLocalHost  = true
var map = null;
var geocoder = null;
var icon = null;
var textindivAD = new Array();

var GoogleMapsApi ={
	App: {
		//campi modificabili
		// google maps api code
		apicode		: 'ABQIAAAAYTH8Ke1kQL1CI-xJfOaX3hRA21RGw2-2Y3o3XCUTo1qqw7xlxhQ7Z8T4fPh4SGau-tC-OsAoPzBYVg', //for http://www.vih-service.it/
//		apicode		: 'ABQIAAAAEvJBCEkOIhfB7r6GEwPneRTycpGv-eazy6k0ZvZ6J35OVNUyPBSSVSF58ZcUJBaSlVvKIuEEkAeFyg',//for http://foto-immagine.com/
//      	apicode		: 'ABQIAAAAEvJBCEkOIhfB7r6GEwPneRT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRn5ZTtg74zTxHTmt7HLI7fm_JGDQ',//for localhost
		//struttura della mappa
		idfrommaps	:	'map',
		contentname	:	'div',
		titlename	:	'h3',
		textname	:	'p',
		
		//controlli della mappa || 0 - disattivo, 1 - piccolo, 2 - larghi, 3 - solo zoom
		mapscontroll	:	1,
		
		//tipologie della mappa || 0 - disattivo, 1 - attivo
		mapsoptions	:	0,
		
		//scala della mappa || 0 - disattivo, 1 - attivo
		mapscale	:	0,
		
		//navigatore della mappa || 0 - disattivo, 1 - attivo
		mapsnavi	:	0,
		
		//scroll della mappa con con la rotellina del mouse || 0 - disattivo, 1 - attivo
		mapscroll	:	1,
		
		//livello dello zoom || 13 - citta, 16 - strada
		zoomlevel	:	8,
		
		//reverse geocoding || 0 - no, 1 - si
		reversegeocode	:	1,
		
		//errori || 0 - no, 1 - si
		error	:	1
		//fine campi modificabili
	},
	init:function(){
		GoogleMapsApi.loadmap();
	},
	loadmap:function(){
		if($(GoogleMapsApi.App.idfrommaps)){
			divmaps=document.getElementById(GoogleMapsApi.App.idfrommaps);
			GoogleMapsApi.loadinner();
			if (GBrowserIsCompatible() && divmaps) {
			
				map = new GMap2(divmaps);
				geocoder = new GClientGeocoder();

				switch(GoogleMapsApi.App.mapscontroll){
					case 1:
						map.addControl(new GSmallMapControl());
						break;
					case 2:
						map.addControl(new GLargeMapControl());
						break;
					case 3:
						map.addControl(new GSmallZoomControl());
						break;
				}
				switch(GoogleMapsApi.App.mapsoptions){
					case 1:
						map.addControl(new GMapTypeControl());
						break;
				}
				switch(GoogleMapsApi.App.mapscale){
					case 1:
						map.addControl(new GScaleControl());
						break;
				}
				switch(GoogleMapsApi.App.mapsnavi){
					case 1:
						map.addControl(new GOverviewMapControl());
						break;
				}
				switch(GoogleMapsApi.App.reversegeocode){
					case 1:
						GEvent.addListener(map, "click", GoogleMapsApi.reverseGeocode);
						break;
				}
				
				map.enableContinuousZoom();
				switch(GoogleMapsApi.App.mapscroll){
					case 1:
						GEvent.addDomListener(divmaps, "DOMMouseScroll", GoogleMapsApi.wheelZoom);
						GEvent.addDomListener(divmaps, "mousewheel",     GoogleMapsApi.wheelZoom);
						break;
				}
			}
			GoogleMapsApi.insertaddress();
		}
	},
	loadinner:function(){
		idmappa = GoogleMapsApi.App.idfrommaps;
		
		div = GoogleMapsApi.App.contentname;
		title = GoogleMapsApi.App.titlename;
		text = GoogleMapsApi.App.textname;
		
		divmaps_div=$$("#"+idmappa+" "+div);
		divmaps_div_title=$$("#"+idmappa+" "+div+" "+title);
		divmaps_div_p=$$("#"+idmappa+" "+div+" "+text);
		
		divmaps_div.each(function(e,i){
			textindivAD[i] = new Array();
			switch(e.getAttribute("class")){
				case "address":	
					textindivAD[i][0]="address";
					break;
				case "latlong":
					textindivAD[i][0]="latlong";
					break;	
			}
			textindivAD[i][1]=divmaps_div_p[i].get('text');
			if(divmaps_div_title[i]){
				textindivAD[i][2]=divmaps_div_title[i].get('text');
			}
		});
	},
	insertaddress: function(){
		textindivAD.each(function(e,i){
			tabtext='';
			opentab=false;
			if(e[2]){
				tabtext=e[2];
				opentab=true;
			}
			switch(e[0]){
				case "address":
					GoogleMapsApi.showAddress(e[1],true,tabtext,opentab,false);
					break;
				case "latlong":
					latlon=e[1].split(",");
					GoogleMapsApi.showlatlng(latlon[0],latlon[1],true,tabtext,opentab,false);
					break;	
			}
		});
	},
	showAddress: function(address,setmarker,tabtext,opentab,opened) {
		if(geocoder){
			geocoder.getLatLng(address,
				function(point) {
				if(!point){
					idmap=document.getElementById(GoogleMapsApi.App.idfrommaps);
					if(GoogleMapsApi.App.debug) idmap.setHTML('Google Maps error<br /><strong>' + address + '</strong> not found');
				}else{
					map.setCenter(point, GoogleMapsApi.App.zoomlevel);
					if(setmarker){
						GoogleMapsApi.addMarker(point,tabtext,opentab,opened);
					}
				}
			});
		}
	},
	reverseGeocode:function(overlay, latlng) {
      if (latlng) {
        geocoder.getLocations(latlng, function(addresses) {
          if(addresses.Status.code != 200) {
            if(GoogleMapsApi.App.error) alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
          }else {
            address = addresses.Placemark[0];
            var myHtml = address.address;
            map.openInfoWindow(latlng, "<br/>"+myHtml);
          }
        });
      }
    },
	addMarker: function(point,tabtext,opentab,opened){
		var marker = new GMarker(point);
		map.addOverlay(marker);
		if(opentab){
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(tabtext);
			});			
		}
		if(opened){
			marker.openInfoWindowHtml(infoTabs);
		}
	},
	showlatlng: function(latshow,lonshow,setmarker,tabname,tabtext,opentab,opened){
		var point =new GLatLng(latshow, lonshow);
		map.setCenter(point, GoogleMapsApi.App.zoomlevel);
		if(setmarker){
			map.setCenter(point, GoogleMapsApi.App.zoomlevel);
			if(setmarker){
				GoogleMapsApi.addMarker(point,tabtext,opentab,opened);
			}
		}
	},
	wheelZoom: function(a){
		if (a.detail){
			if (a.detail < 0){ 
				map.zoomIn(); 
			}else if (a.detail > 0){
				map.zoomOut(); 
			}
		}else if (a.wheelDelta){
			if (a.wheelDelta > 0){
				map.zoomIn(); 
			}else if (a.wheelDelta < 0){
				map.zoomOut(); 
			}
		}
	}

}

document.write('<script src="http://maps.google.com/maps?file=api&v=2&key='+GoogleMapsApi.App.apicode+'&sensor=true" type="text/javascript"></script>');

