/*
* Oggetto per la creazione di un accordion.
* Utilizzo tipico: var nomeAccordion = new jckAccordion(); jckAccordion.init('contenitore',velocita);
* @contenitore		elemento radice che contiene l'accordion
* @velocita		velocita dell'effetto di scorrimento, valori piu bassi implicano velocita piu alte.
* All'interno del contenitore dovranno trovarsi una serie di coppie di div header-contenuti, 
* i cui id dovranno essere del tipo:
* nome#-header
* nome#-content
* con # numero progressivo.
* Accodare "selezionato" nell'header dell'elemento che si vuole gia visibile all'avvio.
* Per un effetto migliore, I div di tipo content non devono avere margin o padding. Utilizzare eventualmente
* un secondo div all'interno di questi con le impostazioni di margin e padding.
*/

function jckAccordion() {
	this.timer=[];	
	this.radiceNome=[];
	this.listaEle=[];
	this.maxH=[];
}

jckAccordion.prototype = {
	s: 7,
	t: 10,	
	
	contraiDiv: function(indiceDiv){
		var	questo=this;
		var ilDiv = document.getElementById(questo.listaEle[indiceDiv]);
		if(parseInt(ilDiv.style.height)>0){
			var v = Math.round(parseInt(ilDiv.style.height)/questo.s);
			v = (v<1) ? 1 :v ;
			v = (parseInt(ilDiv.style.height)-v);
			v = (v<0) ? 0 :v ;
			ilDiv.style.height = v +'px';
			ilDiv.style.opacity = (v/questo.maxH[indiceDiv]);
			ilDiv.style.filter= 'alpha(opacity='+(v*100/questo.maxH[indiceDiv])+');';
		}else{
			ilDiv.style.height='0px';
			ilDiv.style.display='none';
			clearInterval(questo.timer[indiceDiv]);
		}
	},
		
	espandiDiv: function(indiceDiv){
		var	questo = this;
		var ilDiv = document.getElementById(questo.listaEle[indiceDiv]);
		if(parseInt(ilDiv.style.height)<questo.maxH[indiceDiv]){
			var v = Math.round((questo.maxH[indiceDiv]-parseInt(ilDiv.style.height))/questo.s);
			v = (v<1) ? 1 :v ;
			v = (parseInt(ilDiv.style.height)+v);
			ilDiv.style.height = v +'px';
			ilDiv.style.opacity = (v/questo.maxH[indiceDiv]);
			ilDiv.style.filter= 'alpha(opacity='+(v*100/questo.maxH[indiceDiv])+');';
		} else {
			ilDiv.style.height=questo.maxH[indiceDiv]+'px';
			clearInterval(questo.timer[indiceDiv]);
		}
	},
		
	contraiInit: function(indiceDiv){
		var	questo = this;
		var ilDiv = document.getElementById(questo.listaEle[indiceDiv]);
		clearInterval(questo.timer[indiceDiv]);
		questo.timer[indiceDiv]=setInterval(function(){questo.contraiDiv(indiceDiv);},questo.t);
	},
	
	espandiInit: function(indiceDiv){
		var	questo = this;
		var ilDiv = document.getElementById(questo.listaEle[indiceDiv]);
		clearInterval(questo.timer[indiceDiv]);
		ilDiv.style.display='block';
		ilDiv.style.height='0px';
		ilDiv.style.opacity = 0;
		ilDiv.style.filter= 'alpha(opacity=0);';
		questo.timer[indiceDiv]=setInterval(function(){questo.espandiDiv(indiceDiv);},questo.t);
	},
	
	init: function(nomeDiv,vel){	
		var	questo=this;
		var oldonload = window.onload;
		if (typeof window.onload != 'function'){
	    	window.onload = function() {
				questo.preparaCassetto(nomeDiv,vel);
			}
		} else {
			window.onload = function(){
				oldonload();
				questo.preparaCassetto(nomeDiv,vel);
			}
		}
	},
		
	preparaCassetto: function(nomeDiv,vel){
		var	questo=this;
		var listaDiv=document.getElementById(nomeDiv).getElementsByTagName('div');
		var divId;
		for(i=0;i<listaDiv.length;i++){
			divId=listaDiv[i].id;
			if(divId.substr(divId.indexOf('-')+1,divId.length)=='content'){
				questo.listaEle.push(divId);
				questo.radiceNome.push(divId.substr(0,divId.indexOf('-')));
			}
		}
		questo.s=(vel==undefined)? 7 : vel;
		var radiceNomeDiv;
		var divContent;
		var divHeader;
		var sel=null;
		for(i=0;i<listaDiv.length;i++){
			divId=listaDiv[i].id;
			if(divId.substr(divId.indexOf('-')+1,divId.length)=='header'){
				var radice=divId.substr(0,divId.indexOf('-'));
				divContent=document.getElementById(radice+'-content');
				questo.maxH.push(divContent.offsetHeight);
				divContent.style.display='none';
				divContent.style.overflow='hidden';
				divHeader=document.getElementById(divId);
				divHeader.onclick = function(){
					for(i=0;i<questo.listaEle.length;i++){
						if((questo.radiceNome[i]+'-header')==this.id){
							questo.espandiInit(i);
						}else{
							questo.contraiInit(i);
						}
					}
				}
				if(divHeader.className.match(/selezionato+/)!=undefined){ 
					sel=divHeader;
				}
			}
		}
		if(sel!=undefined){
			sel.onclick();
		}
	}
}

var listaNews=new jckAccordion();
listaNews.init("contenitore_accordion",5);