$(document).ready(function(){
  $("div#petites_photos a").wrapAll('<div id="pagination_photo"></div>');
  
  function animation(donnee_2,bt_select)
  {
    $("#pagination_photo").css({position:"relative",width:$('div#petites_photos').css('width')});
    //On met un loader au passage de page
    $('<img src="img/ajax-loader.gif" alt="loader" id="loader" />').appendTo('.pagination');
    $(".next,.prev").css({display:"none"});
    $(".pagination").css({background: "white",opacity:0.2,'text-align':'center'});
    if(bt_select == 'next'){$("#pagination_photo").animate({right: "190px"}, 800 );}
    if(bt_select == 'prev'){$("#pagination_photo").animate({left: "190px"}, 800 );}
    $("#pagination_photo").animate( { opacity: 0} , 300, function(){
      $(this).remove();
      $(donnee_2).appendTo('div#petites_photos').fadeIn(800,function(){
          $('#loader').remove();
          $(".pagination").css({background: "#fff",opacity:1,'text-align':'left'});
          $(".next,.prev").css({display:""});
      });  
    });     
  }

  $('a[id^=suivant],a[id^=precedent]').live('click',function(){
    var page = $(this).attr('id');
    var page_choisi = $(this).attr('class');
    
   $.post("pagination.php",
      {nbr_img: $('#petites_photos').attr('ref'),page:page},
      function(data){
        var donnee = String(data).split('|');
          
        $('a[id^=suivant]').attr('id','suivant_'+donnee['1']);
        $('a[id^=precedent]').attr('id','precedent_'+donnee['3']);
        
        if((parseInt(donnee['0'])+1) == donnee['1']){ 
          $('a[id^=suivant]').remove();
        }else if(parseInt(donnee['0']) == donnee['1']){
          $('a[id^=suivant]').remove();
          $('<a href="#" id="suivant_'+donnee['1']+'" class="next"><img src="./img/next_arrow.png"></a>').appendTo('.pagination');
        }
                 
        if((donnee['3'] == '0'))
        {
          $('a[id^=precedent]').remove();
          $('a[id^=suivant]').remove();  
          $('<a href="#" id="suivant_2" class="next"><img src="./img/next_arrow.png"></a>').appendTo('.pagination');
        }
        else
        {
          $('a[id^=precedent]').remove();
          if($('.next').attr('id')){
            $('.next').before('<a href="#" id="precedent_'+donnee['3']+'" class="prev"><img src="./img/prev_arrow.png"></a>');
          }else{
            $('<a href="#" id="precedent_'+donnee['3']+'" class="prev"><img src="./img/prev_arrow.png"></a>').appendTo('.pagination');
          }         
        }
         
        animation(donnee['2'],page_choisi);
      }
    );    
    
    return false;
  });
});

