var map = null;
var bounds =null;
var newzoom;
var markervet = new Array();
var markerList = new Array();
var slideClosed = false;


function showMyMap(myContainerMap){
	if (!slideClosed){
		hideMyMap(myContainerMap);
	}else{
		if ($('mapContainer')){
			Effect.BlindDown('mapContainer', {  afterFinish:onSlideFinish(myContainerMap) , duration: 0.8 });
		}
	}
}


function hideMyMap(myContainerMap){
	if ($('mapContainer')){
		Effect.BlindUp('mapContainer', { afterFinish:onSlideFinish(myContainerMap) , duration: 0.8 });
	}
}

function onSlideFinish(myContainerMap){
	slideClosed = !slideClosed;
	if (slideClosed){
		//$('labelOpenMap').innerHTML = _LabelApriMappa;
		//$('lblOpenMap').innerHTML = '';
		Element.removeClassName(myContainerMap,"mapBlock-OFF")
		Element.addClassName(myContainerMap,"mapBlock-ON")
	}else{
		//$('labelOpenMap').innerHTML = _LabelChiudiMappa;
		//$('lblOpenMap').innerHTML = '';
		Element.removeClassName(myContainerMap,"mapBlock-ON")
		Element.addClassName(myContainerMap,"mapBlock-OFF")

	}
}

function removeAllMarkers(){
	for (i=0;i<markerList.length;i++){
			map.removeOverlay(markerList[i][0]);
	}

}

function centerTheMarker(idMyMarker){
	if (slideClosed) {
		showMyMap();
	}

	for (i=0;i<markerList.length;i++){
			if (markerList[i][1] == idMyMarker){
				map.setCenter(markerList[i][0].getLatLng(),16);
				getInfoWindow(idMyMarker,markerList[i][0]);
				return;
			}
	}

}


function hide(element){
	element.style.display = "none";
	element.style.position = "absolute";
}
function show(element){
	element.style.display = "block";
	element.style.position = "relative";
}


function hideAllMarkers(){
	for (i=0;i<markerList.length;i++){
			markerList[i][0].hide();
	}
}


function hideMarkers(myMarkerID){
	for (i=0;i<markerList.length;i++){
		if (markerList[i][1] == myMarkerID){
			markerList[i][0].hide();
		}
	}
}

function showMarkers(myMarkerID){
	for (i=0;i<markerList.length;i++){
		if (markerList[i][1] == myMarkerID){
			markerList[i][0].show();
		}
	}
}

function getInfoWindow(id,sogg_Marker){
	var myCurrentHTML;
	var mySogg;
	
	mySogg = Soggetti.select(function(Sogg) {return Sogg[_IDSOGGETTO] == id});
	//$('IW_Loading').innerHTML = '';

	myCurrentHTML = myHTMLInfoWindow;
	
	if (mySogg.length > 0){
		for (i=0;i<mySogg.length;i++){
			//inizio replace stringhe
			myCurrentHTML = myCurrentHTML.replace(/{{link}}/g,mySogg[i][_LINKSCHEDA]);
			myCurrentHTML = myCurrentHTML.replace(/{{img}}/g,mySogg[i][_IMMAGINE]);
			myCurrentHTML = myCurrentHTML.replace(/{{nome}}/g,mySogg[i][_NOMESOGGETTO]);
			myCurrentHTML = myCurrentHTML.replace(/{{indirizzo}}/g,mySogg[i][_INDIRIZZO]);
			myCurrentHTML = myCurrentHTML.replace(/{{citta}}/g,mySogg[i][_CITTA]);
			myCurrentHTML = myCurrentHTML.replace(/{{categoria}}/g,mySogg[i][_CATSOGGETTO]);
		}
		
	}		

	//$('IW_Loading').innerHTML = myCurrentHTML;
	sogg_Marker.openInfoWindowHtml(myCurrentHTML);
}

function initialize(lat,lng) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));

	map.addControl(new GLargeMapControl3D());
	map.addControl(new GOverviewMapControl());

	// Add selectable terrain map
	map.addMapType(G_PHYSICAL_MAP);
	var hControl = new GHierarchicalMapTypeControl();
//	hControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
	map.addControl(hControl);
	
	//map.addMapType(G_SATELLITE_3D_MAP);
	
	map.enableScrollWheelZoom();
      
	var center = new GLatLng(lat,lng);
	map.setCenter(center);
	bounds = new GLatLngBounds;
    //map.setZoom(11);
  }  
}

function addMarker(lat,lng,idM,NumMarker,NomeStruttura,Localita){
        var marker = null;
        var title = idM.toString();
        var coord = new GLatLng(lat.toString(),lng.toString());
        var TxtCoord;
    
        var n = markervet.length;
        str = n.toString();
    
       var icon = new GIcon(G_DEFAULT_ICON);
         icon.image = '/gmap/images/bullet/number.png';
		 
         icon.iconSize = new GSize(24, 29);
         icon.shadow = '';
         icon.iconAnchor = new GPoint(11, 22);
         icon.infoWindowAnchor = new GPoint(12, 2);

      opts = { 
         title : NomeStruttura,
         id: idM, 
         icon: icon,
         clickable: true,
         labelText: NumMarker.toString(),
         labelOffset: new GSize(-11, -18), 
		 labelClass: 'iconLabelClass'
       };
      
      var marker = new LabeledMarker(coord, opts);
       
        //inserimento del marker nel vettore
         markervet.push(marker);
         marker.disableDragging();
   
        //************
        // On Click
        //************
         GEvent.addListener(marker, "click", function(latlng) {
         map.setCenter(marker.getLatLng());
         //OpenTabs(marker);
		 //marker.openInfoWindowHtml($(title).innerHTML)
		 getInfoWindow(marker.id,marker);
         });
          
        //*****************
        // On Double Click
        //*****************
         GEvent.addListener(marker, "dblclick", function() {
         map.setCenter(marker.getLatLng(),14);    
        });

		markerList[markerList.length] = [marker,idM];
        
        //Zoom e centramento
        bounds.extend(coord);
        newzoom =  map.getBoundsZoomLevel(bounds);
        map.setZoom(newzoom);
        map.setCenter(bounds.getCenter());
        map.addOverlay(marker);
        }

function MostraTabsGmap(id){
map.setCenter(markervet[id].getLatLng());
OpenTabs(markervet[id])
}

function CentraPunto(coord){
map.setCenter(coord);
map.setZoom(15);
}
