
/*
* Variable 'S' defines the speed of the accordian
* Variable 'T' defines the refresh rate of the accordian
*/
s=5;
t=20;
// Prototype Method to get the element based on ID
function $(d){
	return document.getElementById(d);
}

// set or get the current display style of the div
function dsp(d,v){
	if(v==undefined){
		return d.style.display;
	}else{
		d.style.display=v;
	}
}

// set or get the height of a div.
function sh(d,v){
	// if you are getting the height then display must be block to return the absolute height
	if(d != undefined) {
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetHeight;
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetHeight);
		dsp(d,o);
		d.style.visibility = viz;
		//alert(r)
		return r;
	}else{
		d.style.height=v;
	}
	}
}


//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(d){
	d = $(d);
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		//d.style.opacity = (v/d.maxh);
		//d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
}


function ctm(d){
	d = $(d);
	//sh(d, d.maxh+'px');
	//clearInterval(d.t);
	if(sh(d)>(d.maxh+1)){
		v = Math.round(sh(d)/d.s/2);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		//d.style.opacity = (v/d.maxh);
		//d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		clearInterval(d.t);
		sh(d,d.maxh+'px');
		//dsp(d,'none');
		
	}
}


//Expand Timer is triggered as a setInterval to increase the height of the div exponentially.
function et(d){
	d = $(d);
	
	if(sh(d)<d.maxh){
		v = Math.round((d.maxh-sh(d))/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)+v);
		sh(d,v+'px');
		//d.style.opacity = (v/d.maxh);
		//d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,d.maxh);
		clearInterval(d.t);
	}
}

// Collapse Initializer
function cl(d){
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.id+'")',t);
	}
}


// Collapse Initializer
function clm(d){
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ctm("'+d.id+'")',t);
	}
}


//Expand Initializer
function ex(d){
	//if(dsp(d)=='none'){
		dsp(d,'block');
	//d.style.height='0px';
		clearInterval(d.t);
		d.t=setInterval('et("'+d.id+'")',t);
	//}
}

// Removes Classname from the given div.
function cc(n,v){
	s=n.className.split(/\s+/);
	for(p=0;p<s.length;p++){
		if(s[p]==v+n.tc){
			s.splice(p,1);
			n.className=s.join(' ');
			break;
		}
	}
}
//Accordian Initializer
function Accordian(d,s,tc){
	
	if($(d)==null) return
	// get all the elements that have id as content
	ul_list=$(d).getElementsByTagName('ul');
	l=$(d).getElementsByTagName('li');
	head=[];
	c=[];
	
	/*content primo livello*/
	/*-----------------------------------*/
	for(var i=0;i<ul_list.length;i++){
		h=ul_list[i].id;
		if(h.substr(h.indexOf('-')+1, h.length)=='content'){
			c.push(h);
		}
	}
	
	sel=null;	
	/*comportamento primo livello*/
	/*-----------------------------------*/
	for(var i=0;i<l.length;i++){
		h=l[i].id;
		
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			d=$(h.substr(0,h.indexOf('-'))+'-content');
			d.maxh =0;
			d.s=(s==undefined)? 7 : s;
			h=$(h);
			h.tc=tc;
			h.c=c;
			var parent_ul_id=d.id;
			var ul2_list=h.getElementsByTagName('li');
			var llist=[];
			
			for(var ll=0;ll<ul2_list.length;ll++){
				var s_li=ul2_list[ll];
				if(parent_ul_id == s_li.parentNode.id) {
					llist.push(s_li)
				}
			}
			
			d.maxh = calculateHeightLevel(llist);
			if(c.length==1) {
				d.style.display='block';
				d.style.overflow='hidden';
				d.style.height=calculateHeightLevel(llist)+'px';
			} else {
				d.style.display='none';
				d.style.overflow='hidden';
				d.style.height='0px';
			}
		d.style.visibility='visible';
		//calcolo altezza primo livello
		
		
		for(var ccp=0;ccp<llist.length;ccp++){
			var si=llist[ccp];
			si.cr=si.getElementsByTagName('div')[0];
			si.hd=si.getElementsByTagName('h4')[0];
			si.ho=d
			si.he=h
			si.s2l=llist;
			si.cr.style.display='none';
			//click secondo livello
			si.onclick =  function(){
				if(dsp(this.cr)=='none' ) {
					this.cr.style.display='block';
					this.hd.style.fontWeight='bold';
					this.ho.maxh=calculateHeightLevel(this.s2l)
					moveFirstLevel(this.he,'open')
				} else {
							//moveSecondLevel(this,'close')	
					this.hd.style.fontWeight='normal';
					this.cr.style.display='none';	
					this.ho.maxh=calculateHeightLevel(this.s2l)
					moveFirstLevel(this.he,'close')	
				}
			}
		}
		
		
		
		//-----------------------------------------------------------------
		// set the onclick function for each header.
		// primo livello solo se ho pił blocchi
		//-----------------------------------------------------------------
			
				if(c.length>1) {
					h3h=h.getElementsByTagName('h3')[0];
					h3h.ht=h
					h3h.onclick = function(){
						moveFirstLevel(this.ht,undefined)
					}
				}
			
			if(h.className.match(/selected+/)!=undefined){ sel=h;}
		}
	}
	if(sel!=undefined){sel.onclick();}
}


/*calcolo altezza in base ai div aperti*/
function calculateHeightLevel(list) {
	var hh=0
		for(var ccp=0; ccp<list.length;ccp++){
			var hhd=list[ccp].getElementsByTagName('h4')[0]
			list[ccp].style.display='block'
			hhd.style.display='block'
			hh +=sh(hhd);
			var dv=list[ccp].getElementsByTagName('div')[0];
			if(dsp(dv)=='block' ) {hh+=sh(dv);}
		}
	return hh;
}


function closeSecondLevel(lev) {
	var p_list=lev.getElementsByTagName('div');
		for(var ccp=0;ccp<p_list.length;ccp++){
				var pl=p_list[ccp];
				pl.style.display='none';
				pl.style.overflow='hidden';
		}
}


/*primo livello  apertura chiusura*/
function openSingleItem(obj) {
		ex($(obj+'-content'));
		switchClassOpen($(obj+'-header'))
		//alert($(obj+'-content').offsetHeight)
		//n=$(obj+'-header');
}

function closeSingleItem(obj) {
	cl($(obj+'-content'));
	cc($(obj+'-header'),'');
	switchClassClose($(obj+'-header'))
}


function moveSingleItem(obj) {
	clm($(obj+'-content'));
	//cc($(obj+'-header'),'');
}

/*scambia le classi primo livello*/

function switchClassOpen(n){
		if(c.length>1) {
			n.getElementsByTagName('h3')[0].className='classFaqOpen';
		}
}
function switchClassClose(n){
			if(c.length>1) {n.getElementsByTagName('h3')[0].className='classFaqClose';}
}


function moveFirstLevel(obj,fromSecLev) {

	for(var i=0;i<obj.c.length;i++){
					cn=obj.c[i];
					n=cn.substr(0,cn.indexOf('-'));
					if((n+'-header')==obj.id){
						/*apri*/
						if(dsp($(cn))=='none') {
							openSingleItem(n)
						} else 	if(fromSecLev=='open') {
							// moveSingleItem(n)
							openSingleItem(n)
						} else 	if(fromSecLev=='close') {	
						 moveSingleItem(n)
						}  else  {
							/*chiudi*/
							if(fromSecLev) {
							
							} else {
							closeSingleItem(n)	
							}
						}
					} else{
			closeSingleItem(n)
		}
	}
	
}

function moveSecondLevel(obj,fromSecLev) {
	for(var i=0;i<obj.s2l.length-1;i++){
		alert(obj.s2l[i]==obj)
	}
}
