// JavaScript Document
$(document).ready(function() { 
	
	$('body').addClass('lang_'+$('.container input[type=hidden]:first').val());
	
	
	navigation();
	rolloverImage();
	navOngletRecette();
	carousselThematique()
	
	// 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( $('.print_memo').size() > 0 ) {
        $('.print_memo').click(function(){window.print()});  
    }
    
    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 ( $('.moteur_recherche').size() > 0 ) {
    
        $('.titre_filtre').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('p + div').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(){
                $('.how_use_active').trigger('click');
                $(this).parent().prev().slideToggle();
                $(this).parent().parent().find('strong:not(.liste_produit_marque div strong)').toggleClass('how_use_active');
                $(this).hide()
                return false;
            });
            $('.how_use_active').live('click',function(){
                $(this).toggleClass('how_use_active').parent().find('.more').prev().slideToggle();
                $(this).parent().find('.more a').show();
            });
        } 
        
    }
    
    // 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, .Graines').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;
        });
    }
    
    /* USTENSILES / INGREDIENTS / PANIFICATION */
    if ( $('.item_list').size() > 0 ) {
        $('.item_list > div:not(:first, .btm_item_list)').hide();
        $('.item_list li img').click(function(){
        
            $('.item_list li img').removeClass().not( $(this) ).addClass('hover').trigger('mouseout');
            
            var source = $(this).attr("src").length;
		    var ext = $(this).attr("src").substring(source - 4, source);
            var newsource = $(this).attr("src").substring(0, source - 4) + "_on" + ext;
            if ( $(this).attr("src").substring( source - 7, source - 4) != "_on" ) {
		      $(this).attr("src", newsource);
		    }
            $('.item_list > div:not(.btm_item_list)').hide().eq( $('.item_list li').index($(this).parent()) ).show();
        });
        $('.item_list li img:first').trigger('click');
    }
    	
    /* MACHINES */	
    if ( $('.pain_a_la_machine	').size() > 0 ) {
    
        $('.list_machine li > img').mouseenter(function(){
            if ( $(this).css('opacity') == 0.7 ) {
                $(this).fadeTo(200, 1);
            }
        });
        function machineBindOut() {
            $('.list_machine li > img').mouseout(function(){
                $(this).fadeTo(200, 0.7);
            });
        }
        machineBindOut();
        
        $('.list_machine li > img').click(function(){
            machineBindOut();
            $('.list_machine li > img').not($(this)).css('opacity',0.7);
            $('.list_machine li > span').hide();
            $(this).parent().find('span').show();
            $(this).unbind('mouseout');
            
            $('.list_machine li').removeClass('selected_machine');
            $(this).parent().addClass('selected_machine');
            $('.machine_detail').show().html('<span class="lm_top"></span>'+$(this).next().next().html()+'<span class="lm_btm"></span>');
            $('.machine_detail img').hover(function () {$(this).next().show();},function () {$(this).next().hide();} );
        });
        
         $('.list_conseil dt').click(function(){
            $('.list_conseil dt').removeClass();
            $(this).addClass('active_sos');
            $('.paneoman').hide()
            $('.list_conseil dd').hide().eq($('.list_conseil dt').index($(this))).show();
         });
        
    }
    	
	// FORMULAIRE : ajout de class afin de pouvoir ciblier les diff&eacute;rents <input type="">
	$(":text").addClass("input-text");
	$(":radio").addClass("input-radio");
	$(":image").addClass("input-image");
	$(":checkbox").addClass("input-checkbox");
	$(":file").addClass("input-upload");
						   
});

