// Autore: Michael Leigeber
// http://javascript.html.it/script/vedi/5650/accordion-verticale-a-piu-livelli/
// Modificato da Oligoatria - 04/09/2010

onload=function() {
     document.getElementById('menuContainer').style.visibility='visible';
}

var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.accordion=function(){
	function slider(n){this.n=n; this.h=[]; this.c=[]}
	
	slider.prototype.init=function(t,e,m,o,k){
		var men=T$('menu');
		men.aperto=false; // all'inizio il menù è chiuso
		
		var a=T$(t), i=x=0; this.s=k||'', w=[], n=a.childNodes, l=n.length; this.m=m||false;
		var c;
		for(i;i<l;i++){if(n[i].nodeType!=3){w[x]=n[i]; x++}} this.l=x;
		for(i=0;i<this.l;i++){
			var v=w[i]; this.h[i]=h=T$$(e,v)[0];
			this.c[i]=T$$('div',v)[0];
			c = this.c[i];
			if (c!=undefined){ // il menù si apre solo se ce n'è uno, ovvero se c'è un div nel li
				h.linkup = true;
				h.onclick=new Function(this.n+'.pr(false,this, false)');
				if(o==i){h.className=this.s; c.style.height='auto'; c.d=1}else{c.style.height=0; c.d=-1}
			}else h.linkup = false;
		}
	};
	slider.prototype.pr=function(f,d, flash){
		// controllo se il menù deve rientrare
			var m=T$('menu');
			var toOpen = false;
			for(var i=0;i<this.l;i++){
				var h=this.h[i];
				
				if (h.linkup){
					var c=this.c[i], k=c.style.height; k=k=='auto'?1:parseInt(k);
					h.style.marginTop = "2px";
					h.style.marginBottom = "0px";
					h.style.color="#90918f";
					//marginTrans(h, 2, 0);
					if((k!=1&&c.d==-1)&&(f==1||h==d)){
						toOpen=true;
					}else if(k>0&&(f==-1||this.m||h==d)){
						;
					}
				}else{
					z = h.children[0];
					if (z != undefined){
						z.style.color="#90918f";
					}
				}
			}
			m.move = (toOpen!=m.aperto);
		

		for(var i=0;i<this.l;i++){
			var h=this.h[i];
			if (h.linkup){
				var c=this.c[i], k=c.style.height; k=k=='auto'?1:parseInt(k); clearInterval(c.t);
				if((k!=1&&c.d==-1)&&(f==1||h==d)){
					c.style.height=''; c.m=c.offsetHeight; c.style.height=k+'px'; c.d=1; h.className=this.s; su(c,flash);
				}else if(k>0&&(f==-1||this.m||h==d)){
					c.d=-1; h.className=''; su(c,flash);
				}
			}
		}

		if (toOpen){ // evidenzio il menù selezionato
			d.style.color="#000000";
			TINY.dSel = d;
			marginTrans(d, 10, 12, flash);
		}else{ // evidenzio la pagina attuale
			var j = window.menuSelection;
			if (this.h[j].linkup)
				this.h[j].style.color="#000000";
			else{
				z = this.h[j].children[0];
				if (z != undefined){
					z.style.color="#000000";
				}
			}
		}
	};
	
	function su(c,flash){
		// Controllo per l'apertura orizzontale
		var m=T$('menu');
		var mc=T$('menuContainer');
			if (m.move){
				if (c.d==-1) c.t=setInterval(function(){sl(c, m, flash)},10); // Apertura verticale
				else{ clearInterval(m.t); m.t=setInterval(function(){sv(m, mc, c, flash)},10);} // Apertura orizzontale
			}else c.t=setInterval(function(){sl(c, m, flash)},10); // transizione verticale
	};
	
	function sv(m, mc, c, flash){ // apertura orizzontale
		var val = m.offsetWidth+(Math.ceil((mc.offsetWidth-m.offsetWidth + 10)/5)*c.d);
		if (!flash) m.style.width=val+'px';
		else{
			if (c.d==1)
				m.style.width=mc.offsetWidth+'px'; // apro istantaneamente
			else
				m.style.width='220px';		 	 // chiudo istantaneamente
		}
		
		if (val >= mc.offsetWidth){ // aperto del tutto
			m.style.width=mc.offsetWidth+'px';
			//styleFloat  // x IE
			clearInterval(m.t);
			c.t=setInterval(function(){sl(c, m, flash)},10); // Apertura verticale
			m.aperto = true;
		}
		if (val <= 220){ // larghezza menù - solo per il sito in questione
			m.style.width='220px';
			clearInterval(m.t);
			m.aperto = false;
		}
	};
	
	function sl(c, m, flash){ // apertura verticale
		var h=c.offsetHeight, d=c.d==1?c.m-h:h; c.style.height=h+(Math.ceil(d/10)*c.d)+'px';
		if (flash){
			if (c.d==1) c.style.height = c.m+'px';
			else	  c.style.height = 0;
		}
		//c.style.opacity=h/c.m; c.style.filter='alpha(opacity='+h*100/c.m+')';
		if((c.d==1&&h>=c.m)||(c.d!=1&&h==1)){
			clearInterval(c.t);
			if(c.d==1){
				c.style.height='auto';
				if ((!flash)&&((String)(window.location).search(TINY.dSel.title)==-1)) window.location = TINY.dSel.title; // se ho aperto ma non istantaneamento onload
			}
			else if ((c.d == -1)&&(m.move)){
				var m=T$('menu');
				var mc=T$('menuContainer');
				clearInterval(m.t);
				m.t=setInterval(function(){sv(m, mc, c, flash)},10); // Apertura orizzontale
			}
		}
	};
	
	function marginTrans(h, top, bottom, flash){ // transizione nel menù principale (ref to <h3>, 0, 2)
		mt = parseInt(h.style.paddingTop);
		mb = parseInt(h.style.paddingBottom);
		if (isNaN(mt)) mt = 0;
		if (isNaN(mb)) mb = 0;
		if ((mt!=top)||(mb!=bottom)){
			if (!flash) h.tim = setInterval(function(){mTr(h, top, bottom)},10);
			else{
				h.style.marginTop =top+"px";
				h.style.marginBottom = bottom+"px";
			}
		}
	};
	function mTr(h, top, bottom){ // transizione nel menù principale (ref to <h3>, 0, 2)
	//alert(h.style.paddingTop);
		mt = parseInt(h.style.marginTop);
		mb = parseInt(h.style.marginBottom);
		if (isNaN(mt)) mt = 0;
		if (isNaN(mb)) mb = 0;
	//	alert(mt+","+top);
		if (top>mt) mt++;
		else if (top<mt) mt--;
		if (bottom>mb) mb++;
		else if (bottom<mb) mb--;
		h.style.marginTop = mt+"px";
		h.style.marginBottom = mb+"px";
		if ((mt==top)&&(mb==bottom)){
			clearInterval(h.tim);
		}
	};
	return{slider:slider}
}();

