var Links = {
	
	//----------------------------------------------------------
	// Initialize
	//
	
	Initialize: function( deleteURL )
	{
		this.deleteURL = deleteURL;
		this.createSortables( );
		this.createTrash( );
		this.update( );
	},

	//----------------------------------------------------------
	// Delete a link
	//
	
	delete: function( ID )
	{
		$.get( this.deleteURL + '/' + ID );
	},
	
	//----------------------------------------------------------
	// Create sortable elements
	//
	
	createSortables: function( )
	{
		$( '#links' ).sortable(
		{	
			revert: 'invalid',
			revertDuration: 250,
			opacity: 0.5,
			distance: 10,
			connectWith: '#trash',
			cancel: 'a.action',
			update: this.update
		} ).disableSelection( );
	},
	
	//----------------------------------------------------------
	// Update the form field
	//
	
	update: function( )
	{
		var order = [ ];
		$( '#links li' ).each( function( ) { order.push( $( this ).attr( 'linkid' ) ); } );
		$( '#LinkOrder' ).val( order.join( ',' ) );		
	},
	
	//----------------------------------------------------------
	// Create the trash bin
	//
	
	createTrash: function( )
	{
		$( '#trash' ).droppable(
		{
			tolerance: 'pointer',
			hoverClass: 'hover',
			accept: '#links li',
			activate: function( ) { $( this ).stop( true ).fadeTo( 500, 1 ) },
			deactivate: function( ) { $( this ).stop( true ).fadeTo( 500, 0.2 ) },
			drop: function( e, ui )
			{
				Links.delete( ui.draggable.attr( 'linkid' ) );
				$( ui.draggable ).remove( );
				$( this ).stop( true ).fadeTo( 250, 0.2 );
			}
		} );
	}
	
};
