/*
	Auteur: Mercosuh
	Site: http://www.oxosphere.net/
	Note:  #{id_conteneur} {
		overflow: hidden;
	}
*/

BlocsRetractables = function()
{
	var blocCourant = null;
	
	this.initialiser = function(id)
	{
		var conteneur = document.getElementById(id);
		var titres = conteneur.getElementsByTagName('h2');
		var blocs = conteneur.getElementsByTagName('div');
		var visible = true;
		
		if(titres.length == blocs.length)
		{
			for(var i = 0; i < blocs.length; i++)
			{	
				var bloc = new Bloc();
				bloc.initialiser(titres[i], blocs[i], visible);
				
				if(i == 0)
				{
					blocCourant = bloc;
					titres[i].setAttribute('class', 'activer');
					visible = false;
				}
				
				attacher(titres[i], 'click', this.ouvrir(bloc));
			}
		}
	}

	this.ouvrir = function(bloc)
	{
		return function()
		{
			if(blocCourant != bloc)
			{
				blocCourant.actionner();
				bloc.actionner();
				
				blocCourant = bloc;
			}
		}
	}
}

Bloc = function()
{
	this.titre = null;
	this.element = null;
	this.timer = null;
	this.visible = true;
	this.hauteur = 0;
	this.increment = 0;
}

Bloc.prototype.initialiser = function(titre, element, visible)
{
	if(element)
	{
		this.titre = titre;
		this.hauteur = element.offsetHeight;
		this.element = element;
		this.visible = visible;
		
		if(this.visible)
			this.increment = this.hauteur;
		else
			element.style.height = '0px';
	}	
}

Bloc.prototype.actionner = function()
{
	if(this.element)
	{
		if(this.timer)
			window.clearInterval(this.timer);
		
		if(this.visible)
		{
			this.timer = window.setInterval(appelMethode(this, this.reduire), 20);
			this.titre.removeAttribute('class');
		}
		else
		{
			this.timer = window.setInterval(appelMethode(this, this.agrandir), 20);
			this.titre.setAttribute('class', 'activer');
		}
		
		this.visible = !this.visible;
	}
}

Bloc.prototype.reduire = function()
{
	this.increment -= 8;
	if(this.increment < 10)
	{
		this.element.style.height = "0px";
		window.clearInterval(this.timer);
	}
	else
		this.element.style.height = this.increment + 'px';
}

Bloc.prototype.agrandir = function()
{
	this.increment += 8;
	if(this.increment > this.hauteur - 10)
	{
		this.element.style.height = this.hauteur + "px";
		window.clearInterval(this.timer);
	}
	else
		this.element.style.height = this.increment + 'px';
}