$(window).load(function () {
	//----------- Inizio Variabile e Funzioni----------
	//----------- Variabili----------
	var pathRoot="", path_xml='xml', pg_xml='pagina', ml_html="", anno_iniz=0, anno_fin=0, el2sort=0;
	//var ml_arr = { num:0, link:"", label:""}; //Non funziona con push
	var ml_arr = []; 
	//alert("lenght="+ml_arr.length);
	//----------- Funzioni----------
	function sortarray2d(nome_array2d,elem2sort){
		if(!elem2sort){el2sort=0;} else{el2sort=elem2sort;}
			//alert("a="+nome_array2d+" --b="+el2sort);
			// this sorts the array using the second element   
			nome_array2d.sort(sortMultiDimensional);
			el2sort=0; //riporta al valore di default
	}
	function sortMultiDimensional(a,b){
		// this sorts the array using the second element   
		return ((a[el2sort] < b[el2sort]) ? -1 : ((a[el2sort] > b[el2sort]) ? 1 : 0));
	}
	function rootLevel(dirRoot){
		var  dirRoot="", pStart=0, sLocal=false;
		//---------Verifica se sta funzionando in locale o in linea.---------
		//var myRegExp = /M:|ww.manuelagreco.com/;
		//pStart=location.href.search(myRegExp);
		if (location.hostname=="") {pStart=location.pathname.search("M:"); sLocal=true;} //search("MAG")
		//---------Fine Verifica---------------
		//---------Trova la profondità della pagina rispetto alla Root (risultato in dirRoot) ed la inserisce nella var pathRedirect per un corretto ritorno alla pagina-----
		//alert("pStart="+pStart+" location.hostname="+location.hostname);
		do {
			pStart = location.pathname.indexOf("/",(pStart)); // pStart+1-->OK, ++pStart-->OK, pStart++ -->Errore
			dirRoot = (pStart!=-1) ? dirRoot +="../" : dirRoot.slice(3); //slice(4) per eliminare un "/../" in più nella formula
			if (pStart!=-1) {pStart++;}
			//alert(pStart+" - "+dirRoot);
		}
		while (pStart!=-1)
		//------------------Fine trova in quante sottodirettori è dalla root la pagina corrente risultato in dirRoot-----------------------
		//alert(dirRoot);
		return (dirRoot)
	}
	//----------- Fine Variabile e Funzioni----------
	
	pathRoot= rootLevel();
	//alert("path= "+pathRoot+path_xml+'/'+pg_xml+'.xml');
	//----------- Inizio costruzione MenuLaterale  "ml"----------
	ml_html+= '<I><b>';
	$.ajax({ type: "GET", url: pathRoot+path_xml+'/'+pg_xml+'.xml', dataType: "xml", async:false,
	//$.ajax({ type: "GET", url: '../xml/pagina.xml', dataType: "xml", async:false,
		success: function(xml) {
			$(xml).find('link_menu').each(function() {
				ml_arr.push([$(this).find('pos_menu').text(), $(this).find('link').text(), $(this).find('label').text()]);
			});    
		},
		error: function(request, error, tipo_errore) { alert(error+': '+ tipo_errore); }
	});
	sortarray2d(ml_arr,0);
	for(var i=0;i<ml_arr.length;i++){
		//var subDir = ""
		var subDir = ml_arr[i][1].slice(0,4)=="http" ? "" : pathRoot;
		ml_html+= '<br><a href="'+subDir+ml_arr[i][1]+'">'+ml_arr[i][2]+'</a>';
		//document.write("ALL_arr =" + arr[i].toString()+"<br>");
	}
	ml_html+= '</I></b>';
	//alert(ml_html);
	$(ml_html).appendTo('#MenuLaterale');
	//----------- Fine costruzione MenuLaterale----------

});
