limitChars = function(textid, limit, infodiv) {
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit) {
		$('#' + infodiv).html(0);
		$('#'+textid).val(text.substr(0,limit));
		
		return false;
	} else {
		$('#' + infodiv).html((limit - textlength));
		return true;
	}
};

$(document).ready(function(){
	$('#guest-post').click(function(){
		$('#post-message-popup').toggle();
		$('#message_name').focus();
	});
	$('#fb-post').click(function(){
		$('#facebook-post-popup').toggle();
		$('#message_fb_body').focus();
	});
	
	$('.reply-link').click(function(){
		var userType = this.id.substring(0,2);
		if(userType=='fb')
		{
			var currentId = parseInt(this.id.substring(8));
			
			$('#message_fb_question_id').val(currentId);
			$('#facebook-post-popup').toggle();
			$('#message_fb_embed').focus();
		}
		else
		{
			var currentId = parseInt(this.id.substring(11));
			
			$('#message_guest_question_id').val(currentId);
			$('#post-message-popup').toggle();
			$('#message_name').focus();
		}
		
		return false;
	});
	
	$('#close-btn-guest').click(function(){
		$('#post-message-popup').hide();
	})
	$('#close-btn-fb').click(function(){
		$('#facebook-post-popup').hide();
	})
	
	$('#message_body').keyup(function(){
		limitChars('message_body', 400, 'countdown');
	}).focus(function() {
		limitChars('message_body', 400, 'countdown');
	}).change(function() {
		limitChars('message_body', 400, 'countdown');
	});
	
	$('#message_fb_body').keyup(function(){
		limitChars('message_fb_body', 400, 'countdown2');
	}).focus(function() {
		limitChars('message_fb_body', 400, 'countdown2');
	}).change(function() {
		limitChars('message_fb_body', 400, 'countdown2');
	});

});

