/*
 * 
 * Textarea Word Count Jquery Plugin 
 * Version 1.0
 * 
 * Copyright (c) 2008 Roshan Bhattarai
 * website : http://roshanbh.com.np
 * 
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
*/
var g_totalWords = 0;

jQuery.fn.wordCount = function(params) {
	var p = {counterElement: 'display_count'};
	var total_words = 0;
	g_totalWords = total_words;
	if(params) {
		jQuery.extend(p, params);
	}
	this.keypress(function() {
		words = this.value.split(/[\r\n]+/);
		total_words = '0';
		jQuery.each(words, function(){
			if (this.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != '') {				
				total_words++;
			}
		});
		g_totalWords = total_words;
		jQuery('#'+p.counterElement).html(total_words);
	});	
	this.keyup(function() {		
		if(this.value.length == 0){
			total_words = '0';
			g_totalWords = total_words;
		} else {
			words = this.value.split(/[\r\n]+/);
			total_words = '0';
			jQuery.each(words, function(){
				if (this.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != '') {					
					total_words++;
				}
			});
			
		}
		g_totalWords = total_words;
		jQuery('#'+p.counterElement).html(total_words);		
	});	
};

