var WordCounter = {
  init: function(text_field, count_down, counter_element)
  {
    var o = {}
    for(var m in this){
      o[m] = this[m]
    }
    
    o.text_field = $(text_field);
    o.count_down = count_down;
    o.counter_element = $(counter_element);    
    o.setup();
    o.update();
  },
  count: function()
  {
    var fullStr     = this.text_field.value;
    var cleanedStr  = fullStr.replace(/^\s+/,'');
    cleanedStr      = cleanedStr.replace(/\s+$/,'');
    cleanedStr      = cleanedStr.replace(/\s{2,}/gi,' ');
    var splitString = cleanedStr.split(" ");
    
    return splitString.length-1;
  },
  update: function()
  {
    var total = this.count_down- this.count();
    if(total< 0){total = "Total is too long. Please reduce the text."}
    else {total = total+" words remaining."}
    this.counter_element.innerHTML = total;
    
  },
  setup: function()
  {
    var wc = this;
    this.text_field.onkeypress   = function(){wc.update()};
    this.text_field.onblur       = function(){wc.update()};
    this.text_field.onfocus      = function(){wc.update()};
    this.text_field.onmousedown  = function(){wc.update()};    
  }
  
}

//javascript:void(o = {s : function(n){t = n}, f: function() {alert(t)}}); o.s("xx"); o.f()
