jQuery(function($){
	
	//Variables
	var blocs = $(".blocListe"),
	btns = $("#liste").find("a"),
	loader = $(document.createElement("img")).attr({ 
		alt: "Chargement en cours", 
		title: "Chargement en cours", 
		src: "images/ajax-loader.gif"
    });
	
	//Version accueil/une
	if(!clients){
		if(identifiant){
			var lienParDefaut = btns.eq(idTabJS[id]);
		}else{
			var lienParDefaut = btns.eq(rd);
		}
		//Construction du conteneur image
		$("#images").append($(document.createElement("div")) 
		.attr("class","image") 
		.append( 
			$(document.createElement("img")).attr({ 
			alt: "", 
			src: lienParDefaut.attr("href") 
		})));
		
		//image = balise IMG
		var image = $(".image").children("img");
		
		$("#informations").append($(document.createElement("p")));
		var commentaire = $("#informations").children("p");
		if(identifiant){
			commentaire.html(html_entity_decode(commentTabJS[idTabJS[id]]));
		}else{
			commentaire.html(html_entity_decode(commentTabJS[rd]));
		}
	
	//Version clients
	}else{
		//image par défaut de la rub
		$("#images").append($(document.createElement("div")) 
		.attr("class","image") 
		.append( 
			$(document.createElement("img")).attr({ 
			title: titreDefaut, 
			src: imgDefaut 
		})));
		var image = $(".image").children("img");
		//légende de l'image par défaut de la rub
		$("#informations").append($(document.createElement("p")));
		var commentaire = $("#informations").children("p");
		commentaire.html(html_entity_decode(commentDefaut));
	}
	
	//survol et sélection
	btns.click(function(e){
		var str = $(this).attr("id");
		var ref = str.substr(4);
		//Gestion de l'images
		e.preventDefault();
		var target = $(this).attr("href");
		if (image.attr("src") == target) return; 
		//image.attr("src",target);
		$(".image").html(loader); 
      	image.load(function(){ 
          $(".image").html($(this).fadeIn(250)); 
		  //$(".image").html($(this)); 
        })
		.attr({ 
			alt: $(this).text(), 
			title: $(this).text(), 
			src: target 
		})
		
		//Gestion des commentaires
		if(!clients){
			var str = $(this).attr("id");
			var lequel = str.substr(4);
			commentaire.html(html_entity_decode(commentTabJS[lequel]));
			
		}else{
			$("#informations").children().remove();
			$("#informations").append($(document.createElement("div")).attr("class","titreListeProjets"));
			$(".titreListeProjets").html("<b>PROJET(S) RÉALISÉ(S)</b>");
			$("#informations").append($(document.createElement("div")).attr("class","projets"));
			var i = 0;
			for(i;i<projetsTabJS[ref].length;i++){
			$(".projets").append($(document.createElement("div"))
			.attr("class","titreProjet")
				.append($(document.createElement("a"))
				.attr("href","http://www.jba.fr/"+rubProjetsTabJS[ref][i]+".php?ssrub="+ssrubProjetsTabJS[ref][i]+"&id="+idTabJS[ref][i])
				.html(html_entity_decode(projetsTabJS[ref][i]))));
			}
		}
		
		blocs.css({ "backgroundImage":"url(images/taquetBlanc.jpg)","background-repeat":"repeat-y","background-position":"left","cursor":"pointer" });
		blocs.removeClass("selected");
		btns.css({ "color":"#000000", "font-weight":"normal", "text-decoration":"none" });
		
		$(this).parents("div.blocListe").css({ "backgroundImage":"url(images/taquetRouge.jpg)","background-repeat":"repeat-y","background-position":"left","cursor":"default" });
		$(this).parents("div.blocListe").addClass("selected");
		$(this).css({ "color":"#CC3333", "font-weight":"normal", "text-decoration":"none" });
	});
	btns.mouseover(function () {
		$(this).parents("div.blocListe").css({ "backgroundImage":"url(images/taquetRouge.jpg)","background-repeat":"repeat-y","background-position":"left","cursor":"pointer" });
		$(this).css({ "color":"#CC3333", "font-weight":"normal", "text-decoration":"none" });
	});
	btns.mouseout(function () {
		if( $(this).parents("div.blocListe").hasClass("selected")){
			$(this).parents("div.blocListe").css({ "backgroundImage":"url(images/taquetRouge.jpg)","background-repeat":"repeat-y","background-position":"left","cursor":"pointer" });
			$(this).css({ "color":"#CC3333", "font-weight":"normal", "text-decoration":"none" });
		}else{
			$(this).parents("div.blocListe").css({ "backgroundImage":"url(images/taquetBlanc.jpg)","background-repeat":"repeat-y","background-position":"left","cursor":"pointer" });
			$(this).css({ "color":"#000000", "font-weight":"normal", "text-decoration":"none" });
		}
	});
	
	//Condition initial
	btns.css({ "color":"#000000", "font-weight":"normal", "text-decoration":"none" });
	if(!clients){
		$(".blocListe.selected").css({ "backgroundImage":"url(images/taquetRouge.jpg)","background-repeat":"repeat-y","background-position":"left","cursor":"pointer" });
		$(".blocListe.selected").find("a").css({ "color":"#CC3333", "font-weight":"normal", "text-decoration":"none" });
	}
	
});
