	
// Creates a marker whose info window displays the given number
    function createMarker(point, icon, title, descr, image, lid) {
        var marker = new GMarker(point, icon);
        var infoWinOptions = { maxWidth : "280" };
        GEvent.addListener(marker, "click", function() {
            if (image != "") imagetag = "<td width='120' valign='top'><img src='" + image + "'></td>";
            else imagetag = "";
            if (lid != "") link = "<br><a href='/web/modules.php?name=Web_Links&l_op=visit&lid="+lid+"' target='_blank'>visita il sito &raquo;</a>";
            else link = "";
            marker.openInfoWindowHtml("<table width='100%' cellpadding='1' cellspacing='2'><tr>"+imagetag+"<td valign='top'><font style='font-size:12px; font-family: sans-serif; font-weight: bold;'>"+title+"</font><font style='font-size:10px; font-family: sans-serif; line-height: 10px;'><br>"+descr+"</font>"+link+"</td></tr></table>", infoWinOptions);
        });
        return marker;
    }





function gMapLoad() {

	map  = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(44.38, 12.1000), 8);
	map.addMapType(G_PHYSICAL_MAP);

    //map.enableContinuousZoom();
    //map.enableScrollWheelZoom();

    var mapControl = new GHierarchicalMapTypeControl();
    mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Stradario", false);
    map.addControl(mapControl, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(22, 5)));
    map.addControl(new GLargeMapControl3D(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(22, 35)));

	map.addControl(new MarkerControl());

	geocoder = new GClientGeocoder();
	point = new GLatLng(44.38, 12.1000);
	marker = new GMarker(point, {draggable: true});
	map.addOverlay(marker);
	address = "";
	GEvent.addListener(marker, 'click', showDetailBalloon);
	GEvent.addListener(marker, 'dragstart', closeInfoBalloon);
	GEvent.addListener(marker, 'dragend', assignPoint);


	$(document).ready(function() {
		var provincieshown = 1;
		$("#provincieDiv").show("slow");
		$("#zoomDiv").click(function() {
			if (provincieshown == 0) {
				$("#provincieDiv").show("slow");
				provincieshown = 1;
			} else {
				$("#provincieDiv").hide("slow");
				provincieshown = 0;
			} 
		});
	});


}



   function showAddress(addr) {
      geocoder.getLatLng(
        addr,
        function(point) {
            address = addr;
          if (!point) {
            alert(address + ": indirizzo NON trovato.");
          } else {
/*          map.clearOverlays();*/
            map.setCenter(point, 14);
            var html = address + "<br>Lat, Long<br>(" + map.getCenter().lat() + ","+ map.getCenter().lng() + ")";
            marker.setPoint(point);
/*            map.addOverlay(marker);*/
            marker.openInfoWindowHtml(html);
/*            document.dati.lat.value = map.getCenter().lat();*/
/*            document.dati.lon.value = map.getCenter().lng();*/
          }
        }
      );
    }


function showDetailBalloon() {
    marker.openInfoWindowHtml(address);
}

function closeInfoBalloon() {
  map.closeInfoWindow();
}

function assignPoint() {
	point = marker.getPoint();
	address = "Lat, Long<br>(" + point.lat() + ","+ point.lng() + ")";
	address += "<br><br><a href='modules.php?name=Mappe&op=segnala&lat="+point.lat()+"&lng="+point.lng()+"'>&raquo; Segnala questo punto geografico</a>";
}


//GEvent.addListener(marker, 'click', showDetailBalloon);
//GEvent.addListener(marker, 'dragstart', closeInfoBalloon);
//GEvent.addListener(marker, 'dragend', assignPoint);



				
//function gMapLoad() {
	// draw the map on the screen
//	map = new GMap2(document.getElementById("map"));
//	map.setCenter(new GLatLng(44.38, 12.1000), 8);
//	map.addMapType(G_PHYSICAL_MAP);
	
//	map.enableContinuousZoom();
//	map.enableScrollWheelZoom();
		
//	var mapControl = new GHierarchicalMapTypeControl();
//	mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Stradario", false);
//	map.addControl(mapControl, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(22, 5)));
//}		



	function MarkerControl() {	}

	MarkerControl.prototype = new GControl();


	MarkerControl.prototype.initialize = function(map) {
  		
  		var container = document.createElement("div");
		container.setAttribute("id", "zoomContainerDiv");

		var zoomDiv = document.createElement("div");
		zoomDiv.setAttribute("id", "zoomDiv");
  		this.setButtonStyle_(zoomDiv);
  		container.appendChild(zoomDiv);
  		zoomDiv.appendChild(document.createTextNode("Visualizza"));

		provincieDiv = document.createElement("div");
		provincieDiv.setAttribute("id", "provincieDiv");
		provincieDiv.style.display = "none";
		this.setButtonStyle_(provincieDiv);
  		container.appendChild(provincieDiv);
		
		var interesse = document.createElement("div");
		this.setSubButtonStyle_(interesse);
		var interesseCB = document.createElement("input");
		interesseCB.setAttribute("id","interesseCB");
		interesseCB.setAttribute("name","interesseCB");
		interesseCB.setAttribute("type","checkbox");
//		interesseCB.setAttribute("checked","checked");
		interesse.appendChild(interesseCB);
		interesse.appendChild(document.createTextNode(" Punti di interesse"));
		GEvent.addDomListener(interesseCB, "click", function() {
			showInteresseMarkers($("#interesseCB").attr("checked"));
  		});
		
		var hotel = document.createElement("div");
		this.setSubButtonStyle_(hotel);
		var hotelCB = document.createElement("input");
        hotelCB.setAttribute("id","hotelCB");
        hotelCB.setAttribute("name","hotelCB");
        hotelCB.setAttribute("type","checkbox");
//        hotelCB.setAttribute("checked","checked");
        hotel.appendChild(hotelCB);
		hotel.appendChild(document.createTextNode(" Alberghi / Hotel"));
		GEvent.addDomListener(hotel, "click", function() {
			 showHotelMarkers($("#hotelCB").attr("checked"));
  		});
  		
		var mangiare = document.createElement("div");
		this.setSubButtonStyle_(mangiare);
		var mangiareCB = document.createElement("input");
		mangiareCB.setAttribute("id","mangiareCB");
		mangiareCB.setAttribute("name","mangiareCB");
		mangiareCB.setAttribute("type","checkbox");
		mangiare.appendChild(mangiareCB);
		mangiare.appendChild(document.createTextNode(" Ristoranti / Pub"));
		GEvent.addDomListener(mangiare, "click", function() {
			showMangiareMarkers($("#mangiareCB").attr("checked"));
  		});
  		
        var aziende = document.createElement("div");
        this.setSubButtonStyle_(aziende);
        var aziendeCB = document.createElement("input");
        aziendeCB.setAttribute("id","aziendeCB");
        aziendeCB.setAttribute("name","aziendeCB");
        aziendeCB.setAttribute("type","checkbox");
        aziende.appendChild(aziendeCB);
        aziende.appendChild(document.createTextNode(" Aziende"));
        GEvent.addDomListener(aziende, "click", function() {
            showAziendeMarkers($("#aziendeCB").attr("checked"));
        });


		var webcam = document.createElement("div");
        this.setSubButtonStyle_(webcam);
        var webcamCB = document.createElement("input");
        webcamCB.setAttribute("id","webcamCB");
        webcamCB.setAttribute("name","webcamCB");
        webcamCB.setAttribute("type","checkbox");
        webcam.appendChild(webcamCB);
        webcam.appendChild(document.createTextNode(" Traffico"));
        GEvent.addDomListener(webcam, "click", function() {
            showWebcamMarkers($("#webcamCB").attr("checked"));
        });

/*		var webcam = document.createElement("div");
		this.setSubButtonStyle_(webcam);
		webcam.appendChild(document.createTextNode("Webcam"));
		GEvent.addDomListener(modena, "click", function() {
			map.setCenter(new GLatLng(44.64813932319396, 10.919723510742188), 10);
  		});*/
		
		provincieDiv.appendChild(interesse);
		provincieDiv.appendChild(hotel);
		provincieDiv.appendChild(mangiare);
		provincieDiv.appendChild(aziende);
		provincieDiv.appendChild(webcam);

		map.getContainer().appendChild(container);
		return container;
	}

	// By default, the control will appear in the top left corner of the map with 7 pixels of padding.
	MarkerControl.prototype.getDefaultPosition = function() {
  		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
	}

	// Sets the proper CSS for the given button element.
	MarkerControl.prototype.setButtonStyle_ = function(button) {
		button.style.textDecoration = "none";
		button.style.color = "#000000";
		button.style.backgroundColor = "white";
		button.style.font = "small Arial";
		button.style.border = "1px solid black";
		button.style.padding = "2px";
		button.style.marginBottom = "3px";
		button.style.textAlign = "center";
		button.style.width = "150px";
		button.style.cursor = "pointer";
	}

	// Sets the proper CSS for the given button element.
	MarkerControl.prototype.setSubButtonStyle_ = function(button) {
		button.style.textDecoration = "none";
		button.style.color = "#0000cc";
		button.style.backgroundColor = "white";
		button.style.font = "small Arial";
		button.style.borderBottom = "0px none #ccc";
		button.style.padding = "2px";
		button.style.textAlign = "left";
		button.style.width = "146px";
		button.style.cursor = "pointer";
	}




function showInteresseMarkers(checked) {
//	alert("checked: "+checked);
	if (checked == true) 
		selectCategoryCombo(-1,0);
	else 
		selectCategoryCombo(-2,0);
}



function showHotelMarkers(checked) {
    if (checked == true)
        selectCategoryCombo(-3,1);
    else
        selectCategoryCombo(-2,1);
}


function showAziendeMarkers(checked) {
    if (checked == true)
        selectCategoryCombo(-7,3);
    else
        selectCategoryCombo(-2,3);
}



function showWebcamMarkers(checked) {
    if (checked == true)
        selectCategoryCombo(-4,4);
    else
        selectCategoryCombo(-2,4);
}


function showMangiareMarkers(checked) {
    if (checked == true)
        selectCategoryCombo(-5,2);
    else
        selectCategoryCombo(-2,2);
}


function selectCategoryCombo(src, tipo) {
    if (tipo == 0) marray = markerarray;
    else if (tipo == 2) marray = markerarrayw;
	else if (tipo == 3) marray = markerarraya;
	else if (tipo == 4) marray = markerarraywc;
    else marray = markerarrayh;
    if (src == -1) catid = -1;
	if (src == -3) catid = -3;
	if (src == -4) catid = -4;
	if (src == -5) catid = -5;
	if (src == -6) catid = -6;
	if (src == -7) catid = -7;
    //else catid = src.options[src.selectedIndex].value;
    for (var j = 0; j < marray.length; j++) {
        if (marray[j] != null) map.removeOverlay(marray[j]);
        marray[j] = null;
    }

    if (src == -2) {
        return;
    }

    GDownloadUrl("/web/catdata.php?catid="+catid, function(data, responseCode) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));
		//alert (point);
        gid = markers[i].getAttribute("gid");
        title = markers[i].getAttribute("title");
        descr = markers[i].getAttribute("descr");
        image = markers[i].getAttribute("image");
        icon = markers[i].getAttribute("icon");
        lid = markers[i].getAttribute("lid");
        eval ("var m = createMarker(point, icon"+icon+", title, descr, image, lid);");
        marray[i] = m;
        map.addOverlay(m);
      }
    });
}


