var initLat = 42;
var initLng = 12;
var initZoom = 6;
var layerRegioni;
var layerProvince;
var layerComuni;
var map;

var arrCentroRegioni = [];

function initialize(){
	
	//ridimensiono la pagina
  	ridimensiona();
  	
	//carico la mappa
	var latlng = new google.maps.LatLng(initLat, initLng);
    var myOptions = {
      zoom: initZoom,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map"),myOptions);
    
    //carico il layer fusione per le regoni
	layerRegioni = new google.maps.FusionTablesLayer({
  		clickable: false, 
	    query: {
	      select: 'geometry',
	      from: '1088735'
  		}
	});
  	layerRegioni.setMap(map);    	    
  	
	google.maps.event.addListener(map, 'zoom_changed', function() {
		if (map.getZoom() > 10) {
			if (layerRegioni.getMap()!= null) {
				layerRegioni.setMap(null);
			}
		} else {
			if (layerRegioni.getMap()== null) {
				layerRegioni.setMap(map);
			}
		}
	});
  	
  	
  	//setto l'array dell centro regioni
  	setArrCentroRegioni();
  	
  	//carico i dati per "Aolo attive" e per "Solo sportello"
  	caricaDati(1, 1);
  	
  	
}

function sceltaSelTipo (tipo) {
	var canaleTutti = tipoCanale = document.getElementById("selTipoCanale0").checked;
	var canaleSport = tipoCanale = document.getElementById("selTipoCanale1").checked;
	var canaleWebTel = tipoCanale = document.getElementById("selTipoCanale2").checked;
	var tipoCanale = 0;
	if (canaleTutti==true) {
		tipoCanale = 0;
	} else if (canaleSport==true) {
		tipoCanale = 1;
	} else if (canaleWebTel==true) {
		tipoCanale = 2;
	} else {
		tipoCanale = 0;
	}
	
	caricaDati(tipo, tipoCanale);
	
}

function sceltaSelTipoCanale (tipoCanale) {
	
	var tipoSel0 = document.getElementById("selTipo0").checked;
	var tipoSel1 = document.getElementById("selTipo1").checked;
	var tipoSel2 = document.getElementById("selTipo2").checked;
	var tipo = 0;
	if (tipoSel0==true) {
		tipo = 0;
	} else if (tipoSel1==true) {
		tipo = 1;
	} else if (tipoSel2==true) {
		tipo = 2;
	} else {
		tipo = 0;
	}
	
	caricaDati(tipo, tipoCanale);
	
}

function loading(add) {
	if(add) {
		//aggiungere la frulla
		openDOMPoupUp(true);
		var loadingHTML = "<div style='text-align:center;'><img src='" + baseUrl + "/img/loading_grande.gif' /><br />Attendere, caricamento dati in corso...</div>";
		setAndCenterDOMPoupUp(loadingHTML);
	} else {
		//rimuovere la frulla
		closeDOMPoupUp();
	}
}

function ridimensiona() {
	try {
		var centroOld = map.getCenter();
	} catch (e){
	}
	//calcolo la larghezza dei div sinistra e destra + altezza mappa
	var myWidth = 0;
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	
	//try catch per problema in editing
	try {
		var newWidth = myWidth-240;
		if (myWidth<300) {
			document.body.style.width = "500px";
		}
		if (myHeight<300) {
			document.body.style.height = "500px";
		}
		if (newWidth<300) {
			document.getElementById("map").style.width="300px";
		} else {
			document.getElementById("map").style.width=newWidth+"px";
		}
		var newHeight = myHeight-150;
		if (newHeight<300) {
			document.getElementById("map").style.height="300px";
		} else {
			document.getElementById("map").style.height=newHeight+"px";
		}
	} catch (e){
	}
	//metto il try perchè se map non è inizializzato non deve schiantare
	try {
		map.setCenter(centroOld);
	} catch (e){
	}
}

function setArrCentroRegioni () {
	arrCentroRegioni ['ABRUZZO'] = new google.maps.LatLng(42.23472258867125361803118721581995487213,13.86008031445209098819759674370288848877);
	arrCentroRegioni ['BASILICATA'] = new google.maps.LatLng(40.51005739213198353354528080672025680542,15.84855669416243628688789613079279661179);
	arrCentroRegioni ['CALABRIA'] = new google.maps.LatLng(39.22767014440564281585466233082115650177,16.32193435776017764737844117917120456696);
	arrCentroRegioni ['CAMPANIA'] = new google.maps.LatLng(40.65826477309337150245482916943728923798,14.85710199373971818204154260456562042236);
	arrCentroRegioni ['EMILIA ROMAGNA'] = new google.maps.LatLng(44.45452262883138416782458079978823661804,11.05686264300096866008971119299530982971);
	//arrCentroRegioni ['EMILIA-ROMAGNA'] = new google.maps.LatLng(43.89891364583333199789194623008370399475,12.40315302083333293126088392455130815506);
	arrCentroRegioni ['FRIULI VENEZIA GIULIA'] = new google.maps.LatLng(45.99964087020340031131127034313976764679,13.10505933109666543145976902451366186142);
	arrCentroRegioni ['LAZIO'] = new google.maps.LatLng(41.96513647134568714136548805981874465942,12.6902063606671600126674093189649283886);
	arrCentroRegioni ['LIGURIA'] = new google.maps.LatLng(44.25465556976587322424165904521942138672,8.88457457557726471009118540678173303604);
	arrCentroRegioni ['LOMBARDIA'] = new google.maps.LatLng(45.47858996006338827555737225338816642761,9.73477596513470544437041098717600107193);
	arrCentroRegioni ['MARCHE'] = new google.maps.LatLng(43.4483653326537364591786172240972518920,12.92628409054456284366096951998770236969);
	arrCentroRegioni ['MOLISE'] = new google.maps.LatLng(41.76394608150469878182775573804974555969,14.54874769368562503757402737392112612724);
	arrCentroRegioni ['PIEMONTE'] = new google.maps.LatLng(44.93433426503005989616212900727987289429,8.124103969438877470565785188227891922);
	arrCentroRegioni ['PUGLIA'] = new google.maps.LatLng(40.68846150958217577908726525492966175079,17.2211679681319971280117897549644112587);
	arrCentroRegioni ['SARDEGNA'] = new google.maps.LatLng(40.20670952522289098851615563035011291504,8.99255758774025970581078581744804978371);
	arrCentroRegioni ['SICILIA'] = new google.maps.LatLng(37.59939791265973951794876484200358390808,13.80525132912729091572145989630371332169);
	arrCentroRegioni ['TOSCANA'] = new google.maps.LatLng(43.35744469615328711142865358851850032806,11.21612702249015214306382404174655675888);
	arrCentroRegioni ['TRENTINO ALTO ADIGE'] = new google.maps.LatLng(46.3384543767349654785903112497180700302,11.24254303767349583154100400861352682114);
	//arrCentroRegioni ['UMBRIA'] = new google.maps.LatLng(43.03429555960852326279564294964075088501,12.43614277556231151322663208702579140663);
	arrCentroRegioni ['UMBRIA'] = new google.maps.LatLng(43.034295,12.436142);
	arrCentroRegioni ['VALLE D`AOSTA'] = new google.maps.LatLng(45.7440050950783003713695507030934095382,7.37212624999999999175770426518283784389);
	arrCentroRegioni ['VENETO'] = new google.maps.LatLng(45.68939465538674227218507439829409122467,12.07425397168508496292815834749490022659);
}

