/*
 * 
 * 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);
	}
	//for each keypress function on text areas
	this.keypress(function()
	{
		total_words = this.value.split(/[\n]+/).length;  // /[\s\.\?]+/  -- /[\s]+/ -- \s
		g_totalWords = total_words;
		jQuery('#'+p.counterElement).html(total_words);
		
		//MOSILLA
		if(jQuery.browser.mozilla){}
		else {
			//IE if (navigator.appVersion.indexOf('MSIE 6.0')>-1){ //IE 6 }else{ //IE 7 }
			//TODO: Patch set cursor to the end of the textarea in IE
//			if (this.createTextRange) {
//				var r = this.createTextRange();
//				r.collapse(false);
//				r.select();				
//			}
//			jQuery(this).focus(); //set focus
		}
	});	
	//for change function on text areas
	this.keyup(function() 
	{ 
		if(this.value.length == 0){
			total_words = "0";
			g_totalWords = total_words;
		}else{
			total_words=this.value.split(/[\n]+/).length;  // /[\s\.\?]+/  -- /[\s]+/ -- \s
			g_totalWords = total_words;
			
		}
		jQuery('#'+p.counterElement).html(total_words);
		
		//MOSILLA
		if(jQuery.browser.mozilla){}
		else {
			//IE if (navigator.appVersion.indexOf('MSIE 6.0')>-1){ //IE 6 }else{ //IE 7 }
			//TODO: Patch set cursor to the end of the textarea in IE
//			if (this.createTextRange) {
//				var r = this.createTextRange();
//				r.collapse(false);
//				r.select();				
//			}
//			jQuery(this).focus(); //set focus
		}
	});	
};
