/*
 * Javascript file for Pickup Selection
 */

var PickupSelection = Class.create();
PickupSelection.prototype = {
  initialize: function(candidates, selections, add, remove){
    var thisModel = this;

    this.candidates = candidates;
    this.selections = selections;
    add.onclick = candidates.ondblclick = function(){ thisModel.add(); };
    remove.onclick = selections.ondblclick = function(){ thisModel.remove(); };
  },
  add: function(){
    var dest = this.selections.options;
    var i;
    while ((index = this.candidates.selectedIndex) >= 0){
      var item = this.candidates.options[index];
      var length = dest.length;
      item.selected = false;
      for (i = 0; i < length; i++){
        if (dest[i].value == item.value) break;
      }
      if (i == length){
        dest[length] = new Option(item.text, item.value);
      }
    }
  },
  remove: function(){
    while ((index = this.selections.selectedIndex) >= 0){
      this.selections.options[index] = null;
    }
  },
  load: function(list){
    var i, options = this.candidates.options;
    for (i = 0; i < options.length; i++){
      this.candidates.options[0] = null;
    }
    list.each(function(value, index){
      options[index] = new Option(value.text, value.id);
    })
  },
  prepare: function(param_id) {
   var options = this.selections.options;
   for (i = 0; i < options.length; i++) {
     var item = options[i];
     item.selected = true;
   }
  },
  query: function(key) {
    return $A(this.selections.options).map(function(option){
      return [key, encodeURIComponent(option.value)].join('=');
    }).join('&');
  }
};

