$(document).ready(function(){	

/***************************************************************************************
	Tooltip => [Images]
***************************************************************************************/
		
			$('.lien2').click(function(){
				return false;
			})
		
			$('.ajaxTip')
				.hover(function (){
					var urlTip = '';
					urlTip = $(this).attr('href');
					var nomTip = ((urlTip).split("/"))[2]
					var id = ((nomTip).split("_"))[0]
					$.ajax({
						type: 'POST',
						data : {
							'id' : id
						},
						url : '/voir.php',
						success : function(data) {
							if(data == ""){
								$('#tooltip').empty();
								$('#tooltip').addClass('ignore');
							}else {
								$('#tooltip').removeClass('ignore');
								$('#tooltip').empty().append(data);
							}
						}
					});
				});
				
				$('.ajaxTip').tooltip({
					showURL: false,
					fade: 250,
					bodyHandler: function() { 
						return $("<img/>").attr("src", '/img/loader.gif');
					}
				});	
			
			$("#tooltip img").ajaxStart(function(){
				$(this).show();
			})
			
			$("#tooltip img").ajaxStop(function(){
				$(this).hide();
			})
		

/***************************************************************************************
	=> Médiathèque => Gestion du bouton de recherche
***************************************************************************************/

		$('#sectionMediatheque #formRecherche button').hide().after('<span class="bouton">'+$('#sectionMediatheque #formRecherche button').html()+'</span> ');
		
		$('#formRecherche select').each(function(){
			
			var indexSelected = $(this).children('option').index($(this).find(':selected'));
			if(indexSelected > 0){
				$('#formRecherche .bouton').hide();
				$('#formRecherche button').show();	
			}
			
		})

/***************************************************************************************
	=> <input/> par défaut
***************************************************************************************/

    $(":input.valeurParDefaut").each(function() { 
         
        var classeCSS = "couleurValeurParDefault"; // optionel 
        var valeurParDefaut = $(this).attr("title"); 
 
        // Au chargement de la page 
        var valeurActuel = $(this).val(); 
        if (valeurActuel == "") { 
            $(this).val(valeurParDefaut); 
            $(this).addClass(classeCSS); // optionel
        } else {
        	$('#formRecherche .bouton').hide();
			$('#formRecherche button').show();
        }
        // Sur le focus 
        $(this).focus(function() { 
            var valeurActuel = $(this).val(); 
            if (valeurActuel == valeurParDefaut) { 
                $(this).val(""); 
                $(this).removeClass(classeCSS); // optionel 
            } 
        }); 
        // Sur le blur 
        $(this).blur(function() { 
            var valeurActuel = $(this).val(); 
            if (valeurActuel == "") { 
                $(this).val(valeurParDefaut); 
                $(this).addClass(classeCSS); // optionel 
                $('#formRecherche .bouton').show();
				$('#formRecherche button').hide();
            } else {
            	$('#formRecherche .bouton').hide();
				$('#formRecherche button').show();
            }
            
        	$('#formRecherche select').each(function(){
        		
        		var indexSelected = $(this).children('option').index($(this).find(':selected'));
				
				if(indexSelected > 0){
	            	$('#formRecherche .bouton').hide();
					$('#formRecherche button').show();
				}	
        		
        	})			
            
        }); 
    }); 

/***************************************************************************************
	=> Select custom [mediathque]
***************************************************************************************/

		if($('#formRecherche select').length > 0){
			$('#formRecherche select').ixSelect();
		}


/***************************************************************************************
	=> Colorbox
***************************************************************************************/

	$('#chansons .tooltip').colorbox({
		iframe:true,
		width:470,
		height:190
	});
	
	$('#sectionMediatheque .colorbox, #sectionMediatheque .boite .listeMedias li.audio a.lien').colorbox({
		iframe:true,
		width:470,
		height:190
	});
	
	$('#sectionMediatheque .imagebox, #sectionMediatheque .boite .listeMedias li.image a').colorbox({
		title: function(){
		    var longdesc = $(this).children('img').attr('longdesc');
		    return longdesc;
		}
	});
	
	$('#sectionMediatheque .videobox, #sectionMediatheque .boite .listeMedias li.video a').colorbox({
		iframe: true,
		width: 700,
		height: 630,
		title: function() {
		    var longdesc = $(this).children('img').attr('longdesc');
		    return longdesc;
		}
	});
	

/***************************************************************************************
	=> Slideshow des fiches
***************************************************************************************/

	var indexImage = 0;
	var lesImages = $('#slide ul li');
	var nbImages = lesImages.length;
	lesImages.eq(indexImage).css('z-index', 10).siblings().hide();
	
	$('#controles a').click(function() {
		var indexImage2 = lesImages.eq(indexImage);
		
		if(nbImages == 1){
			
			return false;
		}else {
			
			if ($(this).is('.precedent')) {
				nouvelleImage = indexImage2.prev();	
				
				if (indexImage == 0) {
					nouvelleImage = lesImages.filter(':last');
					indexImage = nbImages-1;
				} else {
					indexImage--;
				}
			} else {
				nouvelleImage = indexImage2.next();
				if (indexImage == nbImages-1) {
					nouvelleImage = lesImages.filter(':first');
					indexImage = 0;
				} else {
					indexImage++;
				}	
			}
			
			indexImage2.hide();
			lesImages.stop();
			nouvelleImage.css('opacity', 0).show().animate({
				opacity: 1
			},{
				duration: 3000,
				easing: 'easeOutExpo'
			});
		
		}
		return false;
	});

/* =Login de l'entête
   ----------------------------------------------------*/

   $('ul#descriptions li:gt(0)').hide();
/*
   $('ul#choix_accueil li').mouseover(function() {
		var id = $(this).attr('id');
		id = id.replace(/choix-/,'');
		$('ul#descriptions li').hide();
		$('ul#descriptions li#description-' + id).show();
	   });
*/


   $('li#connexion a').click(function() {
		$('li#connexion').hide();
		$('li#inscrire').hide();
		$('#navigation').css('right','50px');
		$('li.entrer').fadeIn('normal');
		$('#erreur_login').fadeIn('normal');
	   });

   $('a#quitter').click(function() {
		$('li#connexion').fadeIn('normal');
		$('li#inscrire').fadeIn('normal');
		$('#navigation').css('right','120px');
		$('li.entrer').hide();
		$('#erreur_login').hide();
	   });

/* =Remplir les champs par défault
----------------------------------------------------*/

   $(":input.valeurParDefaut").each(function() {

        var classeCSS = "couleurValeurParDefault"; // optionel
        var valeurParDefaut = $(this).attr("title");

        // Au chargement de la page
        var valeurActuel = $(this).val();
        if (valeurActuel == "") {
            $(this).val(valeurParDefaut);
            $(this).addClass(classeCSS); // optionel
        }
        // Sur le focus
        $(this).focus(function() {
            var valeurActuel = $(this).val();
            if (valeurActuel == valeurParDefaut) {
                $(this).val("");
                $(this).removeClass(classeCSS); // optionel
            }
        });
        // Sur le blur
        $(this).blur(function() {
            var valeurActuel = $(this).val();
            if (valeurActuel == "") {
                $(this).val(valeurParDefaut);
                $(this).addClass(classeCSS); // optionel
            }
        });
    });

/* =Questions
   ----------------------------------------------------*/

    $('input.question').hide();

	$('#questions2 li').addClass('non_repondu');

	$('#questions2 li').click(function(){
    	$(this).toggleClass('non_repondu')
    	$(this).toggleClass('repondu')

    var checked=$(this).find('input').attr('repondu');
    $('#questions').text(checked);

    if( $('#questions2 li').not(this).hasClass('repondu') )
    	$(this).addClass('repondu')
    	$('#questions2 li').not(this).removeClass('repondu')
    	$('#questions2 li').not(this).addClass('non_repondu')
  	});

	$('.choix_1').click(function(){
    	$('#reponse').attr('value','1');
  	});

	$('.choix_2').click(function(){
    	$('#reponse').attr('value','2');
  	});

	$('.choix_3').click(function(){
    	$('#reponse').attr('value','3');
 	 });


/* =iconographie
   ----------------------------------------------------*/
   

	$(".tooltip").tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		opacity: 1, 
		fixPNG: true 
	}); 


/*  BACKUP - CODE FONCTIONNEL a REMY
	avec TOOLS.TOOLTIP

	if($('#accueil').length > 0){
		$(".tooltip").tooltip({
			tip: '#ic0nographie',
			position: "top left",
			opacity: 0.8,
			offset: [0, 125]
		});

	}else {
		
		$(".tooltip").tooltip({
			tip: '#iconographie',
			position: "center left",
			opacity: 0.8,
			offset: [0, -20]
		});
		
	}
*/
	
/*
	$('div.iconographie').hide();

	$('div.image-gauche img, div.image-droite img').hover(
		function (){
			$(this).siblings('.iconographie').show();
		},
		function (){
			$(this).siblings('.iconographie').hide();
		}
	);

	$('a.tip').hover(
		function (){
			$(this).children('.iconographie').show();
		},
		function (){
			$(this).children('.iconographie').hide();
		}
	);
*/
	
});

