UlpFormFieldMatching = {
    draggableClass: '',
    droppableClass: '',
    possibleAnswersWrapp: '',
    standardTitleMessage: '',
    extraTitleMessage: '',

    init: function(args){
        var obj = this;
        obj.setAttributes(obj, args);

        jQuery(document).ready(function(){
          jQuery(obj.draggableClass).draggable({
              cursor: 'move',
              refreshPositions: true,
          });

          jQuery(obj.droppableClass).droppable({
              accept: obj.draggableClass,
              //olerance: "fit",
              drop: function(evt, ui){
                  obj.handleDrop(evt, ui, obj);
              },
              out: function(evt, ui){},
              deactivate: function(evt, ui){

              },
          });
        });

    },

    setAttributes: function(obj, args){
        for (var key in args) {
          obj[key] = args[key];
        }
    },

    handleDrop: function(evt, ui, obj){
      ui.draggable.draggable( 'option', 'revert', false );
      if (jQuery(evt.target).find('div').length>0){
          ui.draggable.draggable( 'option', 'revert', true );
          return false;
      }
      jQuery(evt.target).append(jQuery(evt.srcElement).detach());
      jQuery(evt.srcElement).css({
          top: '0px',
          left: '0px'
      });

      var valueToWrite = jQuery(evt.srcElement).attr('data-answer_key');
      var hiddenInput = jQuery(evt.target).find("input");
      var currentValue = jQuery(hiddenInput).val();
      if (currentValue!=''){
        ui.draggable.draggable( 'option', 'revert', true );
        return false;
      } else {
          ui.draggable.draggable('disable');
          jQuery(evt.srcElement).addClass('js-ulp-natching-move-to-parent');
          jQuery(evt.srcElement).attr('title', obj.extraTitleMessage);
          jQuery(evt.srcElement).on('click', function(clickEvent){
              obj.handleClick(obj, clickEvent);
          })
          jQuery(hiddenInput).val(valueToWrite);
      }

    },

    handleClick: function(obj, clickEvent){
        jQuery(clickEvent.target).parent().find('input').val('');
        jQuery(obj.possibleAnswersWrapp).append(jQuery(clickEvent.target).detach());
        jQuery(clickEvent.target).removeClass('js-ulp-natching-move-to-parent');
        jQuery(clickEvent.target).draggable('enable');
        jQuery(clickEvent.target).attr('title', obj.standardTitleMessage);
    },

};
