Index: /trunk/instant_messenger/inc/controller.xml
===================================================================
--- /trunk/instant_messenger/inc/controller.xml	(revision 318)
+++ /trunk/instant_messenger/inc/controller.xml	(revision 323)
@@ -58,4 +58,5 @@
 			<item ref="composing" class="Ujabber" method="composing" prefix="class." suffix=".inc.php" />
 			<item ref="paused" class="Ujabber" method="paused" prefix="class." suffix=".inc.php" />
+			<item ref="checkConnection" class="Ujabber" method="checkConnection" prefix="class." suffix=".inc.php" />
 		</php>
 		<xml></xml>
Index: /trunk/instant_messenger/inc/Jabberd2.abstract.php
===================================================================
--- /trunk/instant_messenger/inc/Jabberd2.abstract.php	(revision 311)
+++ /trunk/instant_messenger/inc/Jabberd2.abstract.php	(revision 323)
@@ -171,5 +171,5 @@
 	}
 
-	final function get_last_access_user($pUser)
+	final function get_last_access_user($pUser = array("jid" => "this"))
 	{
 		$id = "last_time_user";
Index: /trunk/instant_messenger/inc/class.Ujabber.inc.php
===================================================================
--- /trunk/instant_messenger/inc/class.Ujabber.inc.php	(revision 318)
+++ /trunk/instant_messenger/inc/class.Ujabber.inc.php	(revision 323)
@@ -59,6 +59,4 @@
 			return "disconnected";
 
-		$this->get_last_access_user(array("jid" => "this"));
-
 		if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
 		{
@@ -76,5 +74,5 @@
 			$buffer = '';
 
-			while ( ( connection_aborted() === 0 ) && time() - $init < 30 )
+			while ( ( connection_aborted() === 0 ) && time() - $init < 50 )
 			{
 				# read from server and write in the client
@@ -106,4 +104,10 @@
 	}
 
+	public final function checkConnection()
+	{
+		$this->get_last_access_user();
+		return $this->isConnected();
+	}
+
 	public final function Composing($pJid)
 	{
Index: /trunk/instant_messenger/js/jabber.js
===================================================================
--- /trunk/instant_messenger/js/jabber.js	(revision 321)
+++ /trunk/instant_messenger/js/jabber.js	(revision 323)
@@ -381,5 +381,11 @@
 			_pPresence = arguments[0];
 
-		_cookies.set('IM_presence', _pPresence);
+		if ( _pPresence == 'auto' )
+		{
+			if ( _cookies.get('IM_presence') == 'available' )
+				_pPresence = 'xa';
+		}
+		else
+			_cookies.set('IM_presence', _pPresence);
 
 		switch ( _pPresence )
@@ -812,4 +818,5 @@
 	Jabber.prototype.setPresence = _setPresence;
 	window.Jabber = Jabber;
+	window.InstantMessengerPresence = _setPresence;
 }
 )();
Index: /trunk/instant_messenger/js/client.js
===================================================================
--- /trunk/instant_messenger/js/client.js	(revision 321)
+++ /trunk/instant_messenger/js/client.js	(revision 323)
@@ -149,11 +149,5 @@
 			if ( !_disconnected )
 			{
-				_conn.go('$this.Ujabber.getContacts',
-					function(data)
-					{
-						if ( data == 'disconnected' )
-							setTimeout(_contacts, 3000);
-					}
-				);
+				_conn.go('$this.Ujabber.getContacts');
 				setTimeout(function()
 				{
@@ -294,8 +288,15 @@
 		}
 
+		function _check_connection()
+		{
+			if ( !_disconnected )
+				_conn.go('t.checkConnection');
+		}
+
 		function Client()
 		{
 			_request();
 			setTimeout(_contacts, 3000);
+			window.setInterval(_check_connection, 20000);
 		}
 
Index: /trunk/instant_messenger/js/preferences.js
===================================================================
--- /trunk/instant_messenger/js/preferences.js	(revision 318)
+++ /trunk/instant_messenger/js/preferences.js	(revision 323)
@@ -7,5 +7,4 @@
 	var	_window		= new Windows(_xtools, _im_content);
 	var _cookies	= new IMCookies;
-	var idTimeout	= "";
 	var Timeout		= "";
 
@@ -37,5 +36,5 @@
 		_win_preferences.bx.style.display = 'none';
 		_win_preferences.wc.style.padding = '5px';
-		
+
 		var _params = {
 			'lang1':imGetLang('Options'),
@@ -76,5 +75,5 @@
 		var preferences = arguments[0].split(';');
 			preferences.pop();
-		
+
 		for(var i in preferences)
 		{
@@ -94,5 +93,5 @@
 			else
 			{
-				Timeout = ( parseInt(value) > 0 ) ? value : "1"; 
+				Timeout = ( parseInt(value) > 0 ) ? value : "1";
 				if( top.document.getElementById(field) )
 					top.document.getElementById(field).value = Timeout;
@@ -104,5 +103,5 @@
 	{
 		var preferences = "";
-		
+
 		if((preferences = _cookies.get('preferences_im')))
 			_preferences(preferences);
@@ -114,8 +113,8 @@
 	{
 		_win_preferences = _window.get('preferences_window');
-	
+
 		var preferences = "";
 		var numbers = /^\d+$/;
-		
+
 		// Offline
         if (top.document.getElementById('ch_offline').checked)
@@ -129,5 +128,5 @@
 			_cookies.set('IM_unavailable', 'hidden');
 		}
-        
+
 		// TimeOut
 		if(numbers.test(top.document.getElementById('time_xa_im').value) && top.document.getElementById('time_xa_im').value != "0")
@@ -141,5 +140,5 @@
 			Timeout = 1;
 		}
-       
+
 		// New Message
 		if(top.document.getElementById('rd_nm').checked)
@@ -150,5 +149,5 @@
 		else
 			preferences += 'rd_nm:false;';
-        
+
 		if(top.document.getElementById('rd_ch').checked)
 		{
@@ -175,65 +174,24 @@
 	}
 
-	function _setPresence()
-	{
-		var _presence = false;
-		var _pPresence;
-		if ( arguments.length == 0 )
-		{
-			if ( !(_pPresence = _cookies.get('IM_presence')) )
-				_pPresence = 'available';
-		}
-		else
-			_pPresence = arguments[0];
-
-		_cookies.set('IM_presence', _pPresence);
-
-		switch ( _pPresence )
-		{
-			case 'away':
-			case 'dnd':
-			case 'xa':
-				_presence = 'show=' + _pPresence;
-			break;
-			case 'available':
-			case 'unavailable':
-				_presence = 'type=' + _pPresence;
-			break;
-		}
-
-		if ( _presence )
-		{
-			var _img_status = eval('im_' + _pPresence + '.src');
-
-			var _status = top.document.getElementById('im_status');
-			if ( _status )
-				_status.style.backgroundImage = 'url(' + _img_status + ')';
-
-			var _win_contacts = _window.get('contact_list');
-			if ( _win_contacts )
-				_win_contacts.icon(_img_status);
-
-			_conn.go('$this.Ujabber.setPresence', _presence);
-		}
-	}
-
+	var _changed = false;
+	var _id_timeout	= "";
 	function _setTimeOut()
 	{
-		_setPresence('xa');
-		idTimeout = setTimeout('top.window.im_preferences.timeOut();', parseInt(Timeout) * 60000);
+		_changed = true;
+		InstantMessengerPresence('auto');
 	}
 
 	function _timeOut()
 	{
-		if(idTimeout)
-		{
-			clearTimeout(idTimeout);
-			var _status = '';
-		
-			if((_status = _cookies.get('IM_presence')) === 'xa')
-				_setPresence('available');
-		}
-		
-		idTimeout = setTimeout('top.window.im_preferences._setTimeOut("xa");', parseInt(Timeout) * 60000);
+		if ( _id_timeout )
+			clearTimeout(_id_timeout);
+
+		if ( _changed )
+		{
+			_changed = false;
+			InstantMessengerPresence();
+		}
+
+		_id_timeout = setTimeout(_setTimeOut, parseInt(Timeout) * 60000);
 	}
 
@@ -243,5 +201,5 @@
 		_timeOut();
 	}
-	
+
 	Preferences.prototype._setTimeOut	 = _setTimeOut;
 	Preferences.prototype.timeOut		 = _timeOut;
@@ -249,5 +207,5 @@
 	Preferences.prototype.getfocus		 = focus_select;
 	top.window.im_preferences			 = new Preferences;
-	top.window.document.onmousemove		 = _timeOut;
+	_config(top.window.document, 'onmousemove', _timeOut);
 
 })();
