// variable de controle de formulaire
var formCheck;

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

    /* Bloc contact */
    
        $("#blocContact").css('right','-122px').hover(    
            function () {
                $(this).stop(true, true).animate({'right':'0px'}, 'slow');
            },
            function () {
                $(this).animate({'right':'-122px'}, 'slow');
            }
        );
    
    /* Rolls sur bons plans */
        
        $('.blocBonsPlans').hover(function() {
            $(this).find('.roll').fadeIn(); // .delay(200)
        }, function() {
            $(this).find('.roll').stop(true, true).fadeOut();
        });
        
    /* Pictos services */
    
        if ( $('.descriptionPictoService').length )
        {
        
            $('.pictoService a').bind('mouseover focus click', function() {
            
                $('.pictoService a').removeClass('current');
                $('.descriptionPictoService').html($(this).addClass('current').find('span').html());
                
                return false;
            
            });
            
        }
        
    /* MenuCC */
        
        $('.btMenuCC').click(function(e) {
            
            $('#menuCC').slideToggle();

            this.blur();
            e.preventDefault();
            
        });
        
    /* Lancement ScrollBar */
        if ( $('.tinyScrollBar').length ) {
            $('.tinyScrollBar').tinyscrollbar();
        }
    
    /* Gestion des onglets de navigation */
        
        if ( $("#container .onglets li a.current").length ) // si on a un onglet activé par défaut on l'affiche
        {
            $("#container .contentOnglets>li:not(:eq("+$("#container .onglets").show().find("li a.current").parent().index()+"))").hide();
        }
        else // sinon on affiche le 1er onglet
        {
            $("#container .onglets").show().find("li:first a").addClass("current");
            $("#container .contentOnglets>li:not(:first)").hide();
        }
        
        $("#container .onglets li").delegate("a", "click", function(e){
        
            $(this).parent().parent().find("a.current").removeClass("current");
            
            $("#container .contentOnglets>li:visible").hide();        
            $("#container .contentOnglets>li:eq("+$(this).addClass("current").parent().index()+")").show();
                        
            this.blur();
            e.preventDefault();
        });
        
    /* Gestion des sous onglets */
    
        if ( $('#container .sousOnglets').length ) {
        

            $('#container .sousOnglets').each(function() {
                
                if ( $(this).find('li a.current').length ) // si on a un onglet activé par défaut on l'affiche
                {
                    $(this).next('.contentSousOnglets').find(">li:not(:eq("+$(this).show().find("li a.current").parent().index()+"))").hide();
                }
                else // sinon on affiche le 1er onglet
                {
                    $(this).show().find("li:first a").addClass("current");
                    $(this).next('.contentSousOnglets').find(">li:not(:first)").hide();
                }
            
            });
            

            $("#container .sousOnglets li").delegate("a", "click", function(e){
            
                $(this).parent().parent().find("a.current").removeClass("current");
                
                $(this).closest('.sousOnglets').next('.contentSousOnglets').find(">li:visible").hide();
                $(this).closest('.sousOnglets').next('.contentSousOnglets').find(">li:eq("+$(this).addClass("current").parent().index()+")").show();
                            
                this.blur();
                e.preventDefault();
            });
        
        }
   
        
    /* Lancement Carousel */
    
        $('#fondFade').carousel({ 
            manual: false, 
            auto: true,
            speed: 1000,
            frequency: 5000
        });


        $('.carousel:not(#carouselPagination)').carouselSlide({
            manual: true, 
            auto: true,
            speed: 1000,
            item_deplace: 1,
            frequency: 3000
        });

        $('#carouselPagination').carouselSlide({
            manual: true,
            auto: true,
            speed: 1500,
            frequency: 6000,
            item_deplace: 1,
            pagination: true
        });
    
    
    /* Gestion de la toolbar impression */
    
        $('#container .btImprim a').click(function (e) {
            window.print();
            e.preventDefault();
        });

        
    /* Gestion des lightbox */
    
        $(".galerieFancybox").fancybox();
        $(".openLightComm").fancybox();
        $(".openLightFancy").fancybox();
        $(".openLightPlan").fancybox({
            'onComplete'	:	function() {
                initialize_map();
            }
        });
            
    // Gère l'affichage des photos en grands // le each évite les boutons prev et next sur les carousels sessions
    //$('#container .carousel').each(function() { $(this).find('.apercuCarousel a').lightBox(); });
    
    /* Lightbox **/
    
        $(".btLightbox").click(function(){
            $("#overlayLightbox").show();
            $($(this).attr('href')+"Box").show();
            this.blur();
            return false;
        });

        $(".lightBox .fermer a, #overlayLightbox").live('click', function() {

            $("#overlayLightbox").hide();
            $("body .lightBox:visible").hide();
            
            this.blur();
            return false;

        });
        
        $(document).keyup(function(event){
            if (event.keyCode == 27 && $("#overlayLightbox").is(':visible')) {
                $("#overlayLightbox").hide();
                $("body>.lightBox:visible").hide();
            }
        });

    /* Controle formulaire */

        $('.formStyle .submit input').click(function() {
            
            var messageForm = {};
            
            $(this).closest('.formStyle').find('.erreurForm').remove();

            $(this).closest('.formStyle').find('.required').each(function(){

                if ( $(this).find('input[type="radio"]:checked, input[type="checkbox"]:checked, input[type="text"][value!=""], input[type="password"][value!=""], select[value!=""], textarea[value!=""]').length == 0  )
                {
                    messageForm['required'] = 'Vous devez remplir les champs obligatoires signalés par un "*"';
                    if ( $(this).is('.radio') || $(this).is('.checkbox') ) {
                        $(this).find('label').css('color','#d01c1c');
                    } else {
                        $(this).find('input,select,textarea').css('border','1px solid #d01c1c');
                    }
                }
                else
                {
                    if ( $(this).is('.radio') || $(this).is('.checkbox') ) {
                        $(this).find('label').css('color','');
                    } else {
                        $(this).find('input,select,textarea').css('border','');
                    }
                }
            });
            
            $(this).closest('.formStyle').find('.trueEmail').each(function(){
            
                input = $(this).find('input');
                valueEmail = input.prop('value');
                emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

                if ( valueEmail != '' )
                {
                    if ( !emailPattern.test(valueEmail) )
                    {
                        messageForm['trueEmail'] = 'Vous devez entrer une adresse mail valide.';
                        input.css('border','1px solid #d01c1c');
                    }
                    else
                    {
                        input.css('border','');
                    }
                }
                else if ( !$(this).is('.required') )
                {
                    input.css('border','');
                }
            });
            
            $(this).closest('.formStyle').find('.passIdentique').each(function(index){
            
                input = $(this).find('input');
                password = input.prop('value');
                
                if ( password != '' )
                {
                    if ( index == 0 )
                    {
                        passwordIndex = password;
                    }
                    
                    if ( passwordIndex != password )
                    {
                        messageForm['passIdentique'] = 'Les mots de passe ne sont pas identiques';
                        $(this).parent().find('.passIdentique input').css('border','1px solid #d01c1c');
                    }
                    else if ( password.length < 6 )
                    {
                        messageForm['passLongueur'] = 'Votre mot de passe doit comporter 6 caractères minimum';
                        input.css('border','1px solid #d01c1c');
                    }
                    else
                    {
                        input.css('border','');
                    }
                }
            });
            
            $(this).closest('.formStyle').find('.formatDate').each(function(){
            
                $(this).find('input').each(function(index) {
                
                    var valueDate = $(this).prop('value');
                    
                    if ( valueDate != '' && !$(this).is('.placeHolderColor') )
                    {
                        if ( index == 0 ) {
                            valueMin = 1;
                            valueMax = 31;
                        } else if ( index == 1 ) {
                            valueMin = 1;
                            valueMax = 12;
                        } else if ( index == 2 ) {
                            valueMin = 1900;
                            valueMax = 2020;
                        }

                        if ( isNaN(valueDate) || valueDate.length < 2 || valueDate < valueMin || valueDate > valueMax )
                        {
                            messageForm['formatDate'] = 'Le format de votre date de naissance est incorrect.';
                            $(this).css('border','1px solid #d01c1c');
                        }
                        else
                        {
                            $(this).css('border','');
                        }
                    }
                });
            });
            
            $(this).closest('.formStyle').find('.formatCP').each(function(){

                input = $(this).find('input');
                valueCP = input.prop('value');
                
                valueMin = 1000;
                valueMax = 99999;

                if ( valueCP != '' )
                {
                    if ( isNaN(valueCP) || valueCP < valueMin || valueCP > valueMax )
                    {
                        messageForm['formatCP'] = 'Le format de votre code postal est incorrect.';
                        input.css('border','1px solid #d01c1c');
                    }
                    else
                    {
                        input.css('border','');
                    }
                }
                else if ( !$(this).is('.required') )
                {
                    input.css('border','');
                }
            });

            var affichageErreur = '';
            
            for ( var cle in messageForm ) {
                affichageErreur += messageForm[cle]+"<br />";
            }

            if ( affichageErreur == '' ) {
            
                formCheck = true;
            }
            else {
                $(this).closest('.formStyle').prepend('<div class="erreurForm">'+affichageErreur+'</div>');
                formCheck = false;
            }
        });
    
        
        // Controle formCheck
        
        $('.formStyle').submit(function() {
        
            if ( formCheck ) {
                this.submit();
            }
            else {
                return false;
            }

        });
    
    
  $('#rechercheParNomFiche').change(function () {
    window.location=$(this).val();
  });

  $('#rechercheParThematiqueFiche').change(function () {
    window.location=$(this).val();
  });

  $('#rechercheParAlbum').change(function () {
    window.location=$(this).val();
  });


}); /* jQuery end */

