/*
 * 
 * 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"
	};
	var pd = {
		counterElement:"display_countdown"
	};
	var total_words;
	
	if(params) {
		jQuery.extend(p, params);
	}
	
	//for each keypress function on text areas
	this.keypress(function()
	{ 
		total_words=this.value.split(/[\s\.\?]+/).length;
		if(this.value.length==0) total_words='0';
		words_left = 250 - total_words;
		if(total_words == 250) words_left='0';	// it won't show number 0 unless we make it a string.	
		if(total_words > 250) {
			jQuery('#'+p.counterElement).css('color','#ff0000');
			jQuery('#'+pd.counterElement).css('color','#ff0000');
		}
		else {
			jQuery('#'+p.counterElement).css('color','#000000');
			jQuery('#'+pd.counterElement).css('color','#000000');
		}
		jQuery('#'+p.counterElement).html(total_words);
		jQuery('#'+pd.counterElement).html(words_left);
	});	
};

