	// variabile mappa
var map;
  	//icona base con shadow e altre info
var baseIcon;
  	// navigatore localita
var lista_loc_html = "";
      // arrays to hold copies of the markers and html used by the lista_loc_html
      // because the function closure trick doesnt work there
var gmarkers = [];
var htmls = [];
var base_img = "<table><tr><td><img src=\"http://www.sardiniapoint.it/opsmedia/jpg/";
var select_html = "<select onChange=\"handleSelected(this)\">" + "<option selected> - Select a location - </option>";	

      //carica mappa
function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		var center = new GLatLng(39.8000959563484, 9.0087890625);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(center, 8);
	    //leggo le localita DWR
		mappeManager.listaLocalita(localitaSuMappa);
        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		toggleLayer( "listaCaseLoc", "none" );
	}
}
  
  //callback dwr
var localitaSuMappa = function (localita) {
	if (localita != null && typeof localita == "object") {
		for (var i = 0; i < localita.length; i++) {
			var lat = localita[i].lat;
			var lng = localita[i].lng;
			var txt = base_img 
				+ localita[i].fotospiaggia 
				+ "\">&nbsp;&nbsp;</td><td><a class=\"navigatore dim8pt\" href=\"/rs2007/listaCase.htm?action=listaCaseSottoLoc&sottoLoc="
				+localita[i].idsottoloc
				+ "\">" 
				+ localita[i].nome 
				+ " - " 
				+ localita[i].descrizione 
				+ " (" 
				+ localita[i].numcase 
				+ ")</a></td></tr><tr><td colspan=2>"
        		+ "Directions - Itinerario: <br><b>Inizio - Start - Départ : </b>" 
        		+ "<form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\">" 
        		+ "<input type=\"text\" SIZE=40 MAXLENGTH=40 name=\"saddr\" id=\"daddr\" value=\"\" /><br>" 
        		+ "<INPUT value=\"Ok\" TYPE=\"SUBMIT\">" 
        		+ "<input type=\"hidden\" name=\"daddr\" value=\"" 
        		+ lat 
        		+ "," 
        		+ lng
        		+ "\"/><input type=\"hidden\" name=\"hl\" value=\""+RS_LINGUA+"\">";
				+"</td></tr></table>";
			var txtLoc = localita[i].nome + " - " + localita[i].descrizione + " (" + localita[i].numcase + ")";
			var marker = createMarker(new GLatLng(lat, lng), txt, txtLoc);
			gmarkers[i] = marker;
			htmls[i] = txt;
			map.addOverlay(marker);
		}
       // put the assembled side_bar_html contents into the side_bar div
        select_html += '</select>';
        document.getElementById("selection").innerHTML = select_html;		
	}
};

      // This function picks up the click and opens the corresponding info window
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
	// chiamo funzione dwr per lista case della località click
	toggleLayer( "listaCaseLoc", "block" );
}
        
    // creo marker mappe
function createMarker(point, html, txtLoc) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function () {
		marker.openInfoWindowHtml(html);
		// chiamo funzione dwr per lista case della località click		
		toggleLayer( "listaCaseLoc", "block" );
		
	});
	//GEvent.addListener(marker, "mouseover", function () {
	    //marker.showMapBlowup();
		//marker.openInfoWindowHtml(html);
	//});
	select_html += "<option> " + txtLoc + "</option>";
	return marker;
}
      // ======= This function handles selections from the select box ====
      // === If the dummy entry is selected, the info window is closed ==
function handleSelected(opt) {
	var i = opt.selectedIndex - 1;
	if (i > -1) {
		GEvent.trigger(gmarkers[i], "click");
	} else {
		map.closeInfoWindow();
	}
}
//nascondo e visualizzo div
function toggleLayer( whichLayer, wichoption )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  //vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  vis.display = wichoption; 
}
     // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }


