/**
    * todo: make it possible to count words instead of characters
    *
    * Copyright 2007 Bjorn Wijers
    * This file is part of textlimiter.
    * textlimiter is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    * textlimiter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
    * You should have received a copy of the GNU General Public License along with textlimiter; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    * The latest source code is available at http://www.burobjorn.nl
    * Contact burobjorn [at] burobjorn [dot] nl
    *
    * Description: Text limiter is a jQuery plugin which enables you to limit textarea's and input fields.
    * It will display a counter with the amount of characters left.
**/
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="counter"><span>' +max+ '</span></div>';
            $(this).before(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 = 'toomuch';
                    /*this.value = this.value.substring(0, maxLength); Lo quitamos para no cortarle el texto*/
                } else {
                    this.relatedElement.className = '';
                }
                var left_over = maxLength - currentLength;
                this.relatedElement.firstChild.nodeValue = left_over;
            });
    });
};

