/**
 * Adds maxlength functionality to textareas.
 * @author Alvaro Castro <alvaro.castro@teracode.com>
 * @version 1.0
 */
$(document).ready(function() {
	$('textarea').each(function() {
		if($(this).attr('maxlength') >= 0) {
			$(this).bind('keyup', function() {
		        var len = $(this).val().length;
				var max = $(this).attr('maxlength');
				if (len > max) {
		            $(this).val($(this).val().substring(0, max));
		        }
			}).bind('keydown', function(event) {
		        var len = $(this).val().length;
				var max = $(this).attr('maxlength');
				if (len >= max) {
					switch (event.keyCode) {
						//delete
						case 8: case 46:
						//arrows
						case 37: case 38: case 39: case 40:
		                    return true;
		            	default:
		                    return false;
					}
				}
			});
		}
	});
});
