$(document).ready(function() {
	$("[class^='count[']").each(function() {
		var elClass = $(this).attr('class');
		var minWords = 0;
		var maxWords = 0;
		var countControl = elClass.substring((elClass.indexOf('['))+1, elClass.lastIndexOf(']')).split(',');
		
		if(countControl.length > 1) {
			minWords = countControl[0];
			maxWords = countControl[1];
		} else {
			maxWords = countControl[0];
		}	
		
		var numberWords = jQuery.trim($(this).val()).split(' ').length;
		if($(this).val() === '') {
      numberWords = 0;
	  }
	  $(this).before('<span class="wordCount"><strong>' + numberWords + '</strong> Words</span>');
		if(numberWords < minWords || (numberWords > maxWords && maxWords != 0)) {
			$(this).siblings('.wordCount').addClass('wcError');
		}	
		
		$(this).bind('keyup click blur focus change paste', function() {
			var numWords = jQuery.trim($(this).val()).split(' ').length;
			if($(this).val() === '') {
				numWords = 0;
			}	
			$(this).siblings('.wordCount').children('strong').text(numWords);
			
			if(numWords < minWords || (numWords > maxWords && maxWords != 0)) {
				$(this).siblings('.wordCount').addClass('wcError');
			} else {
				$(this).siblings('.wordCount').removeClass('wcError');	
			}
		});
	});
});
