/**
 * Word Count
 * 
 */
 
$(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];
		}	
		
		$(this).after('<div class="wordCount"><strong>0</strong> Words</div>');
		if(minWords > 0) {
			$(this).siblings('.wordCount').addClass('error');
		}	
		
		$(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('error');
			} else {
				$(this).siblings('.wordCount').removeClass('error');	
			}
		});
	});
});



/**
 * Character count
 * 
 */
 
 /*
	Original Code was by: AjaxRay ( http://www.ajaxray.com/blog/2007/11/09/interactive-character-limit-for-textarea-using-jquery/ )
	Plugin Written by : Mohammad Amzad Hossain ( tohin.wordpress.com)
	Thanks to : http://www.learningjquery.com/2007/10/a-plugin-development-pattern
*/

(function($) {

$.fn.startCounting = function(options){
	
	var defaults = {
		limit: 30
	};
	
	options = $.extend( defaults, options);
	
	return $(this).each( function(i) 	{	
	
		var elem = $(this);
		elem.after('<input type="text" class="counting_class" readonly size="12" />');				
		elem.keyup( function(i) {
			var limit = options.limit;			
			var text = elem.val();	
			var textlength = text.length;
			
			if( textlength > limit)
			{
				elem.next('.counting_class').val( limit + ' / ' + limit);
				elem.val(text.substr(0,limit));
				return false;
			}
			else
			{				
				elem.next('.counting_class').val( textlength + ' / ' + limit);
				return true;
			}
		}); 				
		elem.trigger('keyup');
	}); 
};


})(jQuery);

$(document).ready(
function() {
	$('#short_description').startCounting({limit: 140});
}
);