--- form_builder.js	2011-01-20 00:00:17.000000000 -0500
+++ form_builder.js	2011-01-07 14:15:56.000000000 -0500
@@ -53,15 +53,15 @@ Drupal.behaviors.formBuilderElement.atta
 Drupal.behaviors.formBuilderFields = {};
 Drupal.behaviors.formBuilderFields.attach = function(context) {
   // Bind a function to all elements to update the preview on change.
-  var $configureForm = $('#form-builder-field-configure');
+  var $configureForm = $('#form-builder-field-configure', context);
 
   $configureForm.find('input, textarea, select')
-    .not('.form-builder-field-change)')
+    .not('.form-builder-field-change')
     .addClass('form-builder-field-change')
     .bind('change', Drupal.formBuilder.elementPendingChange);
 
   $configureForm.find('input.form-text, textarea')
-    .not('.form-builder-field-keyup)')
+    .not('.form-builder-field-keyup')
     .addClass('form-builder-field-keyup')
     .bind('keyup', Drupal.formBuilder.elementPendingChange);
 };
@@ -120,7 +120,7 @@ Drupal.behaviors.formBuilderTabs.attach 
   $fieldsets.filter(':first').before($close).before($tabs);
 
   // Hide all the fieldsets except the first.
-  $fieldsets.not(':first)').css('display', 'none');
+  $fieldsets.not(':first').css('display', 'none');
   $tabs.find('li:first').addClass('active').click(Drupal.formBuilder.clickCancel);
 
   // Enable tab switching by clicking on each tab.
@@ -149,14 +149,16 @@ Drupal.behaviors.formBuilderDeleteConfir
   if ($confirmForm.length) {
     $confirmForm.find('input[type=submit]').bind('click', function(event) {
       event.preventDefault();
+      // Store the form and the options
+      var form = $confirmForm;
       Drupal.formBuilder.ajaxOptions = {
-        url: $confirmForm.attr('action'),
+        url: form.attr('action'),
         success: Drupal.formBuilder.deleteField,
         error: Drupal.formBuilder.ajaxError,
         type: 'post',
         dataType: 'json',
-        errorMessage: Drupal.t('Field could not be deleted at this time.  Please try again later.'),
-        data: $confirmForm.serialize(),
+        action: 'deleteConfirmation',
+        data: form.serialize(),
         tryCount: 0,
         maxTry: 3
       };
@@ -188,7 +190,7 @@ Drupal.behaviors.formBuilderBlockScroll 
 Drupal.behaviors.formBuilderBlockScroll.attach = function(context) {
   var $list = $('ul.form-builder-fields', context);
 
-  if ($list.length) {
+  if ($list.length && $list.hasClass('block-scroll')) {
     var $block = $list.parents('div.block:first').css('position', 'relative');
     var blockScrollStart = $block.offset().top;
 
@@ -280,7 +282,9 @@ Drupal.formBuilder = {
   // replacing newer updates.
   lastUpdateTime: 0,
   // Status of mouse click.
-  mousePressed: 0
+  mousePressed: 0,
+  // Field configure form target
+  fieldConfigureHolder: false
 };
 
 /**
@@ -336,28 +340,43 @@ Drupal.formBuilder.disableField = functi
 /**
  * Load the edit form from the server.
  */
-Drupal.formBuilder.editField = function() {
+Drupal.formBuilder.editField = function(event) {
+  if (event && $(event.target).is('a')) {
+    event.stopPropagation();
+  }
   var element = $(this).parents('div.form-builder-wrapper').get(0);
-  var link = this;
+  var link = $(this);
 
   // Prevent duplicate clicks from taking effect if already handling a click.
   if (Drupal.formBuilder.updatingElement) {
     return false;
   }
 
+  link.addClass('progress');
+
   // If clicking on the link a second time, close the form instead of open.
-  if (element == Drupal.formBuilder.activeElement && link == Drupal.formBuilder.activeLink) {
-    $(link).addClass('progress');
+  if (element == Drupal.formBuilder.activeElement && link.get(0) == Drupal.formBuilder.activeLink) {
     Drupal.formBuilder.closeActive(function() {
-      $(link).removeClass('progress');
+      link.removeClass('progress');
+      if (Drupal.formBuilder.fieldConfigureForm) {
+        Drupal.formBuilder.fieldConfigureForm.html($('<div class="field-settings-message">' + Drupal.t('No field selected') + '</div>'));
+      }
     });
     Drupal.formBuilder.unsetActive();
     return false;
   }
 
+  if (!Drupal.formBuilder.fieldConfigureForm) {
+    $('<div id="#form-builder-field-configure" class="form-builder-field-configure"><div class="field-settings-message">' + Drupal.t('Loading...') + '</div></div>').appendTo(element);
+  } else {
+    $('.field-settings-message').remove();
+    Drupal.formBuilder.fieldConfigureForm.append($('<div class="field-settings-message">' + Drupal.t('Loading...') + '</div>'));
+  }
+  
+
   var getForm = function() {
     Drupal.formBuilder.ajaxOptions = {
-      url: link.href,
+      url: link.attr('href'),
       type: 'GET',
       dataType: 'json',
       data: 'js=1',
@@ -371,10 +390,9 @@ Drupal.formBuilder.editField = function(
     $.ajax(Drupal.formBuilder.ajaxOptions);
   };
 
-  $(link).addClass('progress');
   Drupal.formBuilder.updatingElement = true;
   Drupal.formBuilder.closeActive(getForm);
-  Drupal.formBuilder.setActive(element, link);
+  Drupal.formBuilder.setActive(element, link.get(0));
 
   return false;
 };
@@ -382,7 +400,7 @@ Drupal.formBuilder.editField = function(
 /**
  * Click handler for deleting a field.
  */
-Drupal.formBuilder.deleteField = function() {
+Drupal.formBuilder.deleteField = function(callback) {
   var active = $(Drupal.formBuilder.activeElement);
   // Renable form submission.
   $('form').unbind('submit', Drupal.formBuilder.preventSubmit);
@@ -392,8 +410,14 @@ Drupal.formBuilder.deleteField = functio
     $('ul.form-builder-fields').find('li.' + elementId).show('slow');
     // Remove the field from the form.
     active.remove();
+    // Close the form and unset the active element
+    Drupal.formBuilder.clickCancel();
     // Check for empty fieldsets.
     Drupal.formBuilder.checkFieldsets(null, null, true);
+
+    if (callback && $.isFunction(callback)) {
+      callback();
+    }
   });
 };
 
@@ -411,7 +435,15 @@ Drupal.formBuilder.displayForm = functio
     $.extend(true, Drupal.settings, response.settings);
   }
   var $preview = $('#form-builder-element-' + response.elementId);
-  var $form = $(response.html).insertAfter($preview).css('display', 'none');
+  var $form = $(response.html)
+  if (!Drupal.formBuilder.fieldConfigureForm) {
+    $('.form-builder-field-configure').html($form);
+    $form.css('display', 'none');
+  } else {
+    Drupal.formBuilder.fieldConfigureForm.html($form);
+    $form.css('visibility: hidden');
+  }
+  $('.field-settings-message').remove();
   Drupal.attachBehaviors($form.parent().get(0));
 
   $form
@@ -423,10 +455,13 @@ Drupal.formBuilder.displayForm = functio
     // Add in any messages from the server.
     .find('fieldset:visible:first').prepend(response.messages);
 
+  $form.css({visibility: 'visible', display: 'none'});
   $form.slideDown(function() {
-    $form.parents('div.form-builder-wrapper:first').find('a.progress').removeClass('progress');
+    $preview.parents('.form-builder-wrapper:first').find('a.progress').removeClass('progress');
   });
-  //Drupal.unfreezeHeight();
+
+  // Give focus to the form
+  $form.find('input:visible:first').focus();
 
   Drupal.formBuilder.updatingElement = false;
 };
@@ -437,7 +472,7 @@ Drupal.formBuilder.displayForm = functio
 Drupal.formBuilder.elementChange = function() {
   if (!Drupal.formBuilder.updatingElement) {
     // Store the form and the options
-    var form = $(this).closest('form');
+    var form = $(this).parents('form:first');
     Drupal.formBuilder.ajaxOptions = {
       url: form.attr('action'),
       success: Drupal.formBuilder.updateElement,
@@ -513,7 +548,7 @@ Drupal.formBuilder.elementPendingChange 
  * After submitting the change to the server, display the updated element.
  */
 Drupal.formBuilder.updateElement = function(response) {
-  var $configureForm = $('#form-builder-field-configure');
+  var $configureForm = $('.form-builder-field-configure');
 
   // Do not let older requests replace newer updates.
   if (response.time < Drupal.formBuilder.lastUpdateTime) {
@@ -546,7 +581,7 @@ Drupal.formBuilder.updateElement = funct
   // Do not update the element if errors were received.
   if (!response.errors) {
     var $exisiting = $('#form-builder-element-' + response.elementId);
-    var $new = $(response.html).find('div.form-builder-element:first');
+    var $new = $(response.html).find('.form-builder-element:first');
     $exisiting.replaceWith($new);
 
     // Expand root level fieldsets after updating to prevent them from closing
@@ -569,8 +604,8 @@ Drupal.formBuilder.addElement = function
   if (response.settings) {
     $.extend(true, Drupal.settings, response.settings);
   }
-  var $exisiting = $('#form-builder-element-' + response.elementId).parent();
-  var $new = $(response.html).find('div.form-builder-element:first').parent();
+  var $exisiting = $('.form-builder-new-field');
+  var $new = $(response.html);
   $exisiting.replaceWith($new);
   Drupal.attachBehaviors($new.get(0));
 
@@ -597,7 +632,7 @@ Drupal.formBuilder.addElement = function
   $('#form-builder-positions').replaceWith(response.positionForm);
 
   // Submit the new positions form to save the new element position.
-  Drupal.formBuilder.updateElementPosition($new.get(0));
+  Drupal.formBuilder.updateElementPosition($new);
 };
 
 /**
@@ -605,15 +640,15 @@ Drupal.formBuilder.addElement = function
  */
 Drupal.formBuilder.updateElementPosition = function(element) {
   // Update weights of all children within this element's parent.
-  $(element).parent().children('div.form-builder-wrapper').each(function(index) {
-    var child_id = $(this).children('div.form-builder-element:first').attr('id');
+  element.parent().children('.form-builder-wrapper').each(function(index) {
+    var child_id = $(this).children('.form-builder-element:first').attr('id');
     $('#form-builder-positions input.form-builder-weight').filter('.' + child_id).val(index);
   });
 
   // Update this element's parent.
-  var $parent = $(element).parents('div.form-builder-element:first');
+  var $parent = element.parents('.form-builder-element:first');
   var parent_id = $parent.length ? $parent.attr('id').replace(/form-builder-element-(.*)/, '$1') : 0;
-  var child_id = $(element).children('div.form-builder-element:first').attr('id');
+  var child_id = element.children('.form-builder-element:first').attr('id');
   $('#form-builder-positions input.form-builder-parent').filter('.' + child_id).val(parent_id);
 
   // Submit the position form via AJAX to save the new weights and parents.
@@ -621,18 +656,6 @@ Drupal.formBuilder.updateElementPosition
 };
 
 /**
- * Called when a field is about to be moved via Sortables.
- *
- * @param e
- *   The event object containing status information about the event.
- * @param ui
- *   The jQuery Sortables object containing information about the sortable.
- */
-Drupal.formBuilder.startDrag = function(e, ui) {
-  Drupal.formBuilder.activeDragUi = ui;
-};
-
-/**
  * Called when a field has been moved via Sortables.
  *
  * @param e
@@ -663,7 +686,7 @@ Drupal.formBuilder.drop = function(e, ui
     placeholder.replaceWith($ajaxPlaceholder);
 
     Drupal.formBuilder.ajaxOptions = {
-      url: $(element).find('a').attr('href'),
+      url: element.find('a').attr('href'),
       type: 'GET',
       dataType: 'json',
       data: 'js=1&element_id=' + name,
@@ -683,6 +706,9 @@ Drupal.formBuilder.drop = function(e, ui
     element.removeClass('original').show();
     ui.helper.remove();
     Drupal.formBuilder.updateElementPosition(element);
+
+    // Select the element
+    element.find('a.configure').click();
   }
 
   Drupal.formBuilder.activeDragUi = false;
@@ -794,13 +820,15 @@ Drupal.formBuilder.startDrag = function(
  *   The jQuery Sortables object containing information about the sortable.
  */
 Drupal.formBuilder.stopDrag = function(e, ui) {
+  var $this = $(this);
   // Remove the droppable from all elements within the form
   $('#form-builder .ui-droppable').droppable('destroy');
+
   // If the activeDragUi is still set, we did not drop onto the form.
   if (Drupal.formBuilder.activeDragUi) {
     ui.helper.remove();
     Drupal.formBuilder.activeDragUi = false;
-    $(this).css('visibility', '').show().removeClass('original');
+    $this.css('visibility', '').show().removeClass('original');
     $(window).scroll();
     // Remove the placeholders
     $('#form-builder .form-builder-placeholder').remove();
@@ -808,9 +836,9 @@ Drupal.formBuilder.stopDrag = function(e
     Drupal.formBuilder.checkFieldsets();
   }
   // If dropped onto the form and a unique field, remove it from the palette.
-  else if ($(this).is('.form-builder-unique')) {
-    $(this).animate({ height: '0', width: '0' }, function() {
-      $(this).css({ visibility: '', height: '', width: '', display: 'none' });
+  else if ($this.is('.form-builder-unique')) {
+    $this.animate({height: '0', width: '0'}, function() {
+      $this.css({visibility: '', height: '', width: '', display: 'none'});
     });
   }
 };
@@ -822,7 +850,7 @@ Drupal.formBuilder.stopDrag = function(e
 Drupal.formBuilder.over = function(e, ui) {
   var $this = $(this);
   if (!$this.is('.form-builder-empty-placeholder')) {
-    $this.height(ui.helper.outerHeight(true));
+    $this.height(ui.draggable.height());
   }
   $this.parent().find('.over').height(0);
   $this.addClass('over');
@@ -849,39 +877,17 @@ Drupal.formBuilder.out = function(e, ui)
  * @param
  */
 Drupal.formBuilder.checkFieldsets = function(e, ui, expand) {
-  var $fieldsets = $('#form-builder').find('div.form-builder-element > fieldset.form-builder-fieldset');
-  var emptyFieldsets = [];
-
-  // Remove all current fieldset placeholders.
-  $fieldsets.find('.ui-sortable-placeholder').siblings('div.form-builder-empty-placeholder').remove();
+  var $fieldsets = $('#form-builder div.form-builder-element fieldset.form-builder-fieldset div.fieldset-wrapper');
 
   // Find all empty fieldsets.
   $fieldsets.each(function() {
-    // Check for empty collapsible fieldsets.
-    if ($(this).children('div.fieldset-wrapper').length) {
-      if ($(this).children('div.fieldset-wrapper').children(':not(.description):visible, .ui-sortable-placeholder').length == 0) {
-        emptyFieldsets.push(this);
+    // Remove placeholders.
+    $(this).children('.form-builder-empty-placeholder').remove();
+    // If there are no visible children after placeholders are removed, add a placeholder.
+    if ($(this).children(':not(.description):visible').length == 0) {
+      $(Drupal.settings.formBuilder.emptyFieldset).appendTo(this);
       }
-    }
-    // Check for empty normal fieldsets.
-    if ($(this).children(':not(legend, .description):visible, .ui-sortable-placeholder').length == 0) {
-      emptyFieldsets.push(this);
-    }
-  });
-
-  // Add a placeholder DIV in the empty fieldsets.
-  $(emptyFieldsets).each(function() {
-    var wrapper = $(this).children('div.fieldset-wrapper').get(0) || this;
-    var $placeholder = $(Drupal.settings.formBuilder.emptyFieldset).css('display', 'none').appendTo(wrapper);
-    if (expand) {
-      $placeholder.slideDown();
-    }
-    else {
-      $placeholder.css('display', 'block');
-    }
   });
-
-  $('#form-builder').sortable('refresh');
 };
 
 Drupal.formBuilder.setActive = function(element, link) {
@@ -901,13 +907,13 @@ Drupal.formBuilder.unsetActive = functio
 
 Drupal.formBuilder.closeActive = function(callback) {
   if (Drupal.formBuilder.activeElement) {
-    var $activeForm = $(Drupal.formBuilder.activeElement).find('form');
+    var $activeForm = Drupal.formBuilder.fieldConfigureForm ? Drupal.formBuilder.fieldConfigureForm.find('form') : $(Drupal.formBuilder.activeElement).find('form');
 
     if ($activeForm.length) {
       Drupal.freezeHeight();
       $activeForm.slideUp(function(){
         $(this).remove();
-        if (callback) {
+        if (callback && $.isFunction(callback)) {
           callback.call();
         }
       });
