
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function toggle(el) {
	if (el != null) {
		if (typeof element == 'string')
			var el = $(el);
		if ( el.style.display != 'none' ) {
			el.style.display = 'none';
		}
		else {
			el.style.display = '';
		}
	}
}

function preprocess() {
	var root = $('diaporama');
	
	for (i=0; i<root.childNodes.length; i++) {
		node = root.childNodes[i];
		if (node.nodeName=="DL") {
			total++;
			node.id = 'diapo_' + total;
			toggle(node);
		}
	}
	toggle($('diapo_1'));
	root.onmouseover=function() {
		stopDiaporama();
	}
	root.onmouseout=function() {
		launchDiaporama();
	}
	$('btn_previous').onclick=function() {
		nextDiapo('previous');
		return false;
	}
	$('btn_next').onclick=function() {
		nextDiapo();
		return false;
	}
}

function preprocess_bis() {
	var root = $('diaporama_bis');
	
	for (i=0; i<root.childNodes.length; i++) {
		node = root.childNodes[i];
		if (node.nodeName=="DL") {
			total_bis++;
			node.id = 'diapo_bis_' + total_bis;
			toggle(node);
		}
	}
	toggle($('diapo_bis_1'));
	root.onmouseover=function() {
		stopDiaporama_bis();
	}
	root.onmouseout=function() {
		launchDiaporama_bis();
	}
	$('btn_previous_bis').onclick=function() {
		nextDiapo_bis('previous');
		return false;
	}
	$('btn_next_bis').onclick=function() {
		nextDiapo_bis();
		return false;
	}
}

function nextDiapo(order) {
	toggle($('diapo_' + currentIndex));
	if (order == 'previous') {
		currentIndex = currentIndex -1;
	} else {
		currentIndex++;
	}
	if (currentIndex>total) {
		currentIndex = 1;
	}
	if (currentIndex<1) {
		currentIndex = total;
	}
	toggle($('diapo_' + currentIndex));
	fadeIn(5);
}

function nextDiapo_bis(order) {
	toggle($('diapo_bis_' + currentIndex_bis));
	if (order == 'previous') {
		currentIndex_bis = currentIndex_bis -1;
	} else {
		currentIndex_bis++;
	}
	if (currentIndex_bis>total_bis) {
		currentIndex_bis = 1;
	}
	if (currentIndex_bis<1) {
		currentIndex_bis = total_bis;
	}
	toggle($('diapo_bis_' + currentIndex_bis));
	fadeIn_bis(5);
}

function launchDiaporama() {
	clearInterval(idInterval);
	idInterval = setInterval('nextDiapo()', 1000*4);
}

function launchDiaporama_bis() {
	clearInterval(idInterval_bis);
	idInterval_bis = setInterval('nextDiapo_bis()', 1500*4);
}

function stopDiaporama() {
	clearInterval(idInterval);
}

function stopDiaporama_bis() {
	clearInterval(idInterval_bis);
}

function fadeIn(opacity) {
	var currentImage = $('diapo_' + currentIndex);
	if (opacity < 100) {
		fader(currentImage,opacity);
		opacity += 7;
		window.setTimeout("fadeIn("+opacity+")", 30);
	}
}

function fadeIn_bis(opacity) {
	var currentImage = $('diapo_bis_' + currentIndex_bis);
	if (opacity < 100) {
		fader_bis(currentImage,opacity);
		opacity += 7;
		window.setTimeout("fadeIn_bis("+opacity+")", 30);
	}
}

function fader(imageNumber,opacity) {
	var obj = $('diapo_' + currentIndex);
	
	if (obj == null) {
		return;
	}
	
	if (obj.style) {
		if (obj.style.MozOpacity!=null) {  
			obj.style.MozOpacity = (opacity/100) - .001;
		} else if (obj.style.opacity!=null) {
			obj.style.opacity = (opacity/100) - .001;
		} else if (obj.style.filter!=null) {
			/* set hasLayout to true within the css (necessary to change alpha) */
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}

function fader_bis(imageNumber,opacity) {
	var obj = $('diapo_bis_' + currentIndex_bis);

	if (obj == null) {
		return;
	}
	
	if (obj.style) {
		if (obj.style.MozOpacity!=null) {  
			obj.style.MozOpacity = (opacity/100) - .001;
		} else if (obj.style.opacity!=null) {
			obj.style.opacity = (opacity/100) - .001;
		} else if (obj.style.filter!=null) {
			/* set hasLayout to true within the css (necessary to change alpha) */
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}

var total = 0;
var total_bis = 0;
var idInterval = null;
var idInterval_bis = null;
var currentIndex = 1;
var currentIndex_bis = 1;

addLoadEvent(function() {
	if ($('diaporama')) {
		preprocess();
		launchDiaporama();
	}
	
	if ($('diaporama_bis')) {
		preprocess_bis();
		launchDiaporama_bis();
	}
});