// JavaScript Document

$(document).ready(function(){
						   
	//Formulaire de recherche
	$("#q").attr("value", "Recherche");
	$("#q").bind("focus", function(){
		if($("#q").attr("value") == "Recherche"){
			$("#q").attr("value", "");
		};
	});
	$("#q").bind("blur", function(){
		if($("#q").attr("value") == ""){
			$("#q").attr("value", "Recherche");
		};
	});
	
	//Gestion du menu de 4 catégories
	if($("#sidebar .categories ul").length > 0){
		$("#sidebar .categories li").each(function(){
			 if($(this).text()=="Accueil")$(this).remove()
		});		
		removeSup($("#sidebar .categories ul"), 4);
		$("#sidebar .categories li:eq(0)").addClass("brun");
		$("#sidebar .categories li:eq(2)").addClass("brun");
	}
	
	
	
	$(".post").each(function(i){
		//Problèmes Solutions
		var tabClass = $(this).attr("class").split(" ");
			
		for(var i =0; i<tabClass.length; i++){
			tabClass[i] = nettoyage(tabClass[i]);
		}
		
		$(this).attr("class", tabClass.join(" "));
		if($(this).hasClass("catproblmes")){
			$("#problems").append($(this));
			$("#problems").css("background", 'url("/themes/isee/img/probleme-solution.png") no-repeat scroll left top transparent');;
		}
		else{
			//Actualités
			if($(this).hasClass("catactualits")){
				$("#actus").append($(this));
				$(this).children(".read-it").children("a").html("");
				$(this).children(".read-it").html($(this).children(".read-it").children("a"));
				if($(".dc-category").length!=0){
					var tagTxt = $(this).find(".post-tags").text();
					var cat =  $("#cat").text();
					
	
					if(compare(tagTxt, cat) == true || tagTxt==""){
						$(this).addClass("in");
					}
					else{
						$(this).addClass("out");
					}
				}
			}
			else{
				//Réferences
				if($(this).hasClass("catrfrences")){
					//Gestion des tags en relation avec la catégorie
					if($(".dc-category").length!=0){
						var tagTxt = $(this).find(".post-tags").text();
						var cat =  $("#cat").text();
							
						if(compare(tagTxt, cat) == true || tagTxt==""){
							$(this).addClass("in");
						}
						else{
							$(this).addClass("out");
						}
					}
					if($(this).find("a").length>0){
						$(this).find("a").each(function(){
							if($(this).parent()[0].tagName.toUpperCase() == "LI"){
								$(this).text($(this).text()+" *");
								$(this).bind("click", function(){return false});
							}
						});
					}
					$("#ref #refcontent").append($(this));
				}
				else{				
					//Gestion de l'image de début de billet
					if($(this).children(".post-content").children(":eq(0)")){
						if($(this).children(".post-content").children(":eq(0)").children(":eq(0)")){
							var firstEl = $(this).children(".post-content").children(":eq(0)").children(":eq(0)");
							if(firstEl[0])
							if(firstEl[0].tagName.toUpperCase()=="IMG"){
								firstEl.addClass("imgDeBillet");
								$(this).children(".post-title").after(firstEl);
							}
						}
					}
					//Lettrine et incipit
					var pHtml = $(this).children(".post-content").children("p:eq(0)").html();
					if(pHtml != null){
						var lettrine = pHtml.substring(0,1);
						if(lettrine!="<"){
							pHtml = pHtml.substring(1);
							lettrine = '<span class="lettrine">'+lettrine+'</span>';
							pHtml = '<div class="incipit">'+lettrine+pHtml+'</div>';
							 $(this).children(".post-content").children("p:eq(0)").html(pHtml);
						}
					}
					//Gestion du gras de titre de billet
					if($(this).children(".post-title").text().indexOf("__")!=-1 || $(this).children(".post-title").text().indexOf("%%%")!=-1){
						var str1 = $(this).children(".post-title").text().replace("__","<strong>");
						str1 = str1.replace("__","</strong>");
						
						 var br = new RegExp("(%%%)", "g");
						 str1 = str1.replace(br	,"<br />");
						
						$(this).children(".post-title").html(str1);
						
						/*var tabPostTitle =$(this).children(".post-title").text().split("__");
						stTitle =$(this).children(".post-title").text().split("__");
						
						if(tabPostTitle.length==3)$(this).children(".post-title").html("<strong>"+tabPostTitle[1]+"</strong><br>"+tabPostTitle[2]);*/
					}
				}
			}
		 }
		 

		 
		 //Suppression des gras dans la navigation
		 var dblTT = new RegExp("(__)", "g");
		 if($("#navlinks").length>0)$("#navlinks").html($("#navlinks").html().replace(dblTT,""));
	});
	


	if($("#actus .post").length == 0){
		$("#actus").css("display", "none");
	}

	//Animation des problèmes ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	var v;
	if($("#vproblems").text()!=""){
		var vTemp = $("#vproblems").text().split(" ");
		vTemp = parseInt(vTemp[vTemp.length-1]);
		v = vTemp*1000;
		//alert(v);
	}
	else v=5000;

	
	if($("#problems .post").length == 0){
		$("#mask").css("display", "none");
		$("#problems").css("display", "none");
	}
	else{
		startProblems(v);
	
		var mask = document.createElement("div");
		mask.id = "mask";
		
		document.body.appendChild(mask);
		$("#problems").after($("#mask"));
		
		$("#mask").width($("#problems").width());
		$("#mask").height($("#problems").height());
		$("#mask").css("margin-top", $("#problems").height()*(-1)+"px");
		
		$("#mask").bind("mouseover", function(){
			clearInterval(animProblems);
	   });
		$("#mask").bind("mouseout", function(){
			clearInterval(animProblems);
			//fadeOutProb();
			startProblems(v);
	   });
		$("#mask").bind("click", function(){
			clearInterval(animProblems);
			fadeOutProb();
	   });
	}
	
	//Animation des références ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	if($("#vrefs").text()!=""){
		var vrefsTemp = $("#vrefs").text().split(" ");
		vr = parseInt(vrefsTemp[vrefsTemp.length-1]);
		//alert(vr);
	}
	else vr=4;
	
	if($("#refcontent .post").length == 0){
		$("#ref").css("display", "none");
	}
	else{
		$("#refcontent .post").each(function(){
			if($(this).css("display").indexOf("none") == -1)
			totalWidths+=$(this).width();
		});
											  
		startRefs(80);
		$("#refcontent").bind("mouseover", function(){
			clearInterval(animRefs);
	   });
		$("#refcontent").bind("mouseout", function(){
			clearInterval(animRefs);
			startRefs(80);
	   });
	}
	
	//Masquer Rapidité du déplacement des références  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
	$(".post p").each(function(){
		if($(this).text().indexOf("message de maintenance technique")!=-1)
			$(this).remove();
	});
	
	if($("#problems").css("display") == "none" && $("#actus").css("display") == "none"){
		$("#main").width( 964);
		$("#footer").width( 964);
		$("#sidebar .categories ul").width(928);
		$("#blogextra").width(943);
		$("#footer #ref").width(964);
	};
	
	$("#navlinks a").each(function(i){
		if($(this).html().indexOf("__")!=-1 || $(this).html().indexOf("%%%")!=-1){
			var str1 = $(this).html().replace("__","");
			str1 = str1.replace("__","");
			
			 var br = new RegExp("(%%%)", "g");
			 str1 = str1.replace(br	,"");
			
			$(this).html(str1);
		}
	});

	$("#top h3").each(function(i){
		if($(this).html().indexOf("__")!=-1 || $(this).html().indexOf("%%%")!=-1){
			var str1 = $(this).html().replace("__","");
			str1 = str1.replace("__","");
			
			 var br = new RegExp("(%%%)", "g");
			 str1 = str1.replace(br	,"");
			
			$(this).html(str1);
		}
	});
	
	//Gestion du mail
	$(".email").each(function(){
		if($(this).text().indexOf(" arobase ")!=-1){
			var newText = $(this).text().replace(" arobase ", "@");
			$(this).text(newText);
			$(this).html("<a href='mailto:"+$(this).text()+"'>"+$(this).text()+"</a>");
		}
	});

	
});
//Comparaison de deux chaînes
function compare(s1, s2){
	var s1n = nettoyage(s1);
	var s2n =  nettoyage(s2);
	

	if(s1n==s2n){
		return true
	}
	else{
		return false;
	}
}

function nettoyage(chaine){
	ch = chaine.toLowerCase(); 
	var nettoyage = /\W|_/gi; //supprimer les - . et ; 
	ch = ch.replace(nettoyage,"");
	return ch;
}

//Fonctions d'animation des références
var vr;
var totalWidths = 0;
	

function startRefs(t){
	animRefs = setInterval(function(){ moveRefs(); }, t);
}
function moveRefs(){
	var n = parseInt($("#footer #refcontent").css("margin-left"));
	
	//console.log(totalWidths+" / "+Math.abs(n));
	
	if(totalWidths > Math.abs(n)){
		var nvr = n-vr;
		$("#footer #refcontent").css("margin-left", nvr+"px");
	}
	else
		$("#footer #refcontent").css("margin-left", 0);
}


//Fonctions d'animation des problèmes/solution
var animProblems;
function startProblems(t){
	animProblems = setInterval(function(){ fadeOutProb(); }, t);
}
function fadeOutProb(){
	$("#problems .post:eq(0)").fadeOut('fast', function (){replaceProb()});
}
function replaceProb(){
	$("#problems").append($("#problems .post:eq(0)"));
	$("#problems .post:eq(0)").fadeIn('fast');
}

//Fonction de gestion du menu
function removeSup(tab, n){
	if(tab.children("li").length > n){
		tab.children("li:last-child").remove();
		removeSup(tab, n);
	}
}
