var NBC_CharCounter = function() { 
};

NBC_CharCounter.prototype.config;

/**
 * Example:
        var cc = new NBC_CharCounter();
        cc.init({
          objName: "cc",
          maxLength: 160,
          textAreaId: 'momentCollTxtBox_area', 
          charLimitTextFieldId: 'charLimit',
          charLimitFieldId: 'charLimitTxt', // can be the same as charLimitTextFieldId
          charMaxFieldId: 'charMax', // optional 
          grow: { // optional
            after: 155,
            newSize: 255,
            execute: function() {
              alert ("growing the text field...");
            }
          }
       });
 */
NBC_CharCounter.prototype.init = function (configArgs) {  
  var self = this;
  self.config = configArgs;
  $(document).ready(function() {
    $("#" + self.config.textAreaId)
      .bind("focus", function(event) {
        self.startCounter();
      })     
      .bind("blur", function(event) {
        self.stopCounter();
     });
    // on form reset
    $("#" + self.config.textAreaId).parents().find("form")
      .bind("reset", function(event) {
        self.resetCounter();
    });    
  });
};

NBC_CharCounter.prototype.growExecuted = false;
NBC_CharCounter.prototype.commentCount = 0;
NBC_CharCounter.prototype.charCounter = function() {
  var commentLength = document.getElementById(this.config.textAreaId).value.length;
  if (commentLength === this.commentCount) {
    return; // the same
  }
  else {
    this.commentCount = commentLength;
  }
    
  var charLimit     = document.getElementById(this.config.charLimitTextFieldId);  
  if(commentLength < this.config.maxLength) {
    charLimit.firstChild.nodeValue=this.config.maxLength-commentLength;
    charLimit.style.color = '#525252';
    document.getElementById(this.config.charLimitFieldId).style.color = '#525252';
    }
  else if(commentLength == this.config.maxLength) {
    charLimit.firstChild.nodeValue = this.config.maxLength-commentLength;
    charLimit.style.color = 'red';
    document.getElementById(this.config.charLimitFieldId).style.color = 'red';
  }
  else {
    document.getElementById(this.config.textAreaId).value = document.getElementById(this.config.textAreaId).value.substring(0,this.config.maxLength);
    charLimit.firstChild.nodeValue = '0';
    charLimit.style.color = 'red';
    document.getElementById(this.config.charLimitFieldId).style.color = 'red';
  }

  if(!this.growExecuted && this.config.grow && commentLength >= this.config.grow.after) {
    this.increaseMaxLength(this.config.grow.newSize);
    this.config.grow.execute();
    this.growExecuted = true;
  }
}

NBC_CharCounter.prototype.resetCounter = function() {
  var charLimit = document.getElementById(this.config.charLimitTextFieldId);  
  charLimit.firstChild.nodeValue = this.config.maxLength;
  charLimit.style.color = '#525252';
  document.getElementById(this.config.charLimitFieldId).style.color = '#525252';
}

NBC_CharCounter.prototype.increaseMaxLength = function(newSize) {
  this.config.maxLength = newSize;
  // update max size
  if (typeof this.config.charMaxFieldId != 'undefined') {
    var charMax = document.getElementById(this.config.charMaxFieldId);    
    charMax.firstChild.nodeValue = newSize;
  }
  // update avail chars count
  var charLimit = document.getElementById(this.config.charLimitTextFieldId); 
  var commentLength = document.getElementById(this.config.textAreaId).value.length;  
  charLimit.firstChild.nodeValue = this.config.maxLength - commentLength;
}

NBC_CharCounter.prototype.charCountInterval;
NBC_CharCounter.prototype.startCounter = function() {
  // if the counter is already started
  if (typeof this.charCountInterval == 'number') {
    // stop it
    this.stopCounter();
  }
  // start again
  this.charCountInterval = setInterval(this.config.objName + '.charCounter()',1000);
  U.log("started counter: " + this.charCountInterval);
};

NBC_CharCounter.prototype.stopCounter = function() {
  if (typeof this.charCountInterval == 'number') {
    U.log("stopping counter: " + this.charCountInterval)
    clearInterval(this.charCountInterval);
    this.charCountInterval = null;
  }
};
