/**
* Display interactive feedback about a text field or text area with character limit.
* This does nothing to enforce the limit.
* 
* @param limit number of characters allowed
* @param options
*
* Options must include one of these:
* * status_element: element to receive the
* status message. It can be a jQuery object, element or DOM id.
*  * status_element_suffix: string appended to the src element's
* id to find the status element
*
* Options may include:
* * error_element. This element (or set of elements) have class "error" added
* when there are too many characters in the target element.
* * error_class. Alternate class set on error_element above.
* * status_style. Default is 'text'. May also use 'chars_typed', which simply
*   displays the number of characters the user has typed (see twitter). Or
*   'chars_left' to see just a number of characters that are available.
*/
jQuery.fn.show_char_limit = function(limit, opts) {

  if (!opts.status_element && !opts.status_element_suffix) {
    throw new Exception("Must provide status_element or status_element_suffix");
  }
  
  var o = jQuery.extend({
    error_class: 'error',
    status_style: 'text'
  }, opts);
  
  var show_limit = function(src) {
    var chars_typed = jQuery(src).val().length;
    var left = limit - chars_typed;
    var msg;
    if (o.status_style == 'chars_typed') {
      msg = "" + chars_typed;
    } else if (o.status_style == 'chars_left') {
      msg = "" + left;
    } else {
       var status = left >= 0 ? 'left' : 'over';
       var unit = (Math.abs(left) != 1 ? "characters" : "character");
     msg = "" + Math.abs(left) + " " + unit + " " + status;
    }
    var e = o.status_element ? o.status_element : ("#" + jQuery(src).attr('id') + o.status_element_suffix);
    jQuery(e).html(msg);
    if (o.error_element || o.error_element_suffix) {
    var e = o.error_element ? o.error_element : ("#" + jQuery(src).attr('id') + o.error_element_suffix);
    if (left < 0) {
      jQuery(e).addClass(o.error_class);
    } else {
      jQuery(e).removeClass(o.error_class);
    }
    }
  };
  
  return this.each(function() {
    show_limit(this);
    jQuery(this).keyup(function() {
    show_limit(this);
    });
  });
};
