// JavaScript Document
$(document).ready(function() { 
	
	navigation();
	rolloverImage();
	navOngletRecette();
	
	// Lien Changer page d'accueil
	if ( navigator.appName != 'Microsoft Internet Explorer' ) { 
	    // On supprime le lien car la fonction ne marche pas sous FF
	    $('#footer ul:first li:last').prev().prev().remove();
    }
    if(navigator.appVersion.indexOf('Chrome') != -1) {
        $('#footer ul:first li:last').prev().remove();
    };

    if ( $('.popup:not(.popup_convert)').size() > 0 ) {
        addModale();
        $('.fermer_popup').click(function(){
            $('.popup, .modale').fadeOut();
            $('.modale').remove();
            return false;
        });
    }
    
    if ( $('.popup_note').size() > 0 ) {
    
        aide_plus = $('.popup_note li.note_plus label').text();
        $('.popup_note li.note_plus textarea').form_exemple(aide_plus);
        aide_moins = $('.popup_note li.note_moins label').text();
        $('.popup_note li.note_moins textarea').form_exemple(aide_moins);
        
        TempNote = 0;
        
        function getNote(noteX) {
           note = noteX.substring(4,5);
           //console.log(noteX + " - " + note + " - " + TempNote);
           return note;
        }
    
        $('.popup_note ul li.notez span').mouseenter(function(){
            getNote($(this).find('img').attr('class'));
            
            $('.popup_note').find('li img').attr("src", "/ressources/images/picto_notez.gif"); 
            for (i=1; i<=note; i++){
                $('.popup_note').find('img.note'+i).attr("src", "/ressources/images/picto_notez_on.gif"); 
            }
        });
        $('.popup_note ul li.notez span').mouseleave(function(){
		      $('.popup_note').find('li img').attr("src", "/ressources/images/picto_notez.gif");
		      $('.popup_note').find('li img').slice(0,TempNote).attr("src", "/ressources/images/picto_notez_on.gif");
        });
        $('.popup_note ul li.notez span').click(function(){
            TempNote = getNote($(this).find('img').attr('class'));
            $(this).prev().attr('checked','checked');
        });  
        $('.popup_note ul li a.btn_gris').click(function(){
            $('.modale').remove();
        });  
     
    }
    
    // Moteur recherche
    if ( $('.MoteurRecherche').size() > 0 ) {
    
        $('.titre_filtre').not('.titre_filtre:first').next().hide();
        $('.titre_filtre:first').css('backgroundPosition','left bottom');
        $('.titre_filtre').click(function(){
        
            if ( $(this).next().is(':visible') ) {
                $(this).css('backgroundPosition','left top');
                $(this).next().hide();
            } else {
                $(this).css('backgroundPosition','left bottom');
                $(this).next().show();
            }
            
        });
    }
    
    // FAQ
    if ( $('.faq').size() > 0 ) {
        $('dl.faq dd:not(:first)').hide();
        $('dl.faq dt').click(function(){
            if ( !$(this).next().is(':visible') ){
                $('dl.faq dd').slideUp();
                $(this).next().slideDown();
            } else {
                $(this).next().slideUp();
            }
        });
    }
    // LEXIQUE 
    if ( $('.lexique').size() > 0 ) {
        $('.lexique dl dd:not(:first)').hide();
        $('.lexique dl dt').click(function(){
            if ( !$(this).next().is(':visible') ){
                $('.lexique dl dd').slideUp();
                $(this).next().slideDown();
            } else {
                $(this).next().slideUp();
            }
        });
    }
    
    // Gestion onglet recette
    // A voir : hide des div autrement qu'en javascript au chargement.
    // A faire : exporter ce code dans jquery.fn.js
    
    // Imprimer page recette
    if ( $('.imprim_recette').size() > 0 ) {
        window.print();
    }
    
    // marque locale 
    if( $('.marqueLocale').size() > 0 ) {
    
        $('.liste_produit_marque li').find('div:last').hide();
    
        $('.liste_produit_marque li').each(function(){
            if ( $(this).find('p').height() <  $(this).find('img:first').height()-10) {
               $(this).find('p').css('height',$(this).find('img:first').height()-10 );
            }
        });
        
        if ( $('.more').size()>0 ) {
            $('.more a').click(function(){
                $(this).parent().next().slideToggle();
                return false;
            });
        } 
        
    }
    
    // TOUT SAVOIR SUR LE PAIN : FAIRE SON PAIN A LA MAIN / MACHINE
    
    if ($('.autres_ingredients').size() > 0) {
        $('.autres_ingredients').find('div:not(:first):not(.autres_ingredients div div)').hide();
    
        $('.onglet_ingredient li').click(function(){
            var divToShow = $(this).attr('class');
            $(this).parent().parent().find('div:not(.autres_ingredients div div)').hide();
            $('.'+divToShow+':not(li)').show();
        });
        
        
        
        if ( navigator.appName == 'Microsoft Internet Explorer' ) { 
	        $('.autres_ingredients > div').each(function(){
                $(this).css('paddingLeft', '0px');
            });
        } else {
            $('.autres_ingredients > div').each(function(){
                $(this).css('paddingLeft', $(this).parent().find('.onglet_ingredient').width()+10);
            });
        }
        
        
    }
    
    
    // PAGE ATELIER : PRINT
    if( $('.atelier').size() > 0) {
        $('.btn_print').click(function(){
            window.print();
        });
    }
    
    // CONVERTISSEUR 
    if( $('.popup_convert').size() > 0) {
        $('.fermer_popup').click(function(){
            $('.popup_convert').hide();
            return false;
        });
        
        convertisseur();

    }
    if( $('.show_convert').size() > 0) {
        $('.show_convert').click(function(){
            $('.popup_convert').show();
            $(document).scrollTop(0);
            return false;
        });
    }
    
    	
	// FORMULAIRE : ajout de class afin de pouvoir ciblier les différents <input type="">
	$(":text").addClass("input-text");
	$(":radio").addClass("input-radio");
	$(":image").addClass("input-image");
	$(":checkbox").addClass("input-checkbox");
	$(":file").addClass("input-upload");
						   
});

