var manager_response = "";
var manager_callback_function = null;

function manager_load() {

  
  $(document).bind("keydown",function(e) {
      if (e.keyCode == 27) {
          manager_hide();
      }
  });
  

  
}

function manager_show(url){
  
  initialize_manager();
  
  manager_response = null;
  $('#manager_box').html("");
  
  $("#manager_box_place").show("fast", function (){
    $("#manager_box_place").addClass("manager_box_place_active");
    $('#manager_box').load(url, function (){
      image_files_load();
        
    }
      )
    

  });
  



  
  
}


function manager_hide(){
  $("#manager_box_place").removeClass("manager_box_place_active");
  
    $("body").css("overflow", "auto");
    slider_enabled = null;

  
  
}
$(document).ready(function ($) {
  manager_load();
  
  
  
  
});



function manager_prompt(url, callback) {
  manager_callback_function = callback;
  manager_show(url);
}

function manager_send_response(send_value){

  manager_hide();
  manager_callback_function(send_value);


}

function select_image_from_manager(input_id){

  manager_prompt("/admin/image_files.html?manager=true",function(m_return){

    $('#input_'+input_id).val(m_return.image_file_id);
    $('#img_'+input_id).attr("src",m_return.thumb);
    $('#name_'+input_id).val(m_return.name);
    
  });
  
}


function remove_image_cible(input_id){


    $('#input_'+input_id).val("");
    $('#img_'+input_id).attr("src","/default_image.png");
    $('#name_'+input_id).val("");
    


}



function select_gallery_images_from_manager(gallery_content_id){
  
  manager_prompt("/admin/image_files.html?manager=true&multiple=true",function(m_return){

    $.ajax({url:"/portlet/gallery_images/", type: "POST", data : { image_file_ids : m_return, gallery_content_id : gallery_content_id }});

    
  });
  
}



function select_file_from_manager(input_id){
  manager_prompt("/admin/data_files/?manager=true",function(m_return){

    $('#input_'+input_id).val(m_return.data_file_id);
    
    $('#name_'+input_id).html(m_return.name);
    
  });
  
}


function select_product_images_from_manager(p_product_id){
  
  manager_prompt("/admin/image_files.html?manager=true&multiple=true",function(m_return){

    $.ajax({url:"/admin/p_product_images/", type: "POST", data : { image_file_ids : m_return, p_product_id : p_product_id }});

    
  });
  
}


function select_portfolio_item_images_from_manager(portfolio_item_id){
  
  manager_prompt("/admin/image_files.html?manager=true&multiple=true",function(m_return){

    $.ajax({url:"/admin/portfolio_item_images/", type: "POST", data : { image_file_ids : m_return, portfolio_item_id : portfolio_item_id }});

    
  });
  
}

function select_new_label_gamme_images_from_manager(new_label_gamme_id){
  
  manager_prompt("/admin/image_files/?manager=true&multiple=true",function(m_return){

    $.ajax({url:"/admin/new_label_gamme_images/", type: "POST", data : { image_file_ids : m_return, new_label_gamme_id : new_label_gamme_id }});

    
  });
  
}


function manager_send_multiple_image_files(){

  
  
  manager_send_response(multiple_selection_ids());
}



function manager_send_image_file(image_file_id){


  var image_file_div = $('#image_file_'+image_file_id);
  
  manager_send_response({thumb : image_file_div.attr("data_thumb"), image_file_id : image_file_div.attr("data_id"), name : image_file_div.attr("data_name")});
}

function manager_send_cible(cible_id, cible_type, cible_name){
  
  manager_send_response({cible_id : cible_id, cible_type : cible_type, cible_name : cible_name});
}


function manager_send_data_file(data_file_id){


  var data_file_div = $('#data_file_'+data_file_id);
  
  manager_send_response({data_file_id : data_file_div.data("id"), name : data_file_div.data("name")});
}

function select_cible_from_manager(input_id){
    

    manager_prompt("/admin/cibles.html?manager=true",function(m_return){
  
    $('#input_id_'+input_id).val(m_return.cible_id);
    $('#input_type_'+input_id).val(m_return.cible_type);
    $('#name_'+input_id).val(m_return.cible_name);
    
  });
  
}





function initialize_manager(){

  if($('#manager_box_place').length == 0){

    $('body').prepend('<div id="manager_box_place"><div id="manager_box"></div></div>');
    
  }  
  

  
  
  
}



function manager_send_video_file(video_file_id){
    var video_file_div = $('#video_file_'+video_file_id);
  manager_send_response({thumb : video_file_div.attr("data_thumb"), video_file_id : video_file_div.attr("data-id"), name : video_file_div.attr("data_name")});
}


function select_video_from_manager(input_id){
    manager_prompt("/admin/video_files/?manager=true",function(m_return){
      $('#input_'+input_id).val(m_return.video_file_id);
    $('#img_'+input_id).attr("src",m_return.thumb);
    $('#name_'+input_id).val(m_return.name);
    
  });
  
}


