var ajax = {};
var loadFunctions = new Array();
var topTimer;

// Stocker les scripts à lancer au chargement
function addLoadFunction(Obj) { 
	loadFunctions[loadFunctions.length] = Obj; 
}

// Execution des scripts au chargement de la page 
window.onload = function() { 
	for(i = 0; i < loadFunctions.length; i++) 
		loadFunctions[i]();
}

// Afficher un article et masquer les autres éventuellement
var tabArticles = new Array();
function showArticle(article_id) {
	var inArray = false;
	for(key in tabArticles) {
		document.getElementById("article_" + tabArticles[key]).style.display = "none";

		if(tabArticles[key] == article_id)
			inArray = true;
	}

	if(!inArray)
		tabArticles.push(article_id);

	document.getElementById("article_" + article_id).style.display = "block";
}

// Afficher les sources
function showSources() {
	document.getElementById("plugin_more_sources").style.display = "none";
	document.getElementById("plugin_other_sources").style.display = "block";
}

// Arrêter le défilement
function stopTop(i) {
	showTop(i);
	clearInterval(topTimer);
}

// Afficher l'actu du top
function showTop(i) {
	for(var j = 1; j <= 5; j++) {
		document.getElementById("top_actu" + j).style.display = "none";
		document.getElementById("top_num" + j).className = "";
	}

	document.getElementById("top_actu" + i).style.display = "block";
	document.getElementById("top_num" + i).className = "selected";
}

// Afficher la sauvegarde Mon Actu
var tabSaves = new Array();
function showSave(save_id) {
	var inArray = false;
	for(key in tabSaves) {
		document.getElementById("save_" + tabSaves[key]).style.display = "none";

		if(tabSaves[key] == save_id)
			inArray = true;
	}

	if(!inArray)
		tabSaves.push(save_id);

	document.getElementById("save_" + save_id).style.display = "block";
}

var actu = 1;
function topDiaporama() {
	actu++;

	if(actu > 5)
		actu = 1;

	showTop(actu);
}

var friendEmail = true;
function onLoadSearch() {
	if(document.getElementById('friend_email')) {
		document.getElementById('friend_email').onmousedown = function() {
			if(friendEmail) {
				document.getElementById('friend_email').style.color = '#000000';
				document.getElementById('friend_email').value = '';
				document.getElementById('friend_email').focus();
	
				friendEmail = false;
			}
		}
	}

	if(document.getElementById('top5')) {
		showTop(1);
		topTimer = window.setInterval(topDiaporama, 5000);
	}

	// Capturer les saisies au clavier pour le cas de la tabulation
	document.onkeydown = onKeyDownHandler;

	// Création des recherches AJAX
	new ajax.search(document.getElementById('search_form'), document.getElementById('search_words'), document.getElementById('submit_search'));
}

addLoadFunction(onLoadSearch);
