// REQUIERT Prototype.js version 1.6+
// REQUIERT Tween.js


document.observe("dom:loaded", initPage);

function initPage () {
	// Paragraphes d'articles
	var paragraphes = $('paragraphes');
	if (paragraphes) 
		paragraphes.select('div.cadre').each(initParagraphe);

	// Paragraphes d'élements
	var elements = $('elements');
	if (elements) {
		elements.select ('div.paragraphes').each (initElementParagraphe);
	}
	
	document.stopObserving("dom:loaded", initPage);
}

function initElementParagraphe (id) {
	var element = $(id);
	if (element) {
		element.select('div.cadre').each(initParagraphe);
	}
}

function initParagraphe (id) {
	var element = $(id);
	if (element) {
		if (element.hasClassName ('o')) {
			// Ouvrir le paragraphe par défaut, afficher le bouton fermer
			openParagraphe (id);
		} else if (element.hasClassName ('f')) {
			// Fermer le paragraphe par défaut, afficher le bouton ouvrir
			element.down ('.contenu').style.height = '0px';
			closeParagraphe (id);
		} else if (element.down ('h5')) {
			// Laisser le paragraphe ouvert, ne pas afficher le bouton fermer
			// Dans ce cas on n'a pas besoin du lien sur le titre
			var titre = element.down ('h5').down('a').innerHTML;
			if (titre) {
				element.down ('h5').replace ('<h5>'+titre+'</h5>');
			}
		}
	}
}

function openCloseParagraphe (id) {
	var element = $(id);
	if (element) {
		// Si aucune des classes "f" ou "o" n'est spécifiée, le paragraphe doit rester ouvert.
		if (element.hasClassName ('o') || element.hasClassName ('f')) {
			var obj = element.down ('.contenu');
			if (obj.style.height == '0px') {
				openParagraphe (id);
			} else {
				closeParagraphe (id);
			}
		} 
	}
}

function openParagraphe (id) {
	var element = $(id);
	if (element) {
		// masquer le bouton ouvrir
		var plus = element.down ('.plus');
		if (plus) { plus.hide(); }
		// afficher le bouton fermer
		var moins = element.down ('.moins');
		if (moins) { moins.show(); }
		// Ouvrir le calque
		var obj = element.down ('.contenu');
		var h = obj.scrollHeight;
		if (obj.style.height == '0px') {
			sizeTween = new Tween (obj.style,'height',Tween.regularEaseInOut,0,h,0.5,'px');
			sizeTween.start();
		}
	}
}

function closeParagraphe (id) {
	var element = $(id);
	if (element) {
		// masquer le bouton ouvrir
		var plus = element.down ('.plus');
		if (plus) { plus.show(); }
		// afficher le bouton fermer
		var moins = element.down ('.moins');
		if (moins) { moins.hide(); }
		// Fermer le calque
		var obj = element.down ('.contenu');
		var h = obj.scrollHeight;
		if (obj.style.height != '0px') {
			sizeTween = new Tween (obj.style,'height',Tween.regularEaseInOut,h,0,0.7,'px');
			sizeTween.start();
		}
	}
}

function switchMode (newMode) {
	var element = $('paragraphes');
	if (element) {
		for (var i=1;i<4;i++) {
			if (element.hasClassName ('mode'+i)) {
				element.removeClassName ('mode'+i);
			}
		}
		element.addClassName (newMode);
	}
}