jQuery.noConflict();
(function($) {

$(document).ready(function(){
	

jQuery.initContenu = function() {
	ficheEtudiant();
	ficheIntervenant();
	offreEmploisStage();
	travauxEtudiants();
	galerieSensoriel();
	ratingSystem();
	initializeGoogleMap();
}


/* -------------------- Fiches étudiants -------------------- */

function ficheEtudiant() {
	$(".details_etudiant").hide();
	
	$("a.etudiant_details").click(function(){
		// id de l'étudiant
		var idEtudiant = $(this).attr('rel');
		// on masque la fiche
		$(".details_etudiant div.infos, .details_etudiant div.etudiant_photo").fadeOut("fast");
		// récupération des infos
		$.getJSON("assets/snippets/contenu/variables.etudiant.php", { idEtudiant: idEtudiant },
			function(data){
				var etudiant = new Array;
				$.each(data, function(){
					etudiant.push(this);
				});
				// variables de l'étudiant
				var nom = etudiant[0].toString();
				var prenom = etudiant[1].toString();
				var profil = etudiant[2].toString();
				var description = etudiant[3].toString();
				var photo = etudiant[4].toString();
				var email = etudiant[5].toString();
				var site = etudiant[6].toString();
				// on remplis a fiche de détail
				$(".details_etudiant div.infos span:eq(0)").html( prenom );
				$(".details_etudiant div.infos span:eq(1)").html( nom );
				$(".details_etudiant div.infos span.etudiant_profil").html( profil );
				$(".details_etudiant div.infos p").html( description );
				$(".details_etudiant div.etudiant_photo img").attr('src', photo );
				$(".details_etudiant div.infos a:eq(0)").html( email ).attr('href', email ).attr('title', email );
				$(".details_etudiant div.infos a:eq(1)").html( site ).attr('href', site ).attr('title', site );
				// on affiche la fiche de l'étudiant
				$(".details_etudiant div.infos, .details_etudiant div.etudiant_photo").fadeIn("slow");
				$(".details_etudiant").slideDown("slow");
		  	}
		);
		return false;
	});
}


/* -------------------- Fiches intervenants -------------------- */

function ficheIntervenant() {
	$(".details_intervenant").hide();
	
	$("a.intervenant_details").click(function(){
		// id de l'intervenant
		var idIntervenant = $(this).attr('rel');
		// on masque la fiche
		$(".details_intervenant div.infos, .details_intervenant div.intervenant_photo").fadeOut("fast");
		// récupération des infos
		$.getJSON("assets/snippets/contenu/variables.intervenant.php", { idIntervenant: idIntervenant },
			function(data){
				var intervenant = new Array;
				$.each(data, function(){
					intervenant.push(this);
				});
				// variables de l'étudiant
				var nom = intervenant[0].toString();
				var prenom = intervenant[1].toString();
				var profil = intervenant[2].toString();
				var description = intervenant[3].toString();
				var photo = intervenant[4].toString();
				var email = intervenant[5].toString();
				var site = intervenant[6].toString();
				// on remplis a fiche de détail
				$(".details_intervenant div.infos span:eq(0)").html( prenom );
				$(".details_intervenant div.infos span:eq(1)").html( nom );
				$(".details_intervenant div.infos span.intervenant_profil").html( profil );
				$(".details_intervenant div.infos p").html( description );
				$(".details_intervenant div.intervenant_photo img").attr('src', photo );
				$(".details_intervenant div.infos a:eq(0)").html( email ).attr('href', email ).attr('title', email );
				$(".details_intervenant div.infos a:eq(1)").html( site ).attr('href', site ).attr('title', site );
				// on affiche la fiche de l'étudiant
				$(".details_intervenant div.infos, .details_intervenant div.intervenant_photo").fadeIn("slow");
				$(".details_intervenant").slideDown("slow");
		  	}
		);
		return false;
	});
}


/* -------------------- Offre d'emplois -------------------- */

function offreEmploisStage() {
	$("a.offre_link").click(function(){
		// id de l'offre
		var idOffre = $(this).attr('rel');
		// récupération des infos
		$.get("assets/snippets/contenu/variables.offresemplois.php", { idOffre: idOffre },
			function(data){
				$("#template").html(data);
				// application du template de la rubrique
				$("#template > * , #contenu_page_left > * , .news > * , .contenuNews > * , .date > * , #contenu_page_right > *").not($(".btn_transfo")).css("color","#3B3B29")
				$("#template a").not($(".btn_transfo")).css("color","#93941B");
				$(".lien_puce").attr("src","assets/images/fleche_vert.gif");
				$("a.btn_transfo").css("background","transparent url(assets/images/btn_transfo_vert.jpg) no-repeat");
				// rollover sur les liens
				$("#template a:hover").not($(".btn_transfo")).mouseover(function(){
					$(this).css("color","#000000");
				}).mouseout(function(){
					$(this).css("color","#93941B");
				});
			}
		);
	});
	
	$("a.offre_tri").click(function(){
		var filtre = $(this).attr('rel');
		$(".news").each(function (i) {
			if( $(this).hasClass(filtre) || filtre == '' ) {
				$(this).show();
			}
			else $(this).hide();
		});
		return false;
	});
}



/* -------------------- Travaux d'étudiants -------------------- */

function travauxEtudiants() {
	var liste_matieres = $(".liste_matieres");
	// dimentionnement /!\ important /!\
	var nbMatieres = $(".nbMatieres").attr("rel"); // nb de fiches matière
	var marginRight = (nbMatieres - 1) * -870;
	liste_matieres.css('margin-right',marginRight+'px');
	
	// bouton suivant
	$("a.btn_suivant").click(function(){
		$(".liste_travaux, .detail_travaux").slideUp("normal");
		var posX = liste_matieres.css('margin-left');
		posX = parseInt( posX.substring(0, posX.lastIndexOf('px')) );
		
		var newPosX = (Math.floor(posX / 870) * 870) - 870;
		//console.log( '('+ Math.floor(posX / 870) +' * 870) - 870 = '+ newPosX );
		
		if( newPosX >= ((nbMatieres - 1) * -870) ) {
			liste_matieres.animate({"marginLeft": newPosX},1000,"easeOutExpo");
		}
		return false;
	});
	
	// bouton précédent
	$("a.btn_precedent").click(function(){
		$(".liste_travaux, .detail_travaux").slideUp("normal");
		var posX = liste_matieres.css('margin-left');
		posX = parseInt( posX.substring(0, posX.lastIndexOf('px')) );
		
		var newPosX = (Math.floor(posX / 870) * 870) + 870;		
		//console.log( '('+ Math.floor(posX / 870) +' * 870) + 870 = '+ newPosX );
		
		if( newPosX <= 0 ) {
			liste_matieres.animate({"marginLeft": newPosX},1000,"easeOutExpo");
		}
		return false;
	});
	
	$(".liste_travaux, .detail_travaux").hide();
	
	// bouton voir travaux
	$("a.voir_travaux").click(function(){
		// fermeture du détail
		$(".detail_travaux").slideUp("slow");
		// récupération de l'intitulé de la matière
		var intituleMatiere = $(this).attr('rel');
		$.get("assets/snippets/contenu/variables.travaux.php", { intituleMatiere: intituleMatiere },
			function(data){
				$(".liste_travaux").slideUp("slow", function(){
					
					$(".liste_travaux").html(data).slideDown("slow"); // affichage de la liste des travaux
					
					// application du template de la rubrique
					$("a.travail_details").css("color","#E56C11");
					// rollover sur les liens
					$("a.travail_details").mouseover(function(){
						$(this).css("color","#000000");
					}).mouseout(function(){
						$(this).css("color","#E56C11");
					});
					
					// bouton détail travail
					$("a.travail_details").click(function(){
						var idTravail = $(this).attr('rel');
						$.get("assets/snippets/contenu/variables.travaux.php", { idTravail: idTravail },
							function(data){
								$(".detail_travaux").slideUp("slow", function(){
									$(".detail_travaux").html(data).slideDown("slow"); // affichage du détail du travail
									
									// application du template de la rubrique
									$("a.travail_url").css("color","#E56C11");
									// rollover sur les liens
									$("a.travail_url").mouseover(function(){
										$(this).css("color","#000000");
									}).mouseout(function(){
										$(this).css("color","#E56C11");
									});
									// activation du lightbox sur les images
									initLightbox();
									
								}) // slideUp
							}
						);
						return false;						
					});
					
				}) // slideUp
			} // callback
		);
		return false;
	});
}


function galerieSensoriel() {
	var illustration_sensoriel_hover = $(".illustration_sensoriel_hover");
	illustration_sensoriel_hover.css("visibility","hidden");
	$("div.illustration_sensoriel").mouseover(function(){
		illustration_sensoriel_hover.css("visibility","visible");
	}).mouseout(function(){
		illustration_sensoriel_hover.css("visibility","hidden");
	});
}


function ratingSystem() {	
	// rate Up
	$("a.rateUp").click(function(){
		var idArticle = $(this).attr('rel');
		$.post('assets/snippets/contenu/rating.php', 
			{
				idArticle: idArticle, 
				action: 'up'
			},
			function(data){
				$("a.rateUp").parent().html(data);
			}, 
			"html"
		);
		// on masque les lien
		$("a.rateUp, a.rateDown").hide();
		return false;
	});
	
	// rate Down
	$("a.rateDown").click(function(){
		var idArticle = $(this).attr('rel');
		$.post('assets/snippets/contenu/rating.php', 
			{
				idArticle: idArticle, 
				action: 'down'
			},
			function(data){
				$("a.rateDown").parent().html(data);
			}, 
			"html"
		);
		// on masque les lien
		$("a.rateUp, a.rateDown").hide();
		return false;
	});
}


function initLightbox() {
	$(".travaux_illustrations a").lightBox();
}


function initializeGoogleMap() {
	
	if(document.getElementById("google_map"))
	{
		$("#google_map").css('height','250px').css('position','relative');

		var latlng = new google.maps.LatLng(45.862378,1.292363);
	    var myOptions = {
			zoom: 14,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    var map = new google.maps.Map(document.getElementById("google_map"), myOptions);

		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title:"Immeuble Cassiopée - Parc d’Ester<br />26, rue Atlantis<br />87069 Limoges Cedex"
		});
	}
	
}
	
}); // fin $(document).ready()

})(jQuery);