var slider_value_image_actu = 160;

function image_actus_load() 
{
  
  update_multiple_image_actu_selection_text();

  set_image_actus_img_size();

  initialize_slider_image_actu();
  

}

function set_image_actus_img_size(){
  $('.image_actu .img').css('width',slider_value_image_actu+"px");
  $('.image_actu .img').css('height',slider_value_image_actu+"px");
  
}

function update_multiple_image_actu_selection_text()
{
  
  var multiple_ids = multiple_image_actu_selection_ids()
  var text = ""
  if (multiple_ids.length == 0)
    {
      text = "Aucune image séléctionnée";
    }
  else if(multiple_ids.length == 1)
    {
      text= "Une image séléctionnée";
    }
  else
    {
      text= multiple_ids.length+" images séléctionnées";  
    }
  
  
  $('#multiple_selection_text').html(text);
  
}

function multiple_image_actu_selection_ids()
{
  var multiple_image_actu_selection_ids = []
  
  $('#image_actus .active').each(function ()
    {
      multiple_image_actu_selection_ids.push($(this).attr("data_id"))
    });
  
  return multiple_image_actu_selection_ids
}

function close_image_actu_container(image_actu_id)
{
  $('#image_actu_container').removeClass("image_actu_container_active");
  $('#image_actus_container').css("overflow", "");
  $('#right_bar').show();

}



function delete_multiple_image_actus(){
  if(confirm("Voulez-vous vraiment supprimer ces images ?"))
  {

    var multiple_ids = multiple_image_actu_selection_ids();
    $.each(multiple_ids, function(index, value) 
      { 
        $.ajax({
          url:"/admin/image_actus/"+value+".js", 
          type : "DELETE",
          success : function (){
            update_multiple_image_actu_selection_text();
            }
        });
      }
    );

    return false;

  }
  
}

function select_all_image_actu(){
  $('#image_actus .image_actu').each(function (){
    
    
    $(this).addClass("active");
    
    
  });
  update_multiple_image_actu_selection_text();
  
}

function unselect_all_image_actu(){
  
  $('#image_actus .image_actu').each(function (){
    
    
    $(this).removeClass("active");
    
    
  });
  update_multiple_image_actu_selection_text();
}


function initialize_slider_image_actu(){
  
  $("#grid_slider").slider({
    value: slider_value_image_actu,
    max: 250,
    min: 80,
    tooltip:"hide"
  }).on("slide", function(ev) 
  {      
    slider_value_image_actu = ev.value
    set_image_actus_img_size();
  });
  set_image_actus_img_size();

}

$(document).on("dblclick",".image_actu", function (event){
  

  $.ajax({url : $(this).attr('data_show_url'), success: function (){
    $('#image_actu_container').addClass("image_actu_container_active");
    $('#right_bar').hide();
    
  }});
  

});

$(document).on("click","#image_actus_container", function (e){
  
  

  
  if ($(e.target).parents(".image_actu").length == 0){
    unselect_all_image_actu();
    
  }

});

$(document).on("dblclick","#image_actu_container .img", function (){
  close_image_actu_container();
  
});

$(document).on("click",".image_actu", function (){

  if ($(this).hasClass('active')){
    $(this).removeClass('active');
    
  }
  else
  {
    $(this).addClass('active');
  }
  update_multiple_image_actu_selection_text();
  
});

$(document).ready(function ($) {

  $("#left_buttons").on("click", function() {
    multiple_image_actu_selection_ids();
  })

  $('#image_actus_big_container #main_workspace_view #image_actu_container .form input').on('keydown', function(){
    $('#image_actus_big_container #main_workspace_view #image_actu_container .form .submit_tr').show();
  })
  $('#image_actus_big_container #main_workspace_view #image_actu_container .form textarea').on('keydown', function(){
    $('#image_actus_big_container #main_workspace_view #image_actu_container .form .submit_tr').show();
  })
  
  initialize_slider_image_actu();

  image_actus_load();


});