function data_files_load() 
{
	
	update_data_multiple_selection_text();
	set_data_files_big_container_size();


	
	
	
	
	$( ".data_file" ).draggable({
		revert:"invalid",

		opacity: 0.5,
		helper: 'clone',
		appendTo: '#main_workspace_view',
		start: function(event, ui) { 
		
		},
		drag: function(){
		},
		stop: function(){
			
		}
	});
	
	
	$(".file_folder:not(.active)").droppable({
		hoverClass:"album_highlight",
		tolerance : "pointer",
	
		drop: function( event, ui ) {
			ui.draggable.fadeOut().remove();
			
	
			$.ajax({
				url:ui.draggable.data('show_url'),
				type: "PUT",
				data: { 
					data_file : {
						file_folder_id : $(this).data("file_folder_id")
					} 
				}
			});
			$( this ).effect("highlight", {}, 3000);

		}
	});
	
	
	


}



function update_data_multiple_selection_text()
{
	
	var multiple_ids = multiple_data_selection_ids()
	var text = ""
	if (multiple_ids.length == 0)
		{
			text = "Aucun fichier séléctionné";
		}
	else if(multiple_ids.length == 1)
		{
			text= "Un fichier séléctionné";
		}
	else
		{
			text= multiple_ids.length+" fichiers séléctionnés";	
		}
	
	
	$('#data_files_multiple_selection_text').html(text);
	
}

function multiple_data_selection_ids()
{
	var multiple_selection_ids = []
	
	$('#data_files .active').each(function ()
		{
			multiple_selection_ids.push($(this).data("id"))
		
		});
		
	return multiple_selection_ids
	
}

function close_data_file_container(data_file_id)
{
	$('#data_file_container').removeClass("data_file_container_active");
	$('#data_files_container').css("overflow", "");
	$('#right_bar').show();

}

function set_data_files_big_container_size(){
	
	if($("#data_files_big_container").parent().attr("id") == "manager_box")
		{
			$("#data_files_big_container").css("height",( $("#manager_box").height())+"px");
			$("#data_files_big_container").css("top","0px");
		}
	else
		{
			$("#data_files_big_container").css("height",( $(window).height() - 36)+"px");
		}
}


function delete_multiple_files(){
	if(confirm("Voulez-vous vraiment supprimer ces fichiers ?"))
	{

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

		return false;

	}
	
}

function select_all_files(){
	$('#data_files .data_file').each(function (){
		
		
		$(this).addClass("active");
		
		
	});
	update_data_multiple_selection_text();
	
}

function unselect_all_files(){
	
	$('#data_files .data_file').each(function (){
		
		
		$(this).removeClass("active");
		
		
	});
	update_data_multiple_selection_text();
}

function initialize_slider(){
$("#grid_slider").slider({
value: $('.data_file .img').width(),
max: 250,
min: 80,
slide: 
	function(event, ui) 
		{
			slider_value = ui.value
			set_data_files_img_size();
		},
create : 
	function(event, ui) 
		{
			slider_enabled = true
		}

});
}

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


	$(window).bind("resize", function() {
		set_data_files_big_container_size();
	})


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

	$(".data_file").live('dblclick', function (event){
		
	
		$.ajax({url : $(this).data('show_url'), success: function (){
			$('#data_file_container').addClass("data_file_container_active");
			$('#right_bar').hide();
	
			
			
			
			
		}});
		

		
		
		
	
	});
	
	$("#data_files_container").live("click",function (e){
		
		
	
		
		if ($(e.target).parents(".data_file").length == 0){
			unselect_all_image();
			
		}
	
	});

	$("#data_file_container .img").live("dblclick",function (){
		close_data_file_container();
		
	});



	$('#data_files_big_container #main_workspace_view #data_file_container .form input').live('keydown', function(){
		$('#data_files_big_container #main_workspace_view #data_file_container .form .submit_tr').show();
	})
	$('#data_files_big_container #main_workspace_view #data_file_container .form textarea').live('keydown', function(){
		$('#data_files_big_container #main_workspace_view #data_file_container .form .submit_tr').show();
	})

	$(".data_file").live("click",function (){
		if ($(this).hasClass('active')){
			$(this).removeClass('active');
			
		}
		else
		{
			$(this).addClass('active');
		}

		update_data_multiple_selection_text();
		
	});
	
	set_data_files_big_container_size();
	

	
	initialize_slider();

	

	data_files_load();
	
	



});