/* -------------------------------------------------------------------- *
 * -------------------- © 2005 Roberto Puzzanghera -------------------- *
 * ----------------------- roberto@wildzone.it ------------------------ *  
 * ----------- You can freely use and distribute this product --------- *
 * ------------ I would be glad if you advise me by e-mail ------------ *
 * ---------------------- if you decide to use it --------------------- *
 * -------------------------------------------------------------------- */

var apriSolo1 = true; // solo 1 gruppo (quello scelto) rimane aperto dopo il click
var menuCorto = true; // aprendo un gruppo si chiude autom. l'altro

/* calcolo la dim. del menu */
var dim=0;// dim. main menu
for (j=0; j<100; ++j) if (document.getElementById('sub'+j)!==null) ++dim;

var DOM2 = document.getElementById;
if (!DOM2) menuCorto = false;

var isOpen = new Array(); // dice se un certo menu è aperto o no
for (i=0; i<=dim-1; i++) isOpen[i]=false; // all'inizio il menu è chiuso

// puntatore mouse al passaggio sul menu principale
function getCursor() {
	return curStyle = (document.all && DOM2) ? "hand" : 
					  ((!document.all && DOM2) ? "pointer" :
					  "default");
}

// status è un n. binario, un 1 indica che un dato gruppo è aperto, uno 0 indica che esso è chiuso
function checkStatus(URL,w,main) {
	var status = "";
	// se clicco sulle frecce uso 'ai' per determinare quale gruppo aprire
	if (apriSolo1 && f['ai']!='vuoto' && !main) {
		var pz = URL.split('&ai='); pz=pz[1].toString().charAt(0);
		for (k=0; k<dim; k++) {
			if (pz==k) status += "1"; 
			else if (pz != k) status += "0"; 
		}
	}
	// se clicco sulla voce di menu uso l'indice derivante dalla voce scelta
	else if (apriSolo1 && f['ai']=='vuoto' && !main) {
		// uso 'ai' per determinare quale gruppo aprire
		for (k=0; k<dim; k++) {
			if (w==k) status += "1"; 
			else if (w != k) status += "0"; 
		}
	}
	// in caso voglia mantenere la conf di tutti i gruppi aperti
	else {
		for (k=0; k<dim; k++) {
			if (isOpen[k]) status += "1"; 
			else if (!isOpen[k]) status += "0"; 
		}
	}
	return status;
}

// aggiunge all'URL una query string che propaga le informazioni su come deve essere aperto il menù
function addConf(URL,w,main) {
	return URL+'&mc='+checkStatus(URL,w,main);
}

// apre/chiude
function expand(k) {
	if (!DOM2) return;
	/* chiudo tutto */
	if (menuCorto) {
		for (y=0; y<dim; y++) {
			if (y==k) {
				disp = (isOpen[y]) ? 'none' : 'block';
				document.getElementById('sub'+y).style.display = disp;
				isOpen[y] = !isOpen[y];
			}
			else {
				document.getElementById('sub'+y).style.display = 'none';
				isOpen[y] = false;
			}
		}
	}
	/****************/
	else {
		disp = (isOpen[k]) ? 'none' : 'block';
		document.getElementById('sub'+k).style.display = disp;
		isOpen[k] = !isOpen[k];
	}
}

// in caso di browser obsoleto il menu apparirà completamente aperto
if (DOM2) {
	for (k=0; k<dim; ++k) {
		isOpen[k]=true;
		expand(k);
	}
}

// dalla query string (variabile mc e ai) si ricava la configurzione del menu
var f = new Array();
f['mc']='vuoto'; f['ai']='vuoto';
for(var i=0, par=unescape(self.location.search.substring(1)).split("&"); i<par.length; i++) 
f[ ( par[i] = par[i].split("=") ) [0] ] = par[i][1];

if (f['mc']!='vuoto' && DOM2) for (q=0; q<dim; q++) if (f['mc'].charAt(q)==1) expand(q);
if (f['ai']!='vuoto' && DOM2) document.getElementById('subItem'+f['ai']).style.fontWeight='bold';
