$.fn.makeBaboumGallery = function (photos, delay, classe, random_start)
{
   var photos_array = photos;
   var effect_delay = delay;
   var photo_class = classe;
   var default_zindex = 200;
   var next_img_url = "/images/icons/panel-nav-next.gif";
   var prev_img_url = "/images/icons/panel-nav-prev.gif";
   var pointer_size = 42;
   var pause_on_over = true;

   var current_photo = null;
   var next_photo = null;
   var current_comment =null;
   var next_comment =null;
   var paused = false;

   var last_id = null;
   var current_id = null;
   var changing = false;
   var interval_id = null;
   var cache = [];
   var random_start = random_start;
   
   


   var gallery = $(this);




   //alert(photos_array[current_id].src);


   $.fn.getPhoto = function(id)
   {
    //alert(id)
    if(photos_array[id%photos_array.length].comment!="")
    {
      comment = "<div id='comment_"+id+"' class='baboum_gallery_comment'>"+photos_array[id%photos_array.length].comment+"</div>";
    }
    else
    {
      comment = "";
    }

    if(photos_array[id%photos_array.length].href!="")
    {
      href_open='<a href="'+photos_array[id%photos_array.length].href+'" target="'+photos_array[id%photos_array.length].target+'">';
      href_close='</a>';
    }
    else
    {
      href_open="";
      href_close="";
    }
    return $('<div id="photo_'+id+'" class="'+photo_class+'" >'+href_open+'<img class="baboum_gallery_img" src="'+photos_array[id%photos_array.length].src+'" >'+href_close+comment+'</div>').css('z-index', default_zindex)
   }

   $.fn.nextPhoto = function()
   {
     if(!changing && !paused)
     {
       last_id = current_id
       current_id++;
       $(this).changePhoto();
     }

     return false;
   }

   $.fn.forceNextPhoto = function()
   {
      var save_paused = paused
      paused = false;
      gallery.nextPhoto();
      paused = save_paused;
   }



   $.fn.prevPhoto = function()
   {
     if(!changing && !paused)
     {
       last_id = current_id
       current_id--;

       $(this).changePhoto();
     }

     return false;
   }

   $.fn.forcePrevPhoto = function()
   {
      var save_paused = paused
      paused = false;
      gallery.prevPhoto();
      paused = save_paused;
   }


   $.fn.changePhoto = function()
   {
     if(current_id == 0)
     {
       current_id = photos_array.length;
     }
     changing= true;

     // cache de la photo encore suivante
     gallery.setOnCache((current_id+1)%photos_array.length);
     gallery.setOnCache((current_id+2)%photos_array.length);

     // Changement de photo
     next_photo = gallery.getPhoto(current_id);
     next_photo.hide();
     gallery.append(next_photo);

     current_photo.fadeOut(effect_delay, function()
      {

      });
     current_comment = $('#comment_'+last_id);
     current_comment.fadeOut(effect_delay);


     next_comment = $('#comment_'+current_id);
     $(next_comment).width($(next_photo).width())


     next_comment.hide();

     var photo_to_remove = $('#photo_'+last_id);
     next_photo.fadeIn(effect_delay, function()
      {
        photo_to_remove.remove();
        current_photo = next_photo;
        current_comment = next_comment;

        current_comment.fadeIn(effect_delay);
        changing = false;

      });



   }
   //if(random_start==undefined)
   //{
    current_id = Math.floor(Math.random() * photos_array.length)+1;
   //}
   //else
   //{
    current_id = 1;
   //}

   last_id = current_id-1;

   ////// INIT PHOTOS ajout des photos d'inirialisation'

   last_photo = $(this).getPhoto(last_id);
   //last_photo.hide();
   //$(this).append(last_photo);
   current_photo = $(this).getPhoto(current_id);
   $(this).append(current_photo);





   /// INIT POINTER

   pointer_height = pointer_size;
   pointer_width = pointer_size;

   var pointer_top = $(this).height()/2- pointer_height/2;

   next_img_btn = $('<img height="'+pointer_height+'" width="'+pointer_width+'" src="'+next_img_url+'" >').css('z-index', default_zindex+2);
   $(next_img_btn).css('position', 'absolute').css('top', pointer_top+'px').css('right', '0px').css('cursor', 'pointer');
   $(next_img_btn).css('opacity', .5);
   $(next_img_btn).bind('click', function(){$(this).forceNextPhoto()});
   $(next_img_btn).hide()
   $(this).bind('mouseenter', function(){next_img_btn.fadeIn()})
   $(this).bind('mouseleave', function(){next_img_btn.fadeOut()})
   $(this).append(next_img_btn);


   prev_img_btn = $('<img height="'+pointer_height+'" width="'+pointer_width+'" src="'+prev_img_url+'" >').css('z-index', default_zindex+2);
   $(prev_img_btn).css('position', 'absolute').css('top', pointer_top+'px').css('left', '0px').css('cursor', 'pointer');;
   $(prev_img_btn).css('opacity', .5);
   $(prev_img_btn).bind('click', function(){$(this).forcePrevPhoto()});
   $(prev_img_btn).hide()

   $(this).append(prev_img_btn);

   
     /// CACHE PHOTOS
    for (var i = current_id+1; i<current_id+2+photos_array.length; i++)
    {


    }
    
   $.fn.setOnCache  = function(photo_id)
   {
     if(!cache[photo_id])
     {
      var cacheImage = document.createElement('img');
      //console.log(photo_id)
      cacheImage.src = photos_array[photo_id].src;

      //console.log("Mise en cache de la photo: "+photos_array[photo_id].src)
      cache[photo_id] = cacheImage;
     }

   }

   /// INIT FUCNTIONS

   $.fn.onOver = function()
   {

    prev_img_btn.fadeIn()
    if(pause_on_over)
    {
      paused = true;
    }
    $(current_comment).fadeOut()

   }

   $.fn.onOut = function()
   {
    prev_img_btn.fadeOut()
    if(pause_on_over)
    {
      paused = false;
    }
    $(current_comment).fadeIn()
   }

    $.fn.start = function(trigger_delay)
    {
      if(interval_id!=null)
      {
        clearInterval(interval_id);
      }
      if(trigger_delay<2*effect_delay)
      {
        trigger_delay = 2*effect_delay
      }
      interval_id = setInterval(gallery.nextPhoto, trigger_delay)


      return false;
    }

    $.fn.stop = function()
    {
      if(interval_id!=null)
      {
        clearInterval(interval_id);
      }
      return false;
    }



   $(this).bind('mouseenter', function(){gallery.onOver()})
   $(this).bind('mouseleave', function(){gallery.onOut()})



}
