Index: sandbox/jabberit_messenger/trophy_expresso/js/jscode/loadIM.js
===================================================================
--- sandbox/jabberit_messenger/trophy_expresso/js/jscode/loadIM.js	(revision 2689)
+++ sandbox/jabberit_messenger/trophy_expresso/js/jscode/loadIM.js	(revision 2696)
@@ -1,11 +1,14 @@
 (function()
 {
+	var _autoStatus;
+	var _autoStatusTime = 60000; // 1 minuto	
+	var addUser			= null;
+	var conn			= null;
 	var fullName		= "";
 	var path			= "";
-	var addUser			= null;
-	var conn			= null;
+	var _preferencesIM	= "";
 	var selectEditable	= null;
 	var showhidden 		= null;
-	var timeoutId		= null
+	var timeoutId		= null;
 	var userCurrent		= null;
 	var Xtools			= null;
@@ -151,4 +154,50 @@
 	}
 
+	function autoStatus()
+	{
+		var _div_status = ( getElement('status_jabber_expresso') != null ) ?  getElement('status_jabber_expresso') : null;
+		
+		if ( _autoStatus )
+			clearTimeout(_autoStatus);
+
+		if ( _div_status != null )
+		{
+			var _status = _div_status.style.backgroundImage;
+				_status = _status.substr(_status.lastIndexOf('/') + 1);
+				_status = _status.substr(0, _status.indexOf('.'));
+				
+			if( _status == "xa" )
+			{
+				TrophyIM.setPresence("available");
+				loadscript.setStatusJabber("Disponível","available");
+			}
+		}
+		
+		var TimeStatus = _preferencesIM[2].split(':');
+
+		if( TimeStatus[1] )
+			_autoStatus = setTimeout( function(){ autoStatusHandler();}, parseInt(TimeStatus[1]) * _autoStatusTime );
+		else
+			_autoStatus = setTimeout( function(){ autoStatusHandler();}, parseInt(_autoStatusTime));
+	}
+	
+	function autoStatusHandler()
+	{
+		var _div_status = ( getElement('status_jabber_expresso') != null ) ?  getElement('status_jabber_expresso') : null;
+		
+		if ( _div_status != null )
+		{
+			var _status = _div_status.style.backgroundImage;
+				_status = _status.substr(_status.lastIndexOf('/') + 1);
+				_status = _status.substr(0, _status.indexOf('.'));
+			
+			if( _status == "available" )
+			{
+				TrophyIM.setPresence("xa");
+				loadscript.setStatusJabber("Não Disponível","xa");
+			}
+		}
+	}
+
 	function clrAllContacts()
 	{
@@ -182,5 +231,5 @@
 			                	['Adicionar Contato', 'loadscript.addContact();' ],
 			                	['Ajuda', 'alert("2")' ],
-			                	['Preferências', 'alert("3")'],
+			                	['Preferências', 'loadscript.preferences();'],
 						   ];
 
@@ -448,4 +497,78 @@
 		if( arguments.length === 3 )
 			return Xtools.parse(Xtools.xml(arguments[0]), arguments[1], arguments[2] );
+	}
+	
+	function preferences()
+	{
+		var paramPreferences =
+		{
+			'path'  : path,
+			'lang1' : 'Suas Preferências',
+			'lang2' : 'Conexão',	
+			'lang3' : 'Conectar Automaticamente o IM',
+			'lang4' : 'Usuários OffLine',
+			'lang5' : 'Exibir amigos Offline',
+			'lang6' : 'Salvar',
+			'lang7' : 'Cancelar',
+			'lang8' : 'Janela de Contatos',
+			'lang9' : 'Abrir janela como Pop-up',
+			'lang10' : 'Ausente',
+			'lang11' : 'Definir status de ausente depois de',
+			'lang12' : 'minutos',						   
+			'langYes': 'Sim',
+			'langNo' : 'Não'											   
+	  	};
+		
+		
+		var _win_preferences =
+		{ 
+				id_window	 : "jabberit_preferences",
+				width		 : 430,
+				height		 : 330,
+				top		 	 : 150,
+				left		 : 100,
+				draggable	 : true,
+				visible	 	 : "display",
+				resizable	 : true,
+				zindex		 : zIndex++,
+				title		 : 'Expresso Messenger - Preferências',
+				closeAction  : "remove",
+				content		 : Xtools.parse(Xtools.xml('preferences'), 'preferences.xsl', paramPreferences)
+		};
+
+		_winBuild(_win_preferences);
+		
+		
+		var _pButtons = {
+				'lang1' : 'Salvar',
+				'lang2' : 'Fechar',
+				'onclickClose' : '_winBuild("jabberit_preferences","remove");',
+				'onclickSubmit' : 'javascript:loadscript.setPreferences();'
+		}; 
+		
+		document.getElementById('buttons_preferences_jabberit').innerHTML = Xtools.parse(Xtools.xml('buttons_main'), 'buttons.xsl', _pButtons);
+		
+		// Element openWindowJabberit
+		var value1			= _preferencesIM[0].split(':');
+		var element1		= document.getElementById(value1[0]);
+		var valueSelect1	= value1[1];
+		
+		for(var i = 0; i < element1.options.length; i++)
+			if( element1.options[i].value == valueSelect1 )
+				element1.options[i].selected = true;
+
+		// Element openWindowJabberitPopUp
+		var value2			= _preferencesIM[1].split(':');
+		var element2		= document.getElementById(value2[0]);
+		var valueSelect2	= value2[1];
+			
+		for(var i = 0; i < element2.options.length; i++)
+			if( element2.options[i].value == valueSelect2 )
+				element2.options[i].selected = true;
+
+		// Element flagAwayIM
+		var value3		= _preferencesIM[2].split(':');
+		var element3	= document.getElementById(value3[0]);
+		element3.value	= value3[1];
 	}
 	
@@ -592,4 +715,67 @@
 	}
 	
+	
+	function setPreferences()
+	{
+		// Element openWindowJabberit
+		var elementOpenW	= document.getElementById('openWindowJabberit');
+		var value			= '';
+		var flagReload		= false;
+		
+		for(var i = 0 ; i < elementOpenW.options.length; i++)
+			if( elementOpenW.options[i].selected == true)
+			{
+				value = 'preferences1=openWindowJabberit:' + elementOpenW.options[i].value;
+				_preferencesIM[0] = 'openWindowJabberit:' + elementOpenW.options[i].value;
+			}
+
+		// Element openWindowJabberitPopUp
+		var elementOpenPop = document.getElementById('openWindowJabberitPopUp');	
+
+		for(var i = 0; i < elementOpenPop.options.length; i++ )
+			if( elementOpenPop.options[i].selected ==  true )
+			{
+				value += '&preferences2=openWindowJabberitPopUp:' + elementOpenPop.options[i].value;
+				
+				if( _preferencesIM[1] != 'openWindowJabberitPopUp:' + elementOpenPop.options[i].value)
+					flagReload = true;
+					
+				_preferencesIM[1] = 'openWindowJabberitPopUp:' + elementOpenPop.options[i].value;
+			}
+		
+		// Element flagAwayIM
+		var elementFlagIM = document.getElementById('flagAwayIM');
+		
+		if( elementFlagIM.value.length > 0 && parseInt(elementFlagIM.value) > 0 )
+		{
+			_preferencesIM[2] = 'flagAwayIM:' + elementFlagIM.value;
+			value += '&preferences3=flagAwayIM:' + elementFlagIM.value;
+		}
+		else
+		{
+			alert('Informe um valor igual ou maior que 1!');
+			return false;
+		}
+		
+		
+		// Save Preferences
+		conn.go('p.pf.setPreferences',
+				 function(data)
+				 {
+					if(data == 'true')
+					{
+						if( flagReload )
+						{
+							window.location.reload();
+							myWindow.close();							
+						}
+					}
+					else{ alert('Erro salvando suas preferências!'); }
+
+					_winBuild('jabberit_preferences', 'remove');
+				 },
+				 value);
+	}
+	
 	function setPresence()
 	{
@@ -705,4 +891,7 @@
 			fullName = arguments[0];
 			
+			// Preferences
+			_preferencesIM = arguments[1].split(";");
+
 			loadScripts(files);
 			
@@ -733,4 +922,17 @@
 					return true;
 				};
+				
+				
+				// Auto Connect
+				setTimeout(function()
+				{
+					if( _preferencesIM[0] === 'openWindowJabberit:true' )
+						TrophyIM.load();
+				},1500);
+				
+				// Auto Status
+				autoStatus();
+				configEvents( document, 'onmousemove', autoStatus );
+				configEvents( document, 'onkeypress', autoStatus );
 
 			}, 2000);
@@ -752,6 +954,8 @@
 	loadIM.prototype.notification		= notificationNewMessage;
 	loadIM.prototype.parse				= parse;
+	loadIM.prototype.preferences		= preferences;
 	loadIM.prototype.searchUser			= searchUser
 	loadIM.prototype.setAutorization	= setAutorization;
+	loadIM.prototype.setPreferences		= setPreferences;
 	loadIM.prototype.setPresence		= setPresence;
 	loadIM.prototype.setStatusJabber	= setStatusJabber;
