var buttonsPath = "css/picts/bouton/";
var buttonsContainer = "choixHome";
var contentsContainer = "imageHome";
var timeToWait = 4000;
var timerCarrousel = null;

/**
 * @return Tableau des boutons
 */
function getButtons() {
	var buttons = new Array();
	var child = document.getElementById(buttonsContainer).firstChild;
	var i = 0;
	
	while(child != null) {
		if((child.id != null) && (child.id.indexOf("choix") != -1)) {
			buttons[i++] = child;
		}
		
		child = child.nextSibling;
	}
	
	return buttons;
}

/**
 * @return Tableau des divs de contenu
 */
function getContents() {
	var contents = new Array();
	var child = document.getElementById(contentsContainer).firstChild;
	var i = 0;
	
	while(child != null) {
		if((child.id != null)
				&& (child.id.indexOf(contentsContainer) != -1)) {
			contents[i++] = child;
		}
		
		child = child.nextSibling;
	}
	
	return contents;
}

/**
 * Gestion des boutons on / off.
 * param index Position du bouton on
 */
function manageButtons(index) {
	var buttons = getButtons();
	var buttonOn = buttons[index - 1];
	
	for(var i = 0; i < buttons.length; i++) {
		if(buttonOn.id != buttons[i].id) {
			buttons[i].src = buttons[i].src.replace(/_on.gif/, "_off.gif");
		}
	}
	
	buttonOn.src = buttonOn.src.replace(/_off.gif/, "_on.gif");
}

/**
 * Gestion des contenus.
 * param index Position du contenu à afficher
 */
function manageContents(index) {
	var contents = getContents();
	var contentOn = contents[index - 1];
	
	for(var i = 0; i < contents.length; i++) {
		var content = contents[i];
		
		if(contentOn.id != content.id) {
			content.style.display = "none";
		}
	}
	
	contentOn.style.display = "block";
}

/**
 * Gestion de l'affichage du bouton et du contenu à afficher.
 * param index Position du bouton et du contenu à afficher
 */
function display(index) {
	clearTimeout(timerCarrousel);
	
	var newIndex = index + 1;
	
	if(newIndex > getButtons().length) {
		newIndex = 1;
	}
	
	manageButtons(index);
	manageContents(index);
	
	timerCarrousel = setTimeout(function() { display(newIndex) },
		timeToWait);
}
