function JMap_newMap(id)
{
	if(!GBrowserIsCompatible()) return null;
	map = new GMap2(document.getElementById(id));
	return map;
}

function JMap_setCenter(map, lat, lng, zoom, save)
{
	if(__jmap_arg(map,null)==null) return -1;
	if(__jmap_arg(lat,0)==0 || __jmap_arg(lng,0)==0) return -1;
	map.setCenter(new GLatLng(lat,lng), __jmap_arg(zoom,10));
	if(__jmap_arg(save,false)) map.savePosition();
	return 0;
}

function JMap_setControls(map, mapTypes, searchBar, arrowZoom, scale, mapDrag, infoWindow, dbClickZoom, continueZoom, scrollZoom)
{
	if(__jmap_arg(map,null)==null) return -1;
	
	mapTypes = __jmap_arg(mapTypes,0);
	map.addControl(new GMapTypeControl()); // aggiunge il controllo
	if(mapTypes>=0) map.addMapType(G_NORMAL_MAP);
	if(mapTypes>=1)	map.addMapType(G_SATELLITE_MAP); else map.removeMapType(G_SATELLITE_MAP);
	if(mapTypes>=2)	map.addMapType(G_HYBRID_MAP);	 else map.removeMapType(G_HYBRID_MAP);
	if(mapTypes>=3)	map.addMapType(G_PHYSICAL_MAP);	 else map.removeMapType(G_PHYSICAL_MAP);
	
	arrowZoom = __jmap_arg(arrowZoom, 0); if(arrowZoom<0 || arrowZoom>2) arrowZoom=0;
	scale = __jmap_arg(scale, false);
	mapDrag = __jmap_arg(mapDrag, true);
	infoWindow = __jmap_arg(infoWindow, true);
	dbClickZoom = __jmap_arg(dbClickZoom, true);
	continueZoom = __jmap_arg(continueZoom, true);
	scrollZoom = __jmap_arg(scrollZoom, true);
	
	if(arrowZoom==0)	map.addControl(new GLargeMapControl()); // Freccette e Zoom - controlli grandi
	if(arrowZoom==1)	map.addControl(new GSmallMapControl()); // Freccette - controlli piccoli
	if(arrowZoom==2)	map.addControl(new GSmallZoomControl()); // Zoom - controlli piccoli
	if(scale)			map.addControl(new GScaleControl()); // visualizza la scala della mappa
	if(mapDrag) 		map.enableDragging();			else map.disableDragging(); // abilita il trascinamento
	if(infoWindow) 		map.enableInfoWindow(); 		else map.disableInfoWindow(); // abilita i quadri informazione
	if(dbClickZoom)		map.enableDoubleClickZoom(); 	else map.disableDoubleClickZoom(); // abilita lo zoom con il doppio click
	if(continueZoom) 	map.enableContinuousZoom(); 	else map.disableContinuousZoom(); // abilita lo zoom continuato
	if(scrollZoom) 		map.enableScrollWheelZoom();	else map.disableScrollWheelZoom(); // abilita lo zoom con la rotellina del mouse
	if(searchBar)		map.enableGoogleBar();			else map.disableGoogleBar(); // abilita la barra di ricerca sulla mappa
	
	return 0;
}

function JMap_createMarker(map, lat, lng, icon)
{
	if(__jmap_arg(map,null)==null) return null;
	if(__jmap_arg(lat,0)==0 || __jmap_arg(lng,0)==0) return null; // Se sono null o indefinite, termina l'esecuzione
	icon = __jmap_arg(icon,G_DEFAULT_ICON); // Controllo sull'oggetto icona
	return (new GMarker(new GLatLng(lat,lng),icon));
}

function JMap_addMarker(map, marker, html)
{
	if(__jmap_arg(map,null)==null) return null;
	if(__jmap_arg(marker,null)==null) return null;
	map.addOverlay(marker); // Aggiunge il marker alla mappa
	if(__jmap_arg(html,"")!="") GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); } );
}

function JMap_createIcon(path, width, height, anchorX, anchorY, infowin_anchorX, infowin_anchorY)
{
	if(__jmap_arg(path,0)==0 || __jmap_arg(width,0)==0 || __jmap_arg(height,0)==0) return -1;
	anchorX = __jmap_arg(anchorX,width/2);
	anchorY = __jmap_arg(anchorY,height/2);
	infowin_anchorX = __jmap_arg(infowin_anchorX,width/2);
	infowin_anchorY = __jmap_arg(infowin_anchorY,height/2);
	icon = new GIcon();
	icon.image = path;
	icon.iconSize = new GSize(width, height);
	icon.iconAnchor = new GPoint(anchorX, anchorY);
	icon.infoWindowAnchor = new GPoint(infowin_anchorX, infowin_anchorY);
	return icon;
}

/*

if(__jmap_arg(icon,G_DEFAULT_ICON)); // Controlli icona
	map.addOverlay(marker = new GMarker(new GLatLng(lat,lng),icon)); // Aggiunge il marker alla mappa
	if(__jmap_arg(html,"")!="")
	{
		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); } );
	}
	marker=undefined;
	return 0;

function JMap_createIcon(map, path, width, height, anchorX, anchorY, infowin_anchorX, infowin_anchorY)
{
	if(__arg(path,0)==0 || __arg(width,0)==0 || __arg(height,0)==0) return -1;
	anchorX = __arg(anchorX,width/2);
	anchorY = __arg(anchorY,height/2);
	infowin_anchorX = __arg(infowin_anchorX,width/2);
	infowin_anchorY = __arg(infowin_anchorY,height/2);
	id = this.icons.length;
	this.icons[id] = new GIcon();
	this.icons[id].image = path;
	this.icons[id].iconSize = new GSize(width, height);
	this.icons[id].iconAnchor = new GPoint(anchorX, anchorY);
	this.icons[id].infoWindowAnchor = new GPoint(infowin_anchorX, infowin_anchorY);
	return id;
}*/

/****************************************************************************************************
*   PRIVATE - Funzioni private (da notare il prefisso "__jmap_")											*
****************************************************************************************************/

// Crea le variabili dell'oggetto che hanno il prefisso "ctrl_"
/*function __createCtrl(y, n)
{
	__jmap_arg(ctrl_ArrowZoom,0); if(y.ctrl_ArrowZoom<0 || y.ctrl_ArrowZoom>3) y.ctrl_ArrowZoom=0;
	y.ctrl_Scale 		= __arg(ctrl_Scale,true);
	y.ctrl_MapDrag 		= __arg(ctrl_MapDrag,true);
	y.ctrl_InfoWindow 	= __arg(ctrl_InfoWindow,true);
	y.ctrl_DbClickZoom 	= __arg(ctrl_DbClickZoom,true);
	y.ctrl_ContinueZoom = __arg(ctrl_ContinueZoom,true);
	y.ctrl_ScrollZoom 	= __arg(ctrl_ScrollZoom,true);
}*/

// Controlla che v non sia null o undefined - se lo è restituisce dv, altrimenti restituisce lo stesso valore della variabile
function __jmap_arg(v, dv) { if(v==null || v==undefined)	return dv; else	return v; }

// Ricerca un elemento in un array (ricerca binaria)
function __arraySearch(array, e, start, total)
{
	start = __arg(start,0);
	total = __arg(total,array.length-1);
	m = (start + total) / 2; // Determina l'elemento centrale
	if(m < start) { return -1; } // l'elemento cercato non c'è
	else if(e < array[m]) { return __arraySearch(array, e, start, m-1); } // Si ripete la ricerca nella parte inferiore
	else if (e > array[m]) { return __arraySearch(array, e, m+1, total); } // Si ripete la ricerca nella parte superiore
	else { return m; }  // m rappresenta l'indice dell'elemento cercato
}





function myGMap_old(id, lat, lng, zoom, mapTypes, ctrl_ArrowZoom, ctrl_Scale, ctrl_MapDrag, ctrl_InfoWindow, ctrl_DbClickZoom, ctrl_ContinueZoom, ctrl_ScrollZoom)
{
	this.error = 0; // Flag 0 = nessun errore
	
	// Browser non compatibile
	if(!GBrowserIsCompatible()) { this.error=-1; return; }
	
	// Creazione oggetto GMap(2)
	this.map = new GMap2(document.getElementById(id));
	this.lat = __arg(lat, 42);
	this.lng = __arg(lng, 13);
	this.zoom = __arg(zoom, 5);
	this.map.setCenter(new GLatLng(this.lat,this.lng), this.zoom);
	this.map.savePosition();
	
	// Impostazione dei pulsanti di scelta del "tipo di mappa"
	mapTypes = __arg(mapTypes,0);
	this.map.addControl(new GMapTypeControl()); // aggiunge il controllo
	if(mapTypes>=0) this.map.addMapType(G_NORMAL_MAP);
	if(mapTypes>=1)	this.map.addMapType(G_SATELLITE_MAP);	else this.map.removeMapType(G_SATELLITE_MAP);
	if(mapTypes>=2)	this.map.addMapType(G_HYBRID_MAP);		else this.map.removeMapType(G_HYBRID_MAP);
	if(mapTypes>=3)	this.map.addMapType(G_PHYSICAL_MAP);	else this.map.removeMapType(G_PHYSICAL_MAP);
	
	//obj = this;
	//GEvent.addListener(this.map,"click",function() { alert(obj.map.getCenter()) });
	// args = this.set_MapTypes.arguments;

	// Aggiunta dei controlli per navigazione e zoom
	__createCtrl(this);
	if(this.ctrl_ArrowZoom==0)		this.map.addControl(new GLargeMapControl()); // Freccette e Zoom - controlli grandi
	if(this.ctrl_ArrowZoom==1)		this.map.addControl(new GSmallMapControl()); // Freccette - controlli piccoli
	if(this.ctrl_ArrowZoom==2)		this.map.addControl(new GSmallZoomControl()); // Zoom - controlli piccoli
	if(this.ctrl_Scale)				this.map.addControl(new GScaleControl()); // visualizza la scala della mappa
	if(this.ctrl_MapDrag) 			this.map.enableDragging(); else	this.map.disableDragging(); // abilita il trascinamento
	if(this.ctrl_InfoWindow) 		this.map.enableInfoWindow(); else this.map.disableInfoWindow();// abilita i quadri informazione
	if(this.ctrl_DbClickZoom)		this.map.enableDoubleClickZoom(); else this.map.disableDoubleClickZoom(); // abilita lo zoom con il doppio click
	if(this.ctrl_ContinueZoom) 		this.map.enableContinuousZoom(); else this.map.disableContinuousZoom(); // abilita lo zoom continuato
	if(this.ctrl_ScrollZoom) 		this.map.enableScrollWheelZoom(); else this.map.disableScrollWheelZoom(); // abilita lo zoom con la rotellina del mouse
	this.map.disableGoogleBar();	this.ctrl_searchBar = false;	// disabilita la barra di ricerca di Google
	this.searchBar = function(active) { if(active) this.map.enableGoogleBar(); else this.map.disableGoogleBar(); } // Abilita la barra di ricerca sulla mappa
	
	// Proprietà
	this.markers = new Array();
	this.icons = new Array();



	/****************************************************************************************************
	*   GET - Metodi di stato (da notare il prefisso "get_")											*
	****************************************************************************************************/
	
	// True se la mappa è stata caricata, False se la mappa non è stata caricata
	this.get_isLoaded = function() { return this.map.isLoaded(); }
	
	// Restituisce il punto centrale della mappa che si sta vedendo in quel momento
	this.get_Center = function() { return this.map.getCenter(); }



	/****************************************************************************************************
	*   SET - Metodi di variazione dello stato (da notare il prefisso "set_")							*
	****************************************************************************************************/
	
	// Modifica il punto centrale della mappa (senza salvarlo)
	this.set_Center = function(lat, lng, zoom) { this.map.setCenter(new GLatLng(lat,lng), zoom); }
	
	
	
	/****************************************************************************************************
	*   MARKER - Creazione, modifica, cancellazione	(da notare il prefisso "mk_"						*
	****************************************************************************************************/
	
	// Crea un marker, passando latitudine (lat) e longitudine (lng), testo (html) alternativo. (n non deve essere usato)
	this.mk_new = function(lat, lng, html, icon_id, n)
	{
		n = this.markers.length;		
		if(__arg(lat,0)==0 || __arg(lng,0)==0)	return null;	// Se sono null o indefinite, termina l'esecuzione
		if(__arg(icon_id,-1)==-1) ico=G_DEFAULT_ICON; else ico=this.icons[icon_id]; // Controlli icona
		this.map.addOverlay(this.markers[n] = new GMarker(new GLatLng(lat,lng),ico)); // Aggiunge il marker alla mappa
		if(__arg(html,"")!="")
		{
			obj = this;
			GEvent.addListener(obj.markers[n], "click", function() { obj.markers[n].openInfoWindowHtml(html); } );
		}
		alert(this.markers.length);
		return n;
	}
	
	// Aggiunge un'icona (path, width, height -> obbligatori)
	this.mk_addIco = function(path, width, height, anchorX, anchorY, infowin_anchorX, infowin_anchorY)
	{
		if(__arg(path,0)==0 || __arg(width,0)==0 || __arg(height,0)==0) return -1;
		anchorX = __arg(anchorX,width/2);
		anchorY = __arg(anchorY,height/2);
		infowin_anchorX = __arg(infowin_anchorX,width/2);
		infowin_anchorY = __arg(infowin_anchorY,height/2);
		id = this.icons.length;
		this.icons[id] = new GIcon();
		this.icons[id].image = path;
	    this.icons[id].iconSize = new GSize(width, height);
		this.icons[id].iconAnchor = new GPoint(anchorX, anchorY);
		this.icons[id].infoWindowAnchor = new GPoint(infowin_anchorX, infowin_anchorY);
		return id;
	}
	
	
	
	/****************************************************************************************************
	*   PRIVATE - Funzioni private (da notare il prefisso "__")											*
	****************************************************************************************************/

	// Crea le variabili dell'oggetto che hanno il prefisso "ctrl_"
	function __createCtrl(y, n)
	{
		y.ctrl_ArrowZoom 	= __arg(ctrl_ArrowZoom,0); if(y.ctrl_ArrowZoom<0 || y.ctrl_ArrowZoom>3) y.ctrl_ArrowZoom=0;
		y.ctrl_Scale 		= __arg(ctrl_Scale,true);
		y.ctrl_MapDrag 		= __arg(ctrl_MapDrag,true);
		y.ctrl_InfoWindow 	= __arg(ctrl_InfoWindow,true);
		y.ctrl_DbClickZoom 	= __arg(ctrl_DbClickZoom,true);
		y.ctrl_ContinueZoom = __arg(ctrl_ContinueZoom,true);
		y.ctrl_ScrollZoom 	= __arg(ctrl_ScrollZoom,true);
	}
	
	// Controlla che v non sia null o undefined - se lo è restituisce dv, altrimenti restituisce lo stesso valore della variabile
	function __arg(v, dv) { if(v==null || v==undefined)	return dv; else	return v; }
	
	// Ricerca un elemento in un array (ricerca binaria)
	function __arraySearch(array, e, start, total)
	{
		start = __arg(start,0);
		total = __arg(total,array.length-1);
		m = (start + total) / 2; // Determina l'elemento centrale
		
		if(m < start) { return -1; } // l'elemento cercato non c'è
		else if(e < array[m]) { return __arraySearch(array, e, start, m-1); } // Si ripete la ricerca nella parte inferiore
		else if (e > array[m]) { return __arraySearch(array, e, m+1, total); } // Si ripete la ricerca nella parte superiore
		else { return m; }  // m rappresenta l'indice dell'elemento cercato
	}
	
}