$(document).ready(function(){

    function decla(){
    
        /*  Variables de modification de l'animation : */
        var $top=-250;
        var $tbase=12500
        var $op=0.4
        var deplacement=-8;
        /* Fin de ces variables */
        var $tempo;
        /* Variable utilisants le css ou html*/
        var nbrc = $('.photo').children().length-1;
        /* Fin de ces variables */

        /* Aléatoires des Images */
        var Val1;
        var Val2;
        var Val3;
    
        /* Aléatoires des positions : 3 types 0/1/2 */
        var Valp3;
        var Valp2;
        var Valp1;
    
        /* les position px en fonction de pos1/pos2/pos3*/
        var number1;
        var number2;
        var number3;
    
        /* position des images*/
        var largeur = $('.images').width();
        largeur+=parseInt($('.images').css("padding-left"));
        largeur+=parseInt($('.images').css("padding-right")) ;
        largeur+=parseInt($('.images').css("margin-left")) ;
        largeur+=parseInt($('.images').css("margin-right")); 

        var hauteur= $('.images').height() ;
        hauteur+=parseInt($('.images').css("padding-top"));
        hauteur+=parseInt($('.images').css("padding-bottom")) ;
        hauteur+=parseInt($('.images').css("margin-top")) ;
        hauteur+=parseInt($('.images').css("margin-bottom")); 

        var pos1=deplacement;
        var pos2=hauteur+ pos1;
        var pos3=pos2+hauteur ;
        var pos4=pos3+hauteur;

        /* Fin position des images */

        /*Pour emepcher que les images se déplacent deux fois */   
        var num1;
        var num2;
        var num3;
    
        /*Autres Variables */   
        var z=50;  /* Pour les z-index pour la supériorité des images */
        var t=0;  /* Pour empecher de bouger les images de départs les deux premmiers tours */



        /* Départ */
        var dep1;
        var dep2;
        var dep3;
        var dep4;
        dep1 =  $.randomBetween(0,nbrc);
        dep2 =  $.randomBetween(0,nbrc);
        dep3 =  $.randomBetween(0,nbrc);
        dep4 =  $.randomBetween(0,nbrc);

        while(dep1==dep2 ||dep1 == dep3 || dep1 == dep4 || dep2 == dep3 || dep2 == dep4 || dep3 == dep4){
    
            dep1 =  $.randomBetween(0,nbrc);
            dep2 =  $.randomBetween(0,nbrc);
            dep3 =  $.randomBetween(0,nbrc);
            dep4 =  $.randomBetween(0,nbrc);   
        }

        /* Affiche les images de départ */
        $('#photo'+(dep1+1)).html('<img src="photos/('+(dep1+1)+').jpg" alt="" />');
        $('#photo'+(dep2+1)).html('<img src="photos/('+(dep2+1)+').jpg" alt="" />');
        $('#photo'+(dep3+1)).html('<img src="photos/('+(dep3+1)+').jpg" alt="" />');
        $('#photo'+(dep4+1)).html('<img src="photos/('+(dep4+1)+').jpg" alt="" />');

        $('#menu').css({
            left:-250
        });
        $('#menu').animate({
            left:0
        },500);
        
        /*
$('#logo').css({ top:-250 });
$('#logo').animate({ top:25 },500);
$('#jura').css({ top:-250 });
$('#jura').animate({ top:25 },500);
         */
       
        $('#photo'+(dep1+1)).css({
            top:pos2, 
            opacity:0.99, 
            right:0-largeur*2
        });
        $('#photo'+(dep1+1)).animate({
            top:pos1, 
            right:0
        },500);
  
        $('#photo'+(dep2+1)).css({
            top:pos2, 
            right:0-largeur*2, 
            opacity:0.99
        });
        $('#photo'+(dep2+1)).animate({
            top:pos2, 
            right:0
        },500);
      
        $('#photo'+(dep3+1)).css({
            top:pos3, 
            opacity:0.99, 
            right:0-largeur*2
        });
        $('#photo'+(dep3+1)).animate({
            top:pos3, 
            right:0
        },500);
    
        $('#photo'+(dep4+1)).css({
            top:pos3, 
            opacity:0.99, 
            right:0-largeur*2
        });
        $('#photo'+(dep4+1)).animate({
            top:pos4, 
            right:0
        },500);
        /*  Fin départ */



        function rano(){  
            t=t+1

            /* Val  */
            Val1 =  $.randomBetween(0,nbrc);   
                
                if(t==1){
                    while(Val1==dep1 ||Val1==dep2|| Val1==dep3|| Val1==dep4 ){
                        Val1 =  $.randomBetween(0,nbrc);
                    }
                }
                
                if(t==2){
                    while(Val1==dep1 ||Val1==dep2|| Val1==dep3|| Val1==dep4 ){
                        Val1 =  $.randomBetween(0,nbrc);
                    }
                }    
    
                    if(t==3){
                        while(Val1==dep1 ||Val1==dep2|| Val1==dep3 || Val1==dep4){
                            Val1 =  $.randomBetween(0,nbrc)
                        }
                    }
                    
                        if(t==4){
                            while(Val1==dep1 ||Val1==dep2|| Val1==dep3|| Val1==dep4  ){
                                Val1 =  $.randomBetween(0,nbrc);
                            }
                        }         
                        
                            Val2 =  $.randomBetween(0,nbrc);
                            while (Val2 == Val1 ){
                                Val2 =  $.randomBetween(0,2);
                            }
        
                            if(t==1){
                                while(Val2==dep1 ||Val2==dep2|| Val2==dep3|| Val2==dep4  ){
                                    Val2 =  $.randomBetween(0,nbrc);
                                    while (Val2 == Val1 ){
                                        Val2 =  $.randomBetween(0,2);
                                    }
                                }
                            }

                            if(t==2){
                                while(Val2==dep1 ||Val2==dep2|| Val2==dep3|| Val2==dep4  ){
                                    Val2 =  $.randomBetween(0,nbrc);
                                    while (Val2 == Val1 ){
                                        Val2 =  $.randomBetween(0,2);
                                    }
                                }
                            }

                            if(t==3){
                                while(Val2==dep1 ||Val2==dep2|| Val2==dep3|| Val2==dep4  ){
                                    Val2 =  $.randomBetween(0,nbrc);
                                    while (Val2 == Val1 ){
                                        Val2 =  $.randomBetween(0,2);
                                    }
                                }
                            }

                            if(t==4){
                                while(Val2==dep1 ||Val2==dep1 ){
                                    Val2 =  $.randomBetween(0,nbrc);
                                    while (Val2 == Val1 ){
                                        Val2 =  $.randomBetween(0,2);
                                    }
                                }   
                            }
                

                            function imp(){
                                while(num1==Val1){
                                    Val1 =  $.randomBetween(0,nbrc);
                                }
                                while(num2==Val1){
                                    Val1 =  $.randomBetween(0,nbrc);
                                }
                                while(num3==Val1){
                                    Val1 =  $.randomBetween(0,nbrc);
                                }
                                while(num1==Val2){
                                    Val2 =  $.randomBetween(0,nbrc);
                                }
                                while(num2==Val2){
                                    Val2 =  $.randomBetween(0,nbrc);
                                }
                                while(num3==Val2){
                                    Val2 =  $.randomBetween(0,nbrc);
                                }

                            }

                            /* Fin Val */
  
 

                            /* Valp  */

                            Valp1 =  $.randomBetween(0,3);
                            while(Valp1==Valp3){
                                Valp1 =  $.randomBetween(0,3);
                            }
                            if(Valp1==0){
                                number1=pos1
                            }
                            if(Valp1==1){
                                number1=pos2
                            }
                            if(Valp1==2){
                                number1=pos3
                            }
                            if(Valp1==3){
                                number1=pos4
                            }

                            Valp2 = $.randomBetween(0,3);
                            while(Valp2==Valp1){
                                Valp2 =  $.randomBetween(0,3);
                            }
                            if(Valp2==0){
                                number2=pos1
                            }
                            if(Valp2==1){
                                number2=pos2
                            }
                            if(Valp2==2){
                                number2=pos3
                            }
                            if(Valp2==3){
                                number2=pos4
                            }



                            if(number1 == pos1|| number2 == pos1 || number3 == pos1){
                                $tempo=$tbase/2
                            }
                            if(number1 == pos2|| number2 == pos2 || number3 == pos2){
                                $tempo=$tbase/1.75
                            }
                            if(number1 == pos3|| number2 == pos3 || number3 == pos3){
                                $tempo=$tbase/1.5
                            }
                            if(number1 == pos4|| number2 == pos4 || number3 == pos4){
                                $tempo=$tbase/1.25
                            }

                            function rand(){
                                z=z+1

                                imp();

 
                                var cont = $('#photo'+(Val1+1)).html();

                                if(cont=="" || cont==" "|| cont=="  "){
                                    $('#photo'+(Val1+1)).html('<img src="photos/('+(Val1+1)+').jpg" alt="" />')
    
                                }
                                $('#photo'+(Val1+1)).css('z-index',z);   
                                $('#photo'+(Val1+1)).css({
                                    top:$top, 
                                    opacity:$op
                                });   
                                $('#photo'+(Val1+1)).animate({
                                    top: number1, 
                                    opacity:0.99
                                },$tempo,"linear", rand2);   

 
                                PosTop1 = number1
                                num1 = Val1
                            }

                            function rand2(){
                                imp();
        
                                var cont = $('#photo'+(Val2+1)).html();

                                if(cont==""){
                                    $('#photo'+(Val2+1)).html('<img src="photos/('+(Val2+1)+').jpg" alt="" />')
    
                                }  


                                z=z+1
                                $('#photo'+(Val2+1)).css('z-index',z);   
                                $('#photo'+(Val2+1)).css({
                                    bottom:$top, 
                                    opacity:$op
                                });   
                                $('#photo'+(Val2+1)).animate({
                                    top: number2, 
                                    opacity:0.99
                                },$tempo,"linear", rano);


                                PosTop2 = number2
                                num2 =Val2

                            }

 
                            rand();

                        }
                        rano();

                    }
    
                    decla();

    
                });
