// Word count
(function(JQ) {
	wordCount = {

		init : function(id,max,textafter) {
			var t = this, co = JQ(id),w,c;
			id=id.substr(1);
      if(typeof(textafter) == "undefined")
        textafter = '';
			co.before('<span id="char-count-'+id+'">0</span> / <span id="word-count-'+id+'">0</span> <span class="word-counter-text">'+textafter+'</span>');
			w = JQ('#word-count-'+id);
			c = JQ('#char-count-'+id);
			t.wc(co,w,c,max);
			co.keyup( function(e) {
				if ( 13 != e.keyCode && 92 > e.keyCode) 
					t.wc(co,w,c,max,false);
				return true;
			});
			co.blur( function(e) {
				if ( 13 != e.keyCode && 92 > e.keyCode) 
					t.wc(co,w,c,max,true);
				return true;
			});
		},

		wc : function(co,w,c,max,blur) {
			var t = this, tc = 0,tx=co.val();

			if(tx.length>max) {
				co.val(tx.substr(0,max));
				return;
			}
			if ( tx ) {
				tc=1;
				tx = tx.replace( /<.[^<>]*?>/g, ' ' ).replace( /&nbsp;/gi, ' ' );
				//tx = tx.replace( /[0-9.(),;:!?%#$¿'"_+=\\/-]*/g, '' );
				tx.replace( /\S\s+|,\S/g, function(){tc++;} );
				if(blur || !jQuery.browser.msie)
					co.val(tx);
			}
			w.html(tc.toString());
			c.html(tx.length.toString());
;
		}
	} 
}(jQuery));

//hasznalata:
//jQuery(document).ready( function(){ wordCount.init('#textarea',maxhossz); } );

