/* Copyright 2011 the Melange authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Melange jQuery plugin to add options to select type HTML widgets on the fly
 *
 * Inspired by (and some portions of code taken from) Mike Botsko's jQuery
 * form builder plugin available at
 * http://www.botsko.net/blog/2009/04/jquery-form-builder-plugin/
 */

(function($){
  $.fn.dynamicoptions = function(options) {
 // Extend the configuration options with user-provided
    var defaults = {};
    var opts = $.extend(defaults, options);

    return this.each(function () {
      var addFieldHtml = function (values) {
        field = '';
        field += '<div>';
        field += '<a href="#" class="remove" title="X">X</a>';
        var j = 0;
        for (var i in opts.fields) {
          if (!values) {
            value = ''
          }
          else {
            value = values[j]
          }
          field += '<input type="text" name=' + i + ' class="tagfield" value="' + unescape(value) + '" />';
          j += 1;
        }
        field += '</div>';
        return field;
      };

      field = '<label class="form-label">' + opts.label + '</label>';

      var initial = JSON.parse(opts.initial);

      for (var i in initial) {
        field += addFieldHtml(initial[i]);
      }

      field += '<div class="add-area clearfix"><a id=add-' + opts.id + ' href="#" class="add">Add</a></div>';

      $(this).append(field);

      $('#add-' + opts.id).live('click', function () {
        $(this).parent().before(addFieldHtml());
        return false;
      });

      $('.remove').live('click', function () {
        $(this).parent('div').animate({
          opacity: 'hide',
          height: 'hide',
          marginBottom: '0px'
        }, 'fast', function () {
          $(this).remove();
        });
        return false;
      });

    });
  };
})(jQuery);

@@ -869,15 +869,25 @@
 ion(
-$)%7B%0A  $
+jQuery)%7B%0A  jQuery
 .fn.
@@ -923,16 +923,19 @@
 ions) %7B%0A
+   
  // Exte
@@ -1020,17 +1020,22 @@
  opts = 
-$
+jQuery
 .extend(
@@ -1139,24 +1139,28 @@
 ) %7B%0A        
+var 
 field = '';%0A
@@ -1279,58 +1279,133 @@
     
-for (var i in opts.fields) %7B%0A          if (!values
+jQuery.each(opts.fields, function(index, item) %7B%0A          var value;%0A          if (values !== undefined && values.length %3E j
 ) %7B%0A
@@ -1424,18 +1424,26 @@
 value = 
-''
+values%5Bj%5D;
 %0A       
@@ -1484,25 +1484,19 @@
 value = 
-values%5Bj%5D
+'';
 %0A       
@@ -1549,16 +1549,20 @@
 me=' + i
+ndex
  + ' cla
@@ -1631,32 +1631,34 @@
  += 1;%0A        %7D
+);
 %0A        field +
@@ -1703,24 +1703,28 @@
   %7D;%0A%0A      
+var 
 field = '%3Cla
@@ -1833,29 +1833,49 @@
     
-for (var i in initial
+jQuery.each(initial, function(index, item
 ) %7B%0A
@@ -1909,17 +1909,11 @@
 ml(i
-nitial%5Bi%5D
+tem
 );%0A 
@@ -1918,16 +1918,18 @@
 %0A      %7D
+);
 %0A%0A      
@@ -2035,25 +2035,30 @@
 v%3E';%0A%0A      
-$
+jQuery
 (this).appen
@@ -2074,17 +2074,22 @@
 %0A%0A      
-$
+jQuery
 ('#add-'
@@ -2128,33 +2128,38 @@
 on () %7B%0A        
-$
+jQuery
 (this).parent().
@@ -2221,17 +2221,22 @@
 %0A%0A      
-$
+jQuery
 ('.remov
@@ -2271,25 +2271,30 @@
 ) %7B%0A        
-$
+jQuery
 (this).paren
@@ -2442,9 +2442,14 @@
     
-$
+jQuery
 (thi

Implement the suggestions made in the comments to r2bbe510d9d90.