//<![CDATA[
var map; // mappa... usata come globale per semplificare
var lastSelectedMarker;
//var mapCenter;
//var mapZoom;

// #########################################################   FUNZIONI   ###############
function putIcon (position, iconFile, width, height, infoTabs)
{
	var inerte = false;
	if (infoTabs.length > 0)
		inerte = true;
	if (iconFile)
	{
		var icon = new GIcon();
		icon.image 			  = iconFile;
		icon.iconSize 		  = new GSize(width, height);
		icon.iconAnchor 	  = new GPoint(width -2, height -2);
		icon.infoWindowAnchor = new GPoint(width -1, height -1);
		
		var marker = new GMarker(position, icon);//, inerte);
	}
	else
	{
		var marker = new GMarker(position);
	}
	if (inerte)
	{
		GEvent.addListener(marker, 'click',
			function()
			{
				marker.openInfoWindowTabsHtml(infoTabs);
				lastSelectedMarker = marker;
			}
		);
	}
	map.addOverlay(marker);
	return (marker);
}

function readAttribute(node, attr, defValue, numeric)
{
	var attribute = defValue; //null;
	if (node.getAttribute(attr) != null)
	{
		if (numeric == true)
			attribute = parseFloat(node.getAttribute(attr));
		else
			attribute = node.getAttribute(attr);
	}
	return (attribute);
}

// #################################################################################
function load(mapID, XMLfile, mapDragging, mapControl, mapType, mapOverview, centerLat, centerLng, zoomDef)
{
	if (GBrowserIsCompatible())
	{ 
		map = new GMap2(document.getElementById(mapID));//, {size: new GSize(700,500)});
		
		var centerPos = new GLatLng(centerLat, centerLng);
		//mapCenter = centerPos;
		//mapZoom = zoomDef;
		
		
		map.setCenter(centerPos, zoomDef);
		
		lastSelectedMarker = map.getCenter();
		map.savePosition();
		
		// ********************** SETUP CONTROLLI (BEGIN)
		// trascina mappa
		if (mapDragging == false)
			map.disableDragging();
		//versione controlli zoom 	
		if (mapControl  == 1) //piccoli
			map.addControl(new GSmallMapControl());
		if (mapControl  == 2) //grandi
			map.addControl(new GLargeMapControl());
		//visualizza controllo tipo mappa stradale aerea ibrida
		if (mapType     == 1)
			map.addControl(new GMapTypeControl());
		//visualizza il riquadro in basso a dx (quadro d'unione)
		if (mapOverview == 1 && mapDragging == true)
			map.addControl(new GOverviewMapControl());	
		
    
    // Legge File XML con segnalibri (coord, testo, icona, ecc..)
		GDownloadUrl(XMLfile, 
		function(data, responseCode)
			{
				var xml  = GXml.parse(data);
				var mapDoc  = xml.documentElement; //alert(xml);

				var places  = mapDoc.getElementsByTagName("place"); //crea elenco chiavi place da file xml
				for (var i=0; i<places.length; i++) // per ogni places trovato devo estrarre le informazioni relative
				{
					// estraggo le informazioni per le info
					var tabInfo = [];
					var pInfo = places[i].getElementsByTagName("info");
					var iTab;
					if (pInfo.length > 0)
					{
						// Legge nomi schede etichetta segnalibro (di soli ce n'è una sola)
						// Legge per ogni scheda Titolo e contenuto in formato html
						// Attenzione alla sinstassi html
            iTab = pInfo[0].getElementsByTagName("tab");
						for (var j=0; j<iTab.length; j++) // ciclo sui tab presenti per estrarne i dati
						{
							var caption = iTab[j].getElementsByTagName("caption");
							var content = iTab[j].getElementsByTagName("content");
							tabInfo.push(new GInfoWindowTab(caption[0].firstChild.nodeValue, content[0].firstChild.nodeValue)); // aggiungo il tab e le info relative
							
							var imx = caption[0].firstChild.nodeValue;
						}
					}
					// estraggo le informazioni per l'icona
					// Legge latitudine, longitudine, nome file, larghezza e altezza segnalibro (bandierina)
					var pIcon = places[i].getElementsByTagName("icon");
					for (var k=0; k<pIcon.length; k++)
					{
						var iLat = readAttribute(pIcon[k], "lat"   , false, true);
						var iLng = readAttribute(pIcon[k], "lng"   , false, true);
						var iSrc = readAttribute(pIcon[k], "image" , false, false);
						var iWid = readAttribute(pIcon[k], "width" , false, true);
						var iHei = readAttribute(pIcon[k], "height", false, true);
						//alert(iLat+" - "+iLng+" - "+iSrc+" - "+iWid+" - "+iHei);
						
            // Crea Segnalibro
            var place = new GLatLng(iLat, iLng);
						var myMarker = putIcon(place, iSrc, iWid, iHei, tabInfo);
					}
				} // end >>> for (var i=0; i<places.length; i++)
			}
		);
		
	} // end >>> if (GBrowserIsCompatible())
} // end >>> load()

function loaddefaults()
{
	load('div_map','http://www.ingegneririuniti.it/Scripts/sede_ir.xml', true,2,1,0, 44.650124,10.892644, 15);
//(mapID, XMLfile, mapDragging, mapControl, mapType, mapOverview, centerLat, centerLng, zoomDef)
}
//window.onloads.push(loaddefaults);	
//]]>

