/*
    @brief:     Javascript and jQuery plugins for AOH Subscriptions plugin
    @author:    Rob Simpkins (http://robsimpkins.com)
    @copyright: Rob Simpkins 2009. All rights reserved.
    @version:   1.0.0
 */


/*
    @brief:     jQuery word counter plugin
    @author:    Trevor Davies (http://trevordavis.net)
    @copyright: Trevor Davies 2009. All rights reserved.
 */
$(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('<span class="word_count"><strong>0</strong> words</span>');
		if(minWords > 0) {
			$(this).siblings('.word_count').addClass('error');
		}	
		
		$(this).bind('keyup click blur focus change paste', function() {
			var numWords = jQuery.trim($(this).val()).replace(/\s+/g," ").split(' ').length;
            
            if($(this).val() === '') {
				numWords = 0;
			}	
			$(this).siblings('.word_count').children('strong').text(numWords);
			
			if(numWords < minWords || (numWords > maxWords && maxWords != 0)) {
				$(this).siblings('.word_count').addClass('error');
			} else {
				$(this).siblings('.word_count').removeClass('error');	
			}
		});
	});
});


/*
    @brief:     jQuery check all checkboxes
    @author:    Brian Cray (http://briancray.com)
    @copyright: Brian Cray 2009. All rights reserved.
 */
$( function () {
	$( '.check_all' ).click( function () {
		$( document ).find( ':checkbox' ).attr( 'checked', this.checked );
	});
});
