$(document).ready(function(){

  var indexFunction = function(element){
    var i = 0;
    var reg = new RegExp('([0-9]+)(\|){1}', 'g');
    $(element).each(function(){
      $(this).find('input, textarea').each(function(){
        $(this).attr('id', $(this).attr('id').replace(reg, i) );
        $(this).attr('name', $(this).attr('name').replace(reg, i) );
      });
      $(this).find('label').each(function(){
        $(this).attr('for', $(this).attr('for').replace(reg, i) );
      });
      i++;
    });
  }

  var list = Array('product', 'contact', 'press', 'parcours', 'machines');

  for( i = 0; i < list.length; i++ )
  {
    var key = list[i];
    $.sfFormDynamic({
      selector: {
        add: '.add_' + key,
        max_elements: '#max_' + key,
        min_elements_event: 'min_elements_' + key,
        max_elements_event: 'max_elements_' + key,
        remove: '.remove_' + key,
        clone: '.clone_' + key + ':first',
        container: '#' + key + '_form #exhibitorFieldsetContainer',
        nb: 'input[name*=nb_' + key + ']',
        element: '.element_' + key
      },
      nbmin: 1,
      index: indexFunction
    });
  }

  $('.element_product .add_product').live('click',function(e){
    if($('.element_product').size() > 5) {
        count = 0;
        $('.element_product').each(function(){
            if(count >= 5 ) $(this).remove();
            count ++;
        });
        }
    });

});

