Event.observe(
	window,
	'load',
	function() {
		 new Ajax.PeriodicalUpdater(
			'msg_list',
			'msgbox.php',
			{
			  frequency: 30,
			  decay: 1
			}
		);
		if ( $('msg_emoticons') != null )
		{
			$('msg_emoticons').descendants().each(
				function(ei)
				{
					ei.setStyle({'cursor': 'pointer'});
					Event.observe(
						ei,
						'click',
						function() { $('msg_txt').value += ei.alt; }
					);
				}
			);
		}
		Event.observe(
			'msg_form',
			'submit',
			function(e)
			{
				var form = $('msg_form');
				Event.stop(e);
				form.disable();
				form.request({
					parameters: { 'msg_txt': $F('msg_txt'), 'ajax': 'on' },
					onSuccess: function()
					{
						form.reset();
						new Ajax.Updater(
							'msg_list',
							'msgbox.php'
						);
						//alert('Messaggio inviato con successo.');
						new PeriodicalExecuter(
							function(pe)
							{
								var list = $('msg_list');
								if ( list.scrollTop != 0 )
								{
									var diff = Math.floor(list.scrollTop / 7);
									list.scrollTop -= diff < 1 ? 1 : diff;
								}
								else { pe.stop(); }
							}, 0.001
						);
					},
					onFailure: function() { alert('Impossibile inviare il messaggio.'); },
					onComplete: function() { $('msg_form').enable(); }
				});
			}
		);
	}
);