var manager_response = "";
var manager_callback_function = null;

function manager_load() {

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

	
}

function manager_show(url){
	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();
			data_files_load();
				initialize_uploads();
				unset_busy();
		}
			)
		

	});
	



	
	
}


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

	
	
}
$(document).ready(function ($) {
	manager_load();
	
	$('#test_manager').live('click', function (){
		
	
		manager_prompt(function(m_return){alert(m_return);});
		

		
		
	});
	
	
	
	
});



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){
		set_busy();
	manager_prompt("/admin/image_files/?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 select_gallery_images_from_manager(gallery_content_id){
	
	manager_prompt("/admin/image_files/?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 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_data_file(data_file_id){


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