// Google Maps api by Eduardo
// uso della mappa
/*----------------------------------------*/
// all'onload del documento
// usare la funzione: showmaps();
// per richiamare un indirizzo nella pagina usare:
//
// per un indirizzo usare
// showAddress('via aniello falcone 300, napoli italia',true,'nascar studios','dati testuali',false);
//comandi
//indirizzo di default | attivare il segnaposto | nome della nuvoletta (richiede che activemarker sia true) | testo della nuvoletta (richiede che activemarker sia true) | apertura della nuvoletta in automatico (richiede che activemarker sia true)
//
// per delle latutudini e longitudini usare
// showlatlng(latitudine,longitudine);
// espresse in questo modo 37.4419 (utilizzare maps.google.it per ricavarle precisamente)
//
/*----------------------------------------*/

//campi modificabili
// google maps api code
var apicode = GMapKey;

//ID del div della mappa
var idfrommaps = 'map';

//controlli della mappa || 0 - disattivo, 1 - piccolo, 2 - larghi, 3 - solo zoom
var mapscontroll=1;

//tipologie della mappa || 0 - disattivo, 1 - attivo
var mapsoptions=0;

//scala della mappa || 0 - disattivo, 1 - attivo
var mapscale=0;

//navigatore della mappa || 0 - disattivo, 1 - attivo
var mapsnavi=0;

//scroll della mappa con con la rotellina del mouse || 0 - disattivo, 16 - attivo
var mapscroll=0;

//livello dello zoom || 13 - citta, 16 - strada
var zoomlevel=15;
var zoomlevelmoremarker=9;
//fine campi modificabili

var map = null;
var geocoder = null;
var icon = null;
var selected = -1
var textindivAD = new Array();
var textindivLL = new Array();
var textindivMM = new Array();


function loadinner(divmaps){
	var el = divmaps.childNodes[0];
	i=0;
	z=0;
	k=0;
	while (el) {
		if((el.nodeName.toLowerCase())=="p"){
			switch(el.className){
				case "address":
					textindivAD[i]=el.innerHTML;
					i++;
				break;
				case "latlong":
					textindivLL[z]=el.innerHTML;
					z++;
				break;
				case "mostra":
					textindivMM[k]=el.innerHTML;
					k++;
				break;
			}
		}
		el = el.nextSibling;
	}
}
function insertaddress(){
	
	if(selected > -1) {
		showAddress(textindivAD[selected],true,'whereIsIt',textindivMM[selected],true);
	}

	for(i=0;i<textindivAD.length;i++){
		finale=textindivMM[i];
		if(i != selected) {
			showAddress(textindivAD[i],true,'whereIsIt',finale,i == textindivAD.length-1);
		}
	}
	
	for(i=0;i<textindivLL.length;i++){
		latlon=textindivLL[i].split(",");
		showlatlng(latlon[0],latlon[1]);
	}
}
function googlesapi_load() {
	if(document.getElementById(idfrommaps)){
		divmaps=document.getElementById(idfrommaps);
		loadinner(divmaps);

		if(textindivAD && textindivAD.length > 1) {
			zoomlevel = zoomlevelmoremarker;
		}

		if (GBrowserIsCompatible() && divmaps) {
			map = new GMap2(divmaps);
			geocoder = new GClientGeocoder();
			switch(mapscontroll){
				case 1:
					map.addControl(new GSmallMapControl());
					break;
				case 2:
					map.addControl(new GLargeMapControl());
					break;
				case 3:
					map.addControl(new GSmallZoomControl());
					break;
			}
			switch(mapsoptions){
				case 1:
					map.addControl(new GMapTypeControl());
					break;
			}
			switch(mapscale){
				case 1:
					map.addControl(new GScaleControl());
					break;
			}
			switch(mapsnavi){
				case 1:
					map.addControl(new GOverviewMapControl());
					break;
			}

			map.enableContinuousZoom();
			switch(mapscroll){
				case 1:
					GEvent.addDomListener(divmaps, "DOMMouseScroll", wheelZoom);
					GEvent.addDomListener(divmaps, "mousewheel",     wheelZoom);
					break;
			}
		}

		// Verifico presenza selezionato
		if(document.getElementById('richiedi-appuntamento-form')){
			if(document.getElementById('filiale')){
				
				// Se voce selezionata
				selectFiliale = document.getElementById('filiale')
				if(selectFiliale.selectedIndex > 0) {
					selected = selectFiliale.selectedIndex - 1; 					
				}				
			}			
		}
		
		insertaddress();
		
	}
}
function showAddress(address,setmarker,tabname,tabtext,opentab) {

	if(geocoder){
	
		geocoder.getLatLng(address,
			function(point) {
			if(!point){
				//idmap=document.getElementById(idfrommaps);
				//idmap.innerHTML = 'Google Maps error<br /><strong>' + address + '</strong> not found';
				//idmap.style.width = '610';
				//idmap.style.height = '0';
				//idmap.style.border = '0';
				//idmap.style.background = '#fff';
				//alert(address + " not found");
			}else{
				map.setCenter(point, zoomlevel);
				if(setmarker){
					var marker = new GMarker(point);
					var infoTabs = [ new GInfoWindowTab(tabname, tabtext)];
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowTabsHtml(infoTabs);
					});
					map.addOverlay(marker);
					if(opentab) {
						marker.openInfoWindowTabsHtml(infoTabs);
					}
				}
			}
		});
	}
}
function showlatlng(latshow,lonshow){
	map.setCenter(new GLatLng(latshow, lonshow), zoomlevel);
}
function wheelZoom(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();
		}
	}
}
function showmaps(){
	googlesapi_load();
}
document.write('<script src="http://maps.google.com/maps?file=api&v=2&key='+apicode+'" type="text/javascript"></script>');

