/**
 * Lickable input behaviors
 */
var Lickable = Behavior.create({
    initialize: function(options){
        options = options || {};
        this._onClass = options['onClass'] || 'on';
        this._hoverClass = options['hoverClass'] || 'focused';
        this._lickableClass = options['lickableClass'] || 'lickable';
        this._feedbackText = options['feedbackText'] || false;
        this._clearErrors = options['clearErrors'] || false;
        this.input = this.element;
        this.element.addClassName(this._lickableClass);
        var upElement = this.element.up();
        if( upElement.match('label')){
          var labelElement = upElement;
          labelElement.writeAttribute('htmlFor', this.input.identify());
          this.label = new Lickable.Label(labelElement, this);
        }
        else
          this.label = new Lickable.Label(upElement.select('label[for="' + this.element.id + '"]').first(), this);
        if(!this.label)
          throw('unable to find lickable label');
        if(this.element.checked) {          
          this._labelsFor(this.element.identify()).invoke('addClassName', 'on');
          this._feedbackElementFor(this.element).invoke('show');
        }
    },
    onclick: function(){
        this.refresh();
    },
    onfocus: function(){
        this.label.onmouseover();
    },
    onblur: function(){
        this.label.onmouseout();
    },
    refresh: function(){
        this._peers().each(function(i){
            var method = i.checked ? 'addClassName' : 'removeClassName';
            this._labelsFor(i.identify()).invoke(method, 'on');
            this._feedbackElementFor(i).invoke(i.checked ? 'show' : 'hide');
            if(this._clearErrors){
              i.siblings().select(function(e){ return e.hasClassName('error-icon')}).invoke('hide');
            }
        }.bind(this));
    },
    _peers: function(){
      var template = new Template("input[name='#{element_name}']");
      return $$(template.evaluate({element_name: this.element.name})).select(function(input){
          return input.type == 'radio' || input.type == 'checkbox';
          });
    },
    _feedbackElementFor: function(element) {        
      if (this._feedbackText == true)
        return element.up().up().select('.feedback');
      else
        return $A([]);
    },
    _labelsFor: function(element_id){
      if(!element_id)
        return $A([]);

      
      var template = new Template("label[for='#{id}']");
      var labels = $$(template.evaluate({id: element_id}));
      if(!labels.any()){
        labels = $A([$(element_id).up('label')]);
      }
      return labels;
    }
});

Lickable.Label = Behavior.create({
    initialize: function(label, lickable, options){
        options = options ||
        {};
        this._onClass = options['onClass'] || 'on';
        this._hoverClass = options['hoverClass'] || 'focused';
        this._lickableClass = options['lickableClass'] || 'lickable';
        this.lickable = lickable; /* the input */
    },
    onmouseover: function(){
        $$('label.' + this._lickableClass + '.' + this._hoverClass).invoke('removeClassName', this._hoverClass);
        this.element.addClassName(this._hoverClass);
    },
    onmouseout: function(){
        this.element.removeClassName(this._hoverClass);
    }
});
