/**
 * *******************************************************
 * FICHER CONTENANT LES FONCTIONS RESERVEES AU SITE PUBLIC
 * *******************************************************
 */

/**
 * Chargement du body : mise en page
 */
function loadingBody() {
	
	if (is_ie && !is_ie7up) {
		$("logo_titre").src = '/images/logotitre.gif';
		$("header-logo").src = '/images/logo.gif';
	}
	
	lightboxInit(new Array("FERMER", "first", "preview", "start slide-show", "stop slide-show", "next", "last", "quitter"));
	
	if ($("box-liste") && $("box-consoles")) {
		var h1 = parseInt($("box-liste").getStyle("height"));
		var h2 = parseInt($("box-consoles").getStyle("height"));
		var max = Math.max(h1, h2);
		$("box-liste").style.height = max + "px";
		$("box-consoles").style.height = max + "px";
	}
	
	if ($("les-plus") && $("les-moins")) {
		var h1 = parseInt($("les-plus").getStyle("height"));
		var h2 = parseInt($("les-moins").getStyle("height"));
		var max = Math.max(h1, h2);
		$("les-plus").style.height = max + "px";
		$("les-moins").style.height = max + "px";
	}

	// test, preview, ...
	if ($("box-article") && $("box-images")) {
		var h1 = parseInt($("box-article").getStyle("height"));
		var h2 = parseInt($("box-images").getStyle("height"));
		var max = Math.max(h1, h2);
		$("box-article").style.height = max + "px";
		$("box-images").style.height = max + "px";
	}
	
	// galeries
	if ($("box-liste") && $("box-galeries")) {
		var h1 = parseInt($("box-liste").getStyle("height"));
		var h2 = parseInt($("box-galeries").getStyle("height"));
		if ($("box-auteur")) {
			var h3 = 0
			+ parseInt($("box-auteur").getStyle("height"))
			+ parseInt($("box-auteur").getStyle("margin-top"))
			+ parseInt($("box-auteur").getStyle("margin-bottom"))
			;
			var max = Math.max(h1, h2 + h3);
			$("box-liste").style.height = max + "px";
			$("box-galeries").style.height = (max - h3) + "px";
		} else {
			var max = Math.max(h1, h2);
			$("box-liste").style.height = max + "px";
			$("box-galeries").style.height = max + "px";
		}
	}

	// flashgames
	if ($("box-liste") && $("box-categories")) {
		var h1 = parseInt($("box-liste").getStyle("height"));
		var h2 = parseInt($("box-categories").getStyle("height"));
		var max = Math.max(h1, h2);
		$("box-liste").style.height = max + "px";
		$("box-categories").style.height = max + "px";
	}

	// news
	if ($("box-article") && $("box-actu")) {
		var h1 = Math.max(parseInt($("box-article").getStyle("height")), 100);
		$("box-article").style.height = h1 + "px";
		$("box-actu").style.height = h1 + "px";
		h_fen["_news"] = parseInt(h1) - 30;
		$("scroller_deroulant_news").style.height = h_fen["_news"] + "px";
	}
	
	initScrollers();
	
	// login / logout
	if ($('user-area')) {
		var loader = document.createElement('div');
		loader.id = 'user-area-loader';
		loader.style.display = 'none';
		loader.style.opacity = '0.00';
		loader.style.filter = 'filter:alpha(opacity=0)';
		loader.innerHTML = '<img src="/images/loader.gif" alt="loading..." />';
		$('user-area').insertBefore(loader, $('user-area-inner'));
	}
	
	// login / logout
	if ($('user-area2')) {
		var loader = document.createElement('div');
		loader.id = 'user-area2-loader';
		loader.style.display = 'none';
		loader.style.opacity = '0.00';
		loader.style.filter = 'filter:alpha(opacity=0)';
		loader.innerHTML = '<img src="/images/loader.gif" alt="loading..." />';
		$('user-area2').insertBefore(loader, $('user-area2-inner'));
	}
	
	externalLinks();
	disableAutoComplete();

}

function mouseOverLien(obj) {
	obj.style.opacity = "1";
	obj.style.filter = "filter:alpha(opacity=100)";
}

function mouseOutLien(obj) {
	obj.style.opacity = "0.40";
	obj.style.filter = "filter:alpha(opacity=40)";
}

/**
 * Hover des images pour noter un jeu
 * 
 * @param note				note du visiteur
 */
function changeMyNote(note) {
	changeNote("myNote", note);
	
}

function changeNotePublic(note) {
	changeNote("allNote", note);
}

/**
 * Change la liste d'étoiles pour les notes
 * @param object			nom de l'objet contenant les notes
 * @param note				valeur de la note
 */
function changeNote(object, note) {
	var i = 0;
	for (i = 1; i <= 10; i++) {
		var obj = $(object + i);
		if (i <= note) {
			obj.src = '/images/notey.png';
		} else {
			obj.src = '/images/notex.png';
		}
	}
}

/**
 * Change l'image de note, pour la page des tests
 * @param object			nom de l'image note
 * @param note				valeur de la note
 * @return
 */
function changeImageNote(object, note) {
	$(object).title = note;
	$(object).alt = "note " + note;
	$(object).src = '/images/notes/'+note+'.gif';
}


/**
 * Réinitialise la note quand on quitte la zone de sélection
 */
function resetMyNote() {
	changeNote("myNote", $("myNote").value);
}

/**
 * Sauve la note d'un visiteur pour un type d'article
 * @param table_article		table contenant l'article à noter
 * @param id_article		numéro de l'article
 * @param note				note du visiteur		
 */
function saveMyNote(table_article, id_article, note) {
	if (note != $("myNote").value) {
		$("totalPublic").value = parseInt($("totalPublic").value) - parseInt($("myNote").value) + note;
		if ($("myNote").value == 0) {
			$("votesPublic").value = parseInt($("votesPublic").value) + 1;
		}
		$("myNote").value = note;
		if ($("publicNote1")) {
			changeNote("publicNote", parseInt($("totalPublic").value / $("votesPublic").value));
		}
		if ($("notePublic")) {
			changeImageNote("notePublic", parseInt($("totalPublic").value / $("votesPublic").value));
		}
			
		var sid = getVar('sid');
		new Ajax.Request('/ajax/notes.php'+(sid?'?sid='+sid:''), {
			method :'post',
			parameters: 'table_article='+table_article+'&id_article='+id_article+'&note='+note
		});
	}
}

/**
 * Ajoute la page courante aux favoris de l'utilisateur
 */
function ajouterFavori() {
	var sid = getVar('sid');
	new Ajax.Request('/ajax/favoris.php'+(sid?'?sid='+sid:''), {
		method :'post'
	});
}

/**
 * Supprime un favori de la liste de l'utilisateur
 * 
 * @param id				numéro du favori
 */
function supprimerFavori(id) {
	var sid = getVar('sid');
	new Ajax.Request('/ajax/favoris.php'+(sid?'?sid='+sid:''), {
		method :'post',
		parameters: {del: id}
	});
}

/**
 * Connecte l'utilisateur
 * @param form_id			identifiant du formulaire
 */
function userLogin(form_id) {
	progressiveAlpha('user-area'+form_id+'-loader', 0, 1);
	new Ajax.Request('/ajax/userarea.php', {
		method: 'post',
		parameters: {login: 'login', username: $("loginform").username.value, password: $("loginform").password.value, form_id: form_id}
	});
	return false;
}

/**
 * Déconnecte l'utilisateur
 * @param form_id			identifiant du formulaire
 */
function userLogout(form_id) {
	progressiveAlpha('user-area'+form_id+'-loader', 0, 1);
	new Ajax.Request('/ajax/userarea.php?logout=1&id='+form_id, {
		method: 'get'
	});
	return false;
}

/**
 * Active un bouton du mini-menu détail d'un article
 * @param id				numéro du bouton
 */
function activateMiniButton(id) {
	for (var i = 1; i <= 10; i ++) {
		if ($("minibouton"+i)) {
			$("minibouton"+i).className = 'minibouton-inactif';
			$("fichetech-layer"+i).hide();
		}
	}
	$("minibouton"+id).className = 'minibouton-actif';
	$("fichetech-layer"+id).show();
}

/**
 * Initialise un loader (cache / invisible au début)
 */
function initLoader(element) {
	var loader = $(element);
	if (loader) {
		loader.style.display = 'none';
		loader.style.opacity = '0.00';
		loader.style.filter = 'filter:alpha(opacity=0)';
		
	}
}


/**
 * Change le mois du calendrier des news
 * @param date				date du premier jour du nouveau mois
 */
function changeMonth(date) {
	progressiveAlpha('box-calendar-loader', 0, 1);
	new Ajax.Request('/ajax/news.php?date='+date, {
		method: 'get'
	});
}

/**
 * Ajoute un loader pour le calendrier des news
 */
function addCalendarLoader() {
	var loader = document.createElement('div');
	loader.id = 'box-calendar-loader';
	loader.style.display = 'none';
	loader.style.opacity = '0.00';
	loader.style.filter = 'filter:alpha(opacity=0)';
	loader.innerHTML = '<img src="/images/loader.gif" alt="loading..." />';
	$('news-calendar').insertBefore(loader, $('box-calendar'));
}

function clearAdvancedSearchForm() {
	$('search_all').value = '';
	$('search_exact').value = '';
	$('search_atleastone').value = '';
	$('search_none').value = '';
}