/**
 * todo: make it possible to count words instead of characters 
 * 
 *	ISD: Modified from original authors work
 * 
 **/
jQuery.fn.textLimiter = function(){
    return this.each(function(){
            if(typeof(nr) == "undefined") { nr = 0; }                           
            var counter_id   = 'counter' +nr;                           
			var max          = this.getAttribute('maxlength');
            var html_counter = '<div id="' +counter_id + '" class="ist-textlimit-counter"><span>' +max+ '</span> &nbsp;Characters Remaining</div>'; /* ISD: modified selector name */
            $(this).after(html_counter);
            var jquery_pattern = '#' +counter_id +' > span';
            this.relatedElement = $(jquery_pattern)[0];
            nr++;
            $(this).bind("keyup", function(){
                var maxLength     = this.getAttribute('maxlength');
                var currentLength = this.value.length;
                if(currentLength >= maxLength) {
                    this.relatedElement.className = 'ist-textlimit-counter-exceeded';				/* ISD: modified selector name */
                    this.value = this.value.substring(0, maxLength);
                } else {
                    this.relatedElement.className = '';
                }
                var left_over = maxLength - currentLength; 
                this.relatedElement.firstChild.nodeValue = left_over;
            });
    });
};
