/*
 * 
 * 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 wordLimit = 80;
	var total_words;
	var wordsLeft;
	
	if(params) {
		jQuery.extend(p, params);
	}
	
	//for each keypress function on text areas
	this.keypress(function(event)
	{
		total_words=this.value.split(/[\s\.\?]+/).length;
		wordsLeft=wordLimit-total_words;
		var text = jQuery('#'+p.counterElement).val();
		
		if(total_words > wordLimit && (event.keyCode != 8 && event.keyCode != 46)) {
			jQuery('#'+p.counterElement).html('You can\'t have more than <strong>'+wordLimit+'</strong> words. You currently have <strong>'+total_words+'</strong>.');
			jQuery('#'+p.counterElement).val(text.substr(0,wordLimit));
			return false;
		} else {
			jQuery('#'+p.counterElement).html(total_words+' word(s). <strong>'+wordsLeft+'</strong> words left.');
			return true;
		}
		
	});	
};

jQuery.fn.mapWordCount = function(params){
	var p = {
		counterElement:"display_count"
	};
	var wordLimit = 35;
	var total_words;
	var wordsLeft;
	
	if(params) {
		jQuery.extend(p, params);
	}
	
	//for each keypress function on text areas
	this.keypress(function(event)
	{
		total_words=this.value.split(/[\s\.\?]+/).length;
		wordsLeft=wordLimit-total_words;
		var text = jQuery('#'+p.counterElement).val();
		if(total_words > wordLimit && (event.keyCode != 8 && event.keyCode != 46)) {
			jQuery('#'+p.counterElement).html('You can\'t have more than <strong>'+wordLimit+'</strong> words. You currently have <strong>'+total_words+'</strong>.');
			jQuery('#'+p.counterElement).val(text.substr(0,wordLimit));
			return false;
		} else {
			jQuery('#'+p.counterElement).html(total_words+' word(s). <strong>'+wordsLeft+'</strong> words left.');
			return true;
		}
		
	});	
};
