function uninitialize_block_js(){
	
	$( ".portlet" ).draggable('disable');
	
	
}


function block_js_initialize(){
	
	initialize_receptables();
	
	$( ".content_types_draggable" ).draggable({
		revert:"invalid",
		//handle:".grip",
		connectToSortable: ".block_portlets",
		opacity: 0.5,
		zIndex:1000,

		helper: 'clone',

	});
	

	$('.portlet').live('hover', function(){
	
		
			

		//$('.portlet').removeClass('portlet_hover');
		$(this).addClass('portlet_hover');
		
		$(this).parents('.portlet').children(".actions").css({'display':'none'});
		
		
	});
	
	$('.portlet').live('mouseleave', function(){
		
		$(this).removeClass('portlet_hover');
		
		
		
		$(this).parents('.portlet').children(".actions").css('display','');
		$(this).parents('.portlet').css({ "border" : ""});
		
		
		
	});
	
	



	

}


function update_portlet_order(){
	var return_order = [];
	

	
	$('.block_portlets').each(function(){
		
		var block_data = {block_id : $(this).data('block_id'), block_portlet_ids : []};
		
		$(this).children('.portlet').each(function(){
			if($(this).data('portlet_id')){
				block_data.block_portlet_ids.push($(this).data('portlet_id'));
				}
			
		});
		return_order.push(block_data);
	});


	$.ajax({url : "/portlet/portlets/reorder", data : {blocks:return_order}, type: "POST"});
	
}


function initialize_receptables(){

	
	$('.portlet_place_holder').remove();
	$('.block_portlets').prepend('<div class="portlet_place_holder"></div>');
	$('.portlet').after('<div class="portlet_place_holder"></div>');
	
	$( ".portlet" ).draggable({
		revert:"invalid",
		handle:".move",
		zIndex:600,
		opacity: 0.5,
		appendTo:"body",
		scrollSensitivity : 20,
		scrollSpeed : 40,
		disabled : false



	});
	
	
	$(".portlet_place_holder").droppable({
		hoverClass:"portlet_receptable_hover",
		tolerance : "pointer",
		accept: '.portlet, .content_types_draggable' ,
		over: function(event, ui) { 
		
			$(this).attr("data_height", $(this).height());
			$(this).css("height", ui.draggable.attr("data_height"))
		
			},
			
		out: function(event, ui) { 
			$(this).css("height", $(this).attr("data_height"))
			
			},
		
		drop: function( event, ui ) {
			
			
		
			
			if(ui.draggable.data("portlet_id")){
				
				$(this).after(ui.draggable);
				ui.draggable.removeClass("ui-draggable");
				ui.draggable.removeClass("ui-draggable-dragging");
				ui.draggable.css({"top":"", "left":""});
				initialize_receptables();
				
				update_portlet_order();
				
	
			}
			else
			{
				
			
				$(this).after('<div id="new_portlet_form_inline" class="portlet"><div id="new_portlet_content_form"></div></div>');
				initialize_receptables();
				$.ajax({
					url:"/portlet/portlets/new.js", 
					type: "GET",
					data: { 
						block_id : $(this).attr("data_block_id"),
						position : $(this).attr("data_position"),
						content_type : ui.draggable.attr("data_type")
						} ,
					success : function (){
						
					}
				});
				
				
			}
			
			

		}
	});
	
	
	
}