// MOTEUR DE RECHERCHE AVANCEE
$(document).ready(function(){
  //Cache les blocs au chargement de la page
  $("div#recherche_precision").hide();
   
  //Ouvre-Ferme un bloc
  $("div#btn_deroulant").click(function(){
    if ($(this).next().is(':hidden')) {
      $(this).next().slideDown("slow");
      document.getElementById("btn_rech_av").style.backgroundPosition="bottom left";
      return false;
    }
    else {
      $(this).next().slideUp("slow");
      document.getElementById("btn_rech_av").style.backgroundPosition="top left";
      return false;
    }
  });
});


// FICHE DETAILLEE LICENCE
function ouvreFermeLicence(){
  var obj = document.getElementById('texte_licence');
  if (obj) {
    if (obj.style.display == 'block') {
      obj.style.display = 'none';
    }
    else if (obj.style.display == 'none'){
      obj.style.display = 'block';
    }
    else {
      obj.style.display = 'none';
    }
  }
}


function replierSelection() {
  //Met un vide à la place du "+" si il n'y a pas de résultats en-dessous
  $('h2').each(function() {
    if ($(this).next().children('.liste_resultats_selection').length > 0) {
      $("a", this).children('img').attr({
        "alt" : "Ouvrir", 
        "src" : "img/bg_h2_plus_selection.gif"
      });
      $("a", this).attr({
        "title" : "Ouvrir"
      });
      $(this).next().children('.liste_resultats_selection').hide();
    }

  });

  var i=0;
  for(i=3;i<7;i=i+1){
    var titre = "h"+i;
    $(titre).each(function() {
      if ($(this).next().children('.liste_resultats_selection').length > 0) {
			
        $("a", this).children('img').attr({
          "alt" : "Ouvrir", 
          "src" : "img/bg_plus_selection.gif"
        });
        $("a", this).attr({
          "title" : "Ouvrir"
        });
        $(this).next().children('.liste_resultats_selection').hide();
      }
    });
  }
}

function deplierSelection() {
  //Met un vide à la place du "+" si il n'y a pas de résultats en-dessous
  $('h2').each(function() {
    if ($(this).next().children('.liste_resultats_selection').length > 0) {
      $("a", this).children('img').attr({
        "alt" : "Fermer", 
        "src" : "img/bg_h2_moins_selection.gif"
      });
      $("a", this).attr({
        "title" : "Fermer"
      });
    }

  });

  var i=0;
  for(i=3;i<7;i=i+1){
    var titre = "h"+i;
    $(titre).each(function() {
      if ($(this).next().children('.liste_resultats_selection').length > 0) {
        $("a", this).children('img').attr({
          "alt" : "Fermer", 
          "src" : "img/bg_moins_selection.gif"
        });
        $("a", this).attr({
          "title" : "Fermer"
        });
      }
    });
  }
}



// SELECTION THEMATIQUE
$(document).ready(function(){

  $("a#deplier").show();
  replierSelection();
	
	
  //Cache les blocs au chargement de la page
  //$(".liste_resultats_selection").hide();
	
  //Ouvre-Ferme tous les blocs en changeant de valeur
  $("a#deplier").click(function(){

    if ($("div#liens_deplier_replier a").text() =='Tout replier') {
      $("div#liens_deplier_replier a").html('Tout d&eacute;plier');
      $(".liste_resultats_selection").each(function() {
        $(this).hide();
        replierSelection();
      });
    } else {
      $("div#liens_deplier_replier a").html('Tout replier');	
      $(".liste_resultats_selection").each(function() {
        $(this).show();
        deplierSelection();
      /*
				$("h2 a").children('img').attr({"alt" : "Fermer", "src" : "img/bg_h2_moins_selection.gif"});
				$("h2 a").attr({"title" : "Fermer"});
		
				var i=0;
				for(i=3;i<7;i=i+1){
					var titre = "h"+i;
					$(titre + "a").children('img').attr({"alt" : "Fermer", "src" : "img/bg_moins_selection.gif"});
					$(titre + "a").attr({"title" : "Fermer"});
				}	*/									  
      });
    }
  /*		
		$(".liste_resultats_selection").each(function() {								
		  if ($(this).is(':hidden')) {
			$(".liste_resultats_selection").slideDown("slow");
			
			$("h2 a").children('img').attr({"alt" : "Fermer", "src" : "img/bg_h2_moins_selection.gif"});
			$("h2 a").attr({"title" : "Fermer"});
		
			var i=0;
			for(i=3;i<7;i=i+1){
				var titre = "h"+i;
				$(titre + "a").children('img').attr({"alt" : "Fermer", "src" : "img/bg_moins_selection.gif"});
				$(titre + "a").attr({"title" : "Fermer"});
			}
			
			$("div#liens_deplier_replier a").html('Tout replier');
			return false;
		  }
		  else {
			$(".liste_resultats_selection").slideUp("slow");
			
			if ($("h2").next().children('.liste_resultats_selection').length == 0) {
				$("h2 a").children('img').attr({"alt" : "Fermer", "src" : "img/bg_h2_vide_selection.gif"});
				$("h2 a").attr({"title" : "Fermer"});
			}
			else{
				$("h2 a").children('img').attr({"alt" : "Ouvrir", "src" : "img/bg_h2_plus_selection.gif"});
				$("h2 a").attr({"title" : "Ouvrir"});
			}
			
			var i=0;
			for(i=3;i<7;i=i+1){
				var titre = "h"+i;
				if ($(titre).next().children('.liste_resultats_selection').length == 0) {
					$(titre + "a").children('img').attr({"alt" : "Fermer", "src" : "img/bg_h2_vide_selection.gif"});
					$(titre + "a").attr({"title" : "Fermer"});
				}
				else{
					$(titre + "a").children('img').attr({"alt" : "Ouvrir", "src" : "img/bg_plus_selection.gif"});
					$(titre + "a").attr({"title" : "Ouvrir"});
				}
			}
			
			$("div#liens_deplier_replier a").html('Tout d&eacute;plier');
			return false;
		  }*/
  });
   
  $("h2").click(function(){
    if ($(this).next().children('.liste_resultats_selection').is(':hidden')) {
      if ($(this).next().children('.liste_resultats_selection').length == 0) {
        $("a", this).children('img').attr({
          "alt" : "Fermer", 
          "src" : "img/bg_h2_vide_selection.gif"
        });
        $("a", this).attr({
          "title" : "Fermer"
        });
      }
      else{
        $("a", this).children('img').attr({
          "alt" : "Fermer", 
          "src" : "img/bg_h2_moins_selection.gif"
        });
        $("a", this).attr({
          "title" : "Fermer"
        });
      }
      $(this).next().children('.liste_resultats_selection').slideDown("slow");
      return false;
    }
    else {
      if ($(this).next().children('.liste_resultats_selection').length == 0) {
        $("a", this).children('img').attr({
          "alt" : "Fermer", 
          "src" : "img/bg_h2_vide_selection.gif"
        });
        $("a", this).attr({
          "title" : "Fermer"
        });
      }
      else{
        $("a", this).children('img').attr({
          "alt" : "Ouvrir", 
          "src" : "img/bg_h2_plus_selection.gif"
        });
        $("a", this).attr({
          "title" : "Ouvrir"
        });
      }
      $(this).next().children('.liste_resultats_selection').slideUp("slow");
      return false;
    }
  });
  
  var i=0;
  for(i=3;i<7;i=i+1){
    var titre = "h"+i;
    $(titre).click(function(){
      if ($(this).next().children('.liste_resultats_selection').is(':hidden')) {
        if ($(this).next().children('.liste_resultats_selection').length == 0) {
          $("a", this).children('img').attr({
            "alt" : "Fermer", 
            "src" : "img/bg_vide_selection.gif"
          });
          $("a", this).attr({
            "title" : "Fermer"
          });
        }
        else{
          $("a", this).children('img').attr({
            "alt" : "Fermer", 
            "src" : "img/bg_moins_selection.gif"
          });
          $("a", this).attr({
            "title" : "Fermer"
          });
        }
        $(this).next().children('.liste_resultats_selection').slideDown("slow");
        return false;
      }
      else {
        if ($(this).next().children('.liste_resultats_selection').length == 0) {
          $("a", this).children('img').attr({
            "alt" : "Fermer", 
            "src" : "img/bg_vide_selection.gif"
          });
          $("a", this).attr({
            "title" : "Fermer"
          });
        }
        else{
          $("a", this).children('img').attr({
            "alt" : "Ouvrir", 
            "src" : "img/bg_plus_selection.gif"
          });
          $("a", this).attr({
            "title" : "Ouvrir"
          });
        }
        $(this).next().children('.liste_resultats_selection').slideUp("slow");
        return false;
      }
    });
  }
});


/**
 * 
 */
function setAjaxDisciplines(niveau, discipline) {
  if ($("select#niveaux option:selected").val()) {
    niveau=$("select#niveaux option:selected").val();
  }
  
  $('select#disciplines').html('<option value=\'\'>Chargement des enseignements</option>');
  $('select#disciplines').attr('disabled', 'disabled');
  $.ajax({
    type: "GET",
    url: "/ajax/ajaxDisciplines.php?niveau="+niveau+"&discipline="+discipline,
    error:function(msg){
      //alert( "Error !: " + msg );
    },
    success:function(data){
      //affiche le contenu du fichier dans le conteneur dédié
      $('select#disciplines').html(data);
      $('select#disciplines option[value='+discipline+']').attr("selected", "selected");
      $('select#disciplines').attr('disabled', '');          
    }
  });
}

/**
 * 
 */
function setAjaxNiveaux(niveau) {
  $.ajax({
    type: "GET",
    url: "/ajax/ajaxNiveaux.php?niveau="+niveau,
    error:function(msg){
      //alert( "Error !: " + msg );
    },
    success:function(data){
      //affiche le contenu du fichier dans le conteneur dédié
      $('select#niveaux').html(data);
      $('select#niveaux option[value='+niveau+']').attr("selected", "selected");
            
    }
  });
}
