// Recherche au haut de la page (toutes les pages)
var tOnglets = ["recents", "commentaires"];
var idActuel = 0;

$(document).ready(function(){
	$(".recherche").bind("focus", function(){
		if ($(".recherche").val() == "recherche" || $(".recherche").val() == ""){
			$(".recherche").val("");
		}
	})
	$(".recherche").bind("blur", function(){
		if ($(".recherche").val() == ""){
			$(".recherche").val("recherche");
		}
	})
	var lancerRecherche = function(){
		if ($(".recherche").val() == "recherche" || $(".recherche").val() == ""){
			$(".recherche").focus();
		} else {
			$("#form_recherche").submit();
		}
	}
	$("#recherche").bind("click", lancerRecherche);
	target_blank();
	
	var idAncien = 0;
	for (var i = 0; i < tOnglets.length; i++){
		$("#" + tOnglets[i]).bind("click", function(){
			if (idActuel != tOnglets.arrayIndexOf(this.id)){
				idAncien = idActuel;
				idActuel = tOnglets.arrayIndexOf(this.id);
				$("#" + tOnglets[idActuel]).removeClass().addClass(tOnglets[idActuel] + "-actif");
				$("#" + tOnglets[idAncien]).removeClass().addClass(tOnglets[idAncien]);
				if ($("#boite-" + tOnglets[idActuel]).height() <= $("#boite-" + tOnglets[idAncien]).height()){
					$("#boite-" + tOnglets[idActuel]).fadeIn(300, ajusterHauteur);
					$("#boite-" + tOnglets[idAncien]).fadeOut(500);
				} else {
					ajusterHauteur();
					$("#boite-" + tOnglets[idActuel]).fadeIn(300);
					$("#boite-" + tOnglets[idAncien]).fadeOut(300);
				}
			}
		})
	};
});

function ajusterHauteur(){
	$("#boite-onglets").animate({height:$("#boite-" + tOnglets[idActuel]).height() + 50 + "px"}, 175);
}


// Place l'objet en opacité, selecteur est un sélecteur jQuery et alpha doit être une décimale
function Alpha(selecteur, alpha){
	$(selecteur).css("opacity", alpha);
	$(selecteur).css("filter",  "alpha(opacity=" + (100 * alpha) + ")");
}

// Array.indexOf ne fonctionne pas sur IE, voici un moyen de parvenir au même but:
Array.prototype.arrayIndexOf = function(o){
for(var i = 0; i < this.length; i++)
	if (this[i] == o){
		return i;
	}
	return false;
}

// Target blank
function target_blank(){
if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
			anchor.target = "_blank";
		}
	}
}

function toggleText(id){
	$("#" + id).attr("title", $("#" + id).val());
	$("#" + id).bind("focus", function(){
		if ($(this).val() == $(this).attr("title")){
			$(this).val("");
		}
	})
	$("#" + id).bind("blur", function(){
		if ($(this).val() == ""){
			$(this).val($(this).attr("title"));
		}
	})
}
