/*
http://www.reindel.com/five_javascript_tricks_jquery/

*/
/*
$(function(){
	var countdown = {
		init: function() {
			countdown.remaining = countdown.max - $(countdown.obj).val().length;
			if (countdown.remaining > countdown.max) {
				$(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));
			}
			$(countdown.obj).siblings(".remaining").html(countdown.remaining + " characters remaining.");
			alert(countdown.remaining)
			if (countdown.remaining < 1 ) {
				
				var newStr = $(countdown.obj).val().substring(0, countdown.max);
				$(countdown.obj).val(newStr);
				return false;
			} else {
				return true;
			}
		},
		max: null,
		remaining: null,
		obj: null
	};
});	
	$(".countdown").each(function() {
		$(this).keypress(function(e) {
			if (e.which == 32 || (65 <= e.which && e.which <= 65 + 25) || (97 <= e.which && e.which <= 97 + 25)) {
				if(e.which == 118) {
					var strStr = $(this).val();
					$(this).val(strStr.substring(0, countdown.max));
					alert(strStr.substring(0, countdown.max));
					//return false;
				}
				if (!countdown.init()) {
					return false;
				}
			} else {
				countdown.init();
			}
		}).blur(function() {
			countdown.init();
		});
		var c = $(this).attr("class");
		countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
		countdown.obj = this;
		countdown.init();
	});
	
	
	
	
	
	
});*/

$(function(){
	var countdown = {
		init: function() {
			countdown.remaining = countdown.max - $(countdown.obj).val().length;
			if (countdown.remaining > countdown.max) {
				$(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));
			}
			$(countdown.obj).siblings(".remaining").html(countdown.remaining + " characters remaining.");
			if (countdown.remaining < 1 ) {
				
				var newStr = $(countdown.obj).val().substring(0, countdown.max);
				$(countdown.obj).val(newStr);
				return false;
			} else {
				return true;
			}
		},
		max: null,
		remaining: null,
		obj: null
	};
	$(".countdown").each(function() {
		$(this).focus(function() {
			var c = $(this).attr("class");
			countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
			countdown.obj = this;
		}).blur(function() {
			countdown.init();
			clearInterval(iCount);
		});
		var c = $(this).attr("class");
		countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
		countdown.obj = this;
		iCount = setInterval(countdown.init,200);
	});
	
	
});
