jQuery(document).ready(function($) {

	$('.edit_button').click(function() {
		$('a.edit_button').hide();
		$('.text_wrapper').hide();
		$('.edit').show();
		$('#user_note_barbox').show();
	});

	$(".editbox").mouseup(function() {
		return false
	});

	$(".editbox").change(function() {

		$('.edit').hide();

		var boxval = $(".editbox").val();

		if (boxval.length > 512) {
			alert('Too much characters (max 512).');

			$('.text_wrapper').hide();
			$('.edit').show();
		} else {
			$.ajax( {
				url : im_cfg.rootPath + "instant_messenger.php",
				dataType : 'html',
				data : {
					action : 'user_note',
					user_note : boxval
				},
				cache : false,
				success : function(html) {
					$('#user_note_barbox').hide();
					$('.text_wrapper').html(boxval);
					$('.text_wrapper').show();
				}
			});
		}

	});

	$(document).mouseup(function() {

		$('.edit').hide();
		$('.text_wrapper').show();
	});

	$(".editbox").keyup(function() {
		var boxval = $(".editbox").val();

		if (boxval.length == 512) {
			alert('The maximum of characters (512) was reached');
			$('.text_wrapper').hide();
			$('.edit').show();
		}

		var full = boxval.length * 100;
		var value = (full / 512);
		var count = 512 - boxval.length;

		if (boxval.length <= 512) {
			$('#user_note_count').html(count);
			$('#user_note_bar').animate( {
				"width" : value + '%'
			}, 1);
		} else {
			$('#user_note_bar').animate( {
				"width" : '100%'
			}, 1);
			$('#user_note_count').html(count);
		}

		return false;
	});

});
