var Countable = new Class({
  defaultOptions: {
    maximum: 100,
    offset: 20
  },
  initialize: function(inputId, options) {
	
    // Merges the default options with the ones given as parameters
    this.setOptions($merge(this.defaultOptions, options));
 
    // Retrieves the textarea element based on its name
    input = $(inputId);
 
    if (input) {
      // Creates an element to serve as a handle:
      // 
      //   <div class="count"></div>
      // 
     // this.handle = new Element("div", {class: "count"});
      this.handle = $('q_char');
 
      // Injects this element just after the textarea
      //this.handle.set('html','&nbsp;').inject(input,'after');
      this.handle.set('html','&nbsp;');
 
      // Setups effects
      //this.opacityEffect = new Fx.Opacity(this.handle, {duration: 600, wait: false});
 
      // Initializes effects on each element
     // this.opacityEffect.hide();
 
      // Fires action upon events
      input.addEvent('keydown', this.onKeyPress.bindWithEvent(this));
      input.addEvent('keyup', this.onKeyPress.bindWithEvent(this));
 
      // Keeps a reference to the handle
      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>";
      }
      
     
 
      // Displays information message
      this.handle.set('html',string);
      
      
   
  }
});
 
// Adds options management support
Countable.implement(Options.prototype);
