/*
 *
 * 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
 *
*/

jQuery.fn.wordCount = function(params){
	var p = {
		counterElement:"display_count",
		counterMinimum:"display_minimum",
		counterTotal:20
	};
	var total_words;

	if(params) {
		jQuery.extend(p, params);
	}

	//for each keypress function on text areas
	this.keypress(function()
	{
		var str = this.value;
		/*
		str = jQuery.trim(str.replace(/[\W]+/i," "));
		str = jQuery.trim(str);
		str = jQuery.trim(str.replace(/[\s]+/i," "));
		str = jQuery.trim(str);
		str = jQuery.trim(str.replace(/[^\d\w ]+/i,""));
		str = jQuery.trim(str);
		*/

		if(str == "") total_words=0;
		//else total_words=str.split(/[\s\.\?]+/).length;
		else total_words=str.replace(/[\s\.\?]+/,"").length;

		if(total_words >= p.counterTotal) {
			jQuery('#'+p.counterMinimum).css('color','green');
			//return false;
		} else {
			jQuery('#'+p.counterMinimum).css('color','red');
		}
		jQuery('#'+p.counterElement).html(total_words);
	});

	this.keyup(function()
	{
		var str = this.value;
		/*
		str = jQuery.trim(str.replace(/[\W]+/i," "));
		str = jQuery.trim(str);
		str = jQuery.trim(str.replace(/[\s]+/i," "));
		str = jQuery.trim(str);
		str = jQuery.trim(str.replace(/[^\d\w ]+/i,""));
		str = jQuery.trim(str);
		*/

		if(str == "") total_words=0;
		//else total_words=str.split(/[\s\.\?]+/).length;
		else total_words=str.replace(/[\s\.\?]+/,"").length;

		if(total_words >= p.counterTotal) {
			jQuery('#'+p.counterMinimum).css('color','green');
			//return false;
		} else {
			jQuery('#'+p.counterMinimum).css('color','red');
		}
		jQuery('#'+p.counterElement).html(total_words);
	});

};
