var slider_value = 160;

function image_files_load() 
{
  
  update_multiple_selection_text();

  set_image_files_img_size();

  initialize_slider();
  

}

function set_image_files_img_size(){
  $('.image_file .img').css('width',slider_value+"px");
  $('.image_file .img').css('height',slider_value+"px");
  
}

function update_multiple_selection_text()
{
  
  var multiple_ids = multiple_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_selection_ids()
{
  var multiple_selection_ids = []
  
  $('#image_files .active').each(function ()
    {
      multiple_selection_ids.push($(this).attr("data_id"))
    });
  
  return multiple_selection_ids
}

function close_image_file_container(image_file_id)
{
  $('#image_file_container').removeClass("image_file_container_active");
  $('#image_files_container').css("overflow", "");
  $('#right_bar').show();

}



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

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

    return false;

  }
  
}

function select_all_image(){
  $('#image_files .image_file').each(function (){
    
    
    $(this).addClass("active");
    
    
  });
  update_multiple_selection_text();
  
}

function unselect_all_image(){
  
  $('#image_files .image_file').each(function (){
    
    
    $(this).removeClass("active");
    
    
  });
  update_multiple_selection_text();
}


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

}

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

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

});

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

  
  if ($(e.target).parents(".image_file").length == 0){
    unselect_all_image();
    
  }

});

$(document).on("dblclick","#image_file_container .img", function (){
  close_image_file_container();
  
});

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

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

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

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

  $('#image_files_big_container #main_workspace_view #image_file_container .form input').on('keydown', function(){
    $('#image_files_big_container #main_workspace_view #image_file_container .form .submit_tr').show();
  })
  $('#image_files_big_container #main_workspace_view #image_file_container .form textarea').on('keydown', function(){
    $('#image_files_big_container #main_workspace_view #image_file_container .form .submit_tr').show();
  })
  
  initialize_slider();

  image_files_load();


});