/*
 ____  _  _  ____  __  __  ____  ___  ____  _____  __  __  ____
(_  _)( \( )(  _ \(  )(  )(_  _)/ __)(  _ \(  _  )(  )(  )(  _ \
 _)(_  )  (  )___/ )(__)(   )( ( (_-. )   / )(_)(  )(__)(  )___/
(____)(_)\_)(__)  (______) (__) \___/(_)\_)(_____)(______)(__)

   Input group
   By Nike in 2009
*/


var inputGroup = new Class({
  initialize: function(prefix,container,template,max_entries){
    this.prefix=prefix;
    this.container=container;
    this.template=template;
    this.max_entries=max_entries;
    this.inputs=new Hash();
  },
  
  add: function(def_val){
    if(this.inputs.getLength()+1>this.max_entries) return('');
    var tmp=new htmlfrag(this.template,'','_wantaref_');

    this.container.grab(tmp.domnode);

    $(this.prefix+'_input'+tmp.sufix).value=def_val;

    $(this.prefix+'_input'+tmp.sufix).addEvent('change',function(e){
         e.stop();
         this.no_dups();
    }.bind(this));

    $(this.prefix+'_add_btn'+tmp.sufix).addEvent('click',function(e){
         e.stop();
         if($(e.target).tagName=='A') { $(e.target).blur(); var id=$(e.target).id; }
         else { $(e.target).getParent().blur(); var id=$(e.target).getParent().id; }
         this.add('');
    }.bind(this));
    
    $(this.prefix+'_del_btn'+tmp.sufix).addEvent('click',function(e){
         e.stop();
         if($(e.target).tagName=='A') { $(e.target).blur(); var id=$(e.target).id; }
         else { $(e.target).getParent().blur(); var id=$(e.target).getParent().id; }
         var sfx=id.substr(id.length-10); this.remove(sfx);
      }.bind(this));
      
    $(this.prefix+'_input'+tmp.sufix).setProperty('name',this.prefix+'_input['+tmp.sufix.substr(1)+']');
    this.inputs[tmp.sufix]=tmp;

    if(this.inputs.getKeys().length<=2) {
      $(this.prefix+'_del_btn'+tmp.sufix).setStyle('display','none');
    } else {
      this.inputs.getKeys().each(function(sfx,idx){ 
        if(idx>0) $(this.prefix+'_del_btn'+sfx).setStyle('display','inline');
      }.bind(this));
    }
    
    this.inputs.getKeys().each(function(sfx,idx){ 
        if(sfx!=tmp.sufix) $(this.prefix+'_add_btn'+sfx).setStyle('display','none');
        else if((this.inputs.getLength()<this.max_entries) && (idx>0) ) $(this.prefix+'_add_btn'+sfx).setStyle('display','inline');
    }.bind(this));

    this.no_dups();

    return(tmp.sufix);
  },

  remove: function(sufix){
    if(this.inputs.getLength()<=2) return;

    var sel_opt_value=$(this.prefix+'_input'+sufix).getSelected()[0].value;
    $(this.template+sufix).destroy();
    this.inputs.erase(sufix);
    
    if(this.inputs.getLength()>2) {
      $(this.prefix+'_del_btn'+this.inputs.getKeys()[1]).setStyle('display','inline');
      this.inputs.getKeys().each(function(sfx,idx){
        if(idx<this.inputs.getLength()-1) $(this.prefix+'_add_btn'+sfx).setStyle('display','none');
        else if(idx>0) $(this.prefix+'_add_btn'+sfx).setStyle('display','inline');
      }.bind(this));
    } else {
      $(this.prefix+'_del_btn'+this.inputs.getKeys()[1]).setStyle('display','none');
      $(this.prefix+'_add_btn'+this.inputs.getKeys()[1]).setStyle('display','inline');
    }
    this.no_dups();
  },

  clear_all: function(){
    this.inputs.getKeys().each(function(sfx,idx){
      if(idx>0) { $(this.template+sfx).destroy(); this.inputs.erase(sfx); }
    }.bind(this));
    this.inputs[this.inputs.getKeys()[0]].domrefs[this.prefix+'_input'].value='';
    $(this.prefix+'_del_btn'+this.inputs.getKeys()[0]).setStyle('display','none');
  },

  no_dups: function(){
    var all_selected=new Hash();
    this.inputs.getKeys().each(function(sfx,idx){
      var sel_obj=$(this.prefix+'_input'+sfx);
      if(sel_obj.getSelected()[0].value!='') all_selected[sfx]=sel_obj.getSelected()[0].value;
      for(var i=0;i<sel_obj.options.length;i++) { // mootools '$each' breaks on the select options array under FUCKING IE... just don't ask!
              sel_obj.options[i].disabled=false;
            }
    }.bind(this));

    all_selected.each(function(val,sfx){
        this.inputs.getKeys().each(function(sfx2){
          if(sfx!=sfx2){
            var sel_obj=$(this.prefix+'_input'+sfx2);
            for(var i=0;i<sel_obj.options.length;i++) { // mootools '$each' breaks on the select options array under FUCKING IE... just don't ask!
              var opt=sel_obj.options[i];
              if(opt.value==val) opt.disabled=true;
            }
          }
        }.bind(this));
    }.bind(this));
    
 }
 
});
