var Countable = new Class({
  defaultOptions: {
    maximum: 100,
    offset: 20
  },
  initialize: function(inputId, options) {
   
    this.setOptions($merge(this.defaultOptions, options));

    input = $(inputId);
 
    if (input) {
      this.handle = $('q_char');
      this.handle.set('html','&nbsp;');
      input.addEvent('keydown', this.onKeyPress.bindWithEvent(this));
      input.addEvent('keyup', this.onKeyPress.bindWithEvent(this));

      this.input = input;
    }
  },
  onKeyPress: function(event) {
    event = new Event(event);
 
    if(!event.shift && !event.control && !event.alt && !event.meta) {
      this.update();
    }
  },
  update: function() {
    // Removes the character entered as the maximum has been reached
    if (this.input.value.length > this.options.maximum) {
      this.input.value = this.input.value.substring(0, this.options.maximum);
    }
       var count = this.options.maximum - this.input.value.length;
 		if (count == 0) {
        var string = "No character left";
      } else if (count == 1) {
        var string = "Character remaining <strong>1</strong>";
      } else {
        var string = "Characters remaining <strong>"+count + "</strong>";
      }
      
      this.handle.set('html',string);
      
      
   
  }
});

Countable.implement(Options.prototype);
