var slider_value = null;
var slider_enabled = null;
function image_files_load() 
{
	
	update_multiple_selection_text();
	set_image_files_big_container_size();
	set_image_files_img_size();

	if(!slider_enabled){
			initialize_slider();
		
	}
	
	

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

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

		}
	});
	
	
	


}

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 set_image_files_big_container_size(){
	
	if($("#image_files_big_container").parent().attr("id") == "manager_box")
		{
			$("#image_files_big_container").css("height",( $("#manager_box").height())+"px");
			$("#image_files_big_container").css("top","0px");
		}
	else
		{
			$("#image_files_big_container").css("height",( $(window).height() - 36)+"px");
			//alert($("#breadcrumb").outerHeight());
		}
}


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: $('.image_file .img').width(),
max: 250,
min: 80,
slide: 
	function(event, ui) 
		{
			slider_value = ui.value
			set_image_files_img_size();
		},
create : 
	function(event, ui) 
		{
			slider_enabled = true
		}

});
}

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


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


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

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

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

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



	$('#image_files_big_container #main_workspace_view #image_file_container .form input').live('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').live('keydown', function(){
		$('#image_files_big_container #main_workspace_view #image_file_container .form .submit_tr').show();
	})

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

	
	initialize_slider();

	

	image_files_load();
	


});