Index: /trunk/prototype/modules/mail/js/followupflag.js
===================================================================
--- /trunk/prototype/modules/mail/js/followupflag.js	(revision 5575)
+++ /trunk/prototype/modules/mail/js/followupflag.js	(revision 5576)
@@ -1,72 +1,2 @@
-/**
- * constrói as duas possíveis janela de alerta, utilizando o mesmo template
- */
-var toIterate = [
-	{
-		attrDeadline: 'doneDeadline', 
-		caption: {singular:'Você tem uma mensagem concluindo hoje:', plural:"Você tem %1 mensagens concluindo hoje:"},
-		onOk: function(event){
-			if($(event.target).parents('.followupflag-alarm').find('[name="stopAlert"]').is(':checked')){
-				$.cookie("fadeCompleted", (new Date).toString("dd/MM/yyyy"), { expires: 1 });
-			}
-		},
-		enable: ($.cookie("fadeCompleted") != (new Date).toString("dd/MM/yyyy"))
-	},
-	{
-		attrDeadline: 'alarmDeadline', 
-		caption: {singular:'Você tem uma mensagem sinalizada para hoje:', plural:'Você tem %1 mensagens sinalizadas para hoje:'},
-		onOk: function(event){
-			if($(event.target).parents('.followupflag-alarm').find('[name="stopAlert"]').is(':checked')){
-				$.cookie("fadeAlarm", (new Date).toString("dd/MM/yyyy"), { expires: 1 });
-			}
-		},
-		enable: ($.cookie("fadeAlarm") != (new Date).toString("dd/MM/yyyy"))
-	}
-];
-
-while (it = toIterate.pop()){
-	if (!it.enable) continue;
-	
-	var alarmInRange = DataLayer.get('followupflagged', {filter: ['AND', ['>', it.attrDeadline, (new Date()).set({hour:0, minute:0, second:0}).toString('yyyy-MM-dd 00:00:00')], ['<', toIterate.attrDeadline, (new Date()).set({hour:0, minute:0, second:0}).addHours(24).toString('yyyy-MM-dd 00:00:00')]], criteria: {deepness: 1}});
-	if(alarmInRange.length > 0){
-		for(var i=0; i<alarmInRange.length; i++){
-			var date = Date.parseExact(alarmInRange[i].alarmDeadline, 'yyyy-MM-dd hh:mm:ss');
-			alarmInRange[i].alarmDeadline = date.toString('hh:mm');
-		}
-		var caption = (alarmInRange.length == 1) ?
-			get_lang(it.caption.singular):
-			get_lang(it.caption.plural, alarmInRange.length);
-			
-		var data = {alarmInRange : alarmInRange, caption: caption};
-		var dialogText = DataLayer.render("../prototype/modules/mail/templates/followupflag_alarm_list.ejs", data);
-		
-		/**
-		 * A última dialog criada é a dialog mais ao alto
-		 */
-		var offsetHeight = $('.ZebraDialog.right-bottom:last').length ? 
-			$(window).height() - $('.ZebraDialog.right-bottom:last').position().top : 0;
-
-		/**
-		 * Se for a primeira, desloca para cima do rodapé do grid
-		 * caso contrário desloca para um pouco acima da última dialog criada
-		 */
-		offsetHeight = !offsetHeight ? 35 : offsetHeight + 20 ;
-		var dialog = new $.Zebra_Dialog(dialogText, {
-			'buttons':  ['Ok'],
-			'modal': false,
-			'vcenter_short_message': false,
-			'position': ['right - 20', 'bottom - ' + offsetHeight.toString() ],
-			'custom_class': 'right-bottom followupflag-alarm',
-		}).dialog;
-
-		$(".ZebraDialog.followupflag-alarm .ZebraDialog_Button0").click(it.onOk);
-		
-		$(".ZebraDialog.followupflag-alarm .display-options .more").click(function(event) {
-			$(event.target).parents('.followupflag-alarm').find('.message-list').css('height', 'auto');
-		});
-		
-	}		
-}
-
 function init_followup(data){
 
@@ -124,4 +54,5 @@
 									}
 								});
+								/*
 								if ( !valid ) {
 									// remove invalid value, as it didn't match anything
@@ -130,4 +61,13 @@
 									input.data( "autocomplete" ).term = "";
 									return false;
+								}
+								*/
+								if ( !valid ) {
+									if(select.has('option[value="custom"]').length > 0) {
+										select.find('option:last').val('custom').html($(this).val()).attr('selected', 'selected');
+									} else {
+										select.append(select.find('option:last').clone().val('custom').html($(this).val()));
+										select.find('option[value="custom"]').attr('selected', 'selected');
+									}
 								}
 							}
@@ -192,4 +132,14 @@
 		var backgroundColor = winElement.find('[name="backgroundColor"]').val();
 		var followupflagId  = winElement.find('[name="name"] option:selected').val();
+		if (followupflagId == 'custom') {
+			DataLayer.put('followupflag', {name:winElement.find('[name="name"] option:selected').text(), uid:User.me.id});
+			DataLayer.commit(false, false, function(data){
+				followupflags = DataLayer.get('followupflag',{criteria:{order: 'id'}}, true);		
+				followupflagId = followupflags[followupflags.length - 1].id;
+				winElement.find('[name="name"] option[value="custom"]').val(followupflagId);
+			});
+		}
+		
+		
 		var isDone = winElement.find('[name="done"]').is(':checked') ? 1 : 0;
 		var alarmDate = false;
@@ -209,13 +159,10 @@
 			folders.push(folder_name);
 			
-			//var followupflag = DataLayer.get('followupflag', followupflagId.toString()),
-			var idFollowupflagged = winElement.find('.id-followup').val();
+			var idFollowupflagged = winElement.find('[name="followupflagId"]').val();
 			var followupflagged = DataLayer.merge({
 				uid : User.me.id,
 				followupflagId : followupflagId, 
 				folderName : folder_name, 
-				messageNumber : messageNumber, 
-				//alarmDeadline : winElement.find('[name="alarmDate"]').datepicker("getDate"), 
-				//doneDeadline: winElement.find('[name="doneDate"]').datepicker("getDate"),
+				messageNumber : messageNumber,
 				isDone: isDone,
 				backgroundColor : backgroundColor
@@ -236,4 +183,7 @@
 			}
 			
+			/**
+			 * Aplica o ícone correspondente na lista de mensagens do expressoMail
+			 */
 			var flagged = $('#td_message_followup_' + messageNumber + ', ' + '#td_message_followup_search_' + messageNumber+':first').find(".flag-edited");
 			if(isDone){
@@ -245,19 +195,29 @@
 			$('#td_message_followup_' + messageNumber + ', ' + 
 			  '#td_message_followup_search_' + messageNumber+':first').find(".flag-edited").css("background", backgroundColor); 
+			  
+			/**
+			 * Salva ou, caso já exista, atualiza
+			 */
 			DataLayer.put('followupflagged', followupflagged);
 		}
+		
 		DataLayer.commit(false, false, function(data){
 			followupflags = DataLayer.get('followupflagged',{criteria:{order: 'id'}}, true);		
 			var id = followupflags[followupflags.length - 1].id;
-			winElement.find('.id-followup').val(id);
-		});		
+			winElement.find('[name="followupflagId"]').val(id);
+			winElement.find('.menu-configure-followupflag .save').button("option", "disabled", true);
+		});
+		
 		var extend = DataLayer.get('followupflagged', {
-		filter: ['AND', ['IN', 'messageNumber', selectedMessageIds], ['IN', 'folderName', folders]],criteria: {deepness: 1}});
+			filter: ['AND', ['IN', 'messageNumber', selectedMessageIds], ['IN', 'folderName', folders]],
+			criteria: {deepness: 1}
+		});
+		
 		if(extend != "" || extend != 'undefined' || extend != []){
-			for(var ex = 0; ex < extend.length; ex++){
-				if(onceOpenedHeadersMessages[extend[ex].folderName]){
-					if(onceOpenedHeadersMessages[extend[ex].folderName][extend[ex].messageNumber]){
-						onceOpenedHeadersMessages[extend[ex].folderName][extend[ex].messageNumber]['followupflagged'] = {};
-						DataLayer.merge(onceOpenedHeadersMessages[extend[ex].folderName][extend[ex].messageNumber]['followupflagged'], extend[ex]);
+			for(var i = 0; i < extend.length; i++){
+				if(onceOpenedHeadersMessages[extend[i].folderName]){
+					if(onceOpenedHeadersMessages[extend[i].folderName][extend[i].messageNumber]){
+						onceOpenedHeadersMessages[extend[i].folderName][extend[i].messageNumber]['followupflagged'] = {};
+						DataLayer.merge(onceOpenedHeadersMessages[extend[i].folderName][extend[i].messageNumber]['followupflagged'], extend[i]);
 					}
 				}
@@ -270,5 +230,6 @@
 	
 	.filter('.menu-configure-followupflag .delete').click(function(){
-
+		if (selectedMessageIds.length == 0) $(this).button("option", "disabled", true);
+		
 		for(i = 0; i < selectedMessageIds.length; i++){
 			var folder_name;
@@ -280,5 +241,5 @@
 				folder_name = tr.getAttribute('name');
 			}
-			var messageNumber   = selectedMessageIds[i];
+			var messageNumber = selectedMessageIds[i];
 			if(onceOpenedHeadersMessages[folder_name][messageNumber]['followupflagged']){
 				if(onceOpenedHeadersMessages[folder_name][messageNumber]['followupflagged'].id){
@@ -297,11 +258,25 @@
 		DataLayer.commit();
 		winElement.dialog("close");
-		
 	});
 
+				
+	if(!winElement.find('[name="followupflagId"]').val())
+		winElement.find('.menu-configure-followupflag .delete').button("option", "disabled", true);
+				
+	/**
+	 * Se houver mudança, habilita o botão "Save"
+	 */
+	winElement.find(':input').change(function(event){
+		if (event.keyCode != '27' && event.keyCode != '13')
+			$(this).button("option", "disabled", true);
+	}).keydown(function(event){
+		if (event.keyCode != '27' && event.keyCode != '13')
+			$(this).button("option", "disabled", true);
+	});	
+	
 	winElement.find('.date').datepicker();
 	winElement.find('.time').timepicker({});
 	winElement.find('[name="name"]').combobox();
-	
+
 	
 	winElement.find('[name="alarmDate"],[name="alarmTime"]').attr("disabled","disabled");
@@ -371,2 +346,78 @@
 
 }
+
+
+
+
+/**
+ * constrói as duas possíveis janela de alerta, utilizando o mesmo template
+ */
+
+$('#main_table').ready(function(){
+	var toIterate = [
+		{
+			attrDeadline: 'doneDeadline', 
+			caption: {singular:'Você tem uma mensagem concluindo hoje:', plural:"Você tem %1 mensagens concluindo hoje:"},
+			onOk: function(event){
+				if($(event.target).parents('.followupflag-alarm').find('[name="stopAlert"]').is(':checked')){
+					$.cookie("fadeCompleted", (new Date).toString("dd/MM/yyyy"), { expires: 1 });
+				}
+			},
+			enable: ($.cookie("fadeCompleted") != (new Date).toString("dd/MM/yyyy"))
+		},
+		{
+			attrDeadline: 'alarmDeadline', 
+			caption: {singular:'Você tem uma mensagem sinalizada para hoje:', plural:'Você tem %1 mensagens sinalizadas para hoje:'},
+			onOk: function(event){
+				if($(event.target).parents('.followupflag-alarm').find('[name="stopAlert"]').is(':checked')){
+					$.cookie("fadeAlarm", (new Date).toString("dd/MM/yyyy"), { expires: 1 });
+				}
+			},
+			enable: ($.cookie("fadeAlarm") != (new Date).toString("dd/MM/yyyy"))
+		}
+	];
+
+	while (it = toIterate.pop()){
+		if (!it.enable) continue;
+		
+		var alarmInRange = DataLayer.get('followupflagged', {filter: ['AND', ['>', it.attrDeadline, (new Date()).set({hour:0, minute:0, second:0}).toString('yyyy-MM-dd 00:00:00')], ['<', toIterate.attrDeadline, (new Date()).set({hour:0, minute:0, second:0}).addHours(24).toString('yyyy-MM-dd 00:00:00')]], criteria: {deepness: 1}});
+		if(alarmInRange.length > 0){
+			for(var i=0; i<alarmInRange.length; i++){
+				var date = Date.parseExact(alarmInRange[i].alarmDeadline, 'yyyy-MM-dd hh:mm:ss');
+				alarmInRange[i].alarmDeadline = date.toString('hh:mm');
+			}
+			var caption = (alarmInRange.length == 1) ?
+				get_lang(it.caption.singular):
+				get_lang(it.caption.plural, alarmInRange.length);
+				
+			var data = {alarmInRange : alarmInRange, caption: caption};
+			var dialogText = DataLayer.render("../prototype/modules/mail/templates/followupflag_alarm_list.ejs", data);
+			
+			/**
+			 * A última dialog criada é a dialog mais ao alto
+			 */
+			var offsetHeight = $('.ZebraDialog.right-bottom:last').length ? 
+				$(window).height() - $('.ZebraDialog.right-bottom:last').position().top : 0;
+
+			/**
+			 * Se for a primeira, desloca para cima do rodapé do grid
+			 * caso contrário desloca para um pouco acima da última dialog criada
+			 */
+			offsetHeight = !offsetHeight ? 35 : offsetHeight + 20 ;
+			var dialog = new $.Zebra_Dialog(dialogText, {
+				'buttons':  ['Ok'],
+				'modal': false,
+				'vcenter_short_message': false,
+				'position': ['right - 20', 'bottom - ' + offsetHeight.toString() ],
+				'custom_class': 'right-bottom followupflag-alarm',
+			}).dialog;
+
+			$(".ZebraDialog.followupflag-alarm .ZebraDialog_Button0").click(it.onOk);
+			
+			$(".ZebraDialog.followupflag-alarm .display-options .more").click(function(event) {
+				$(event.target).parents('.followupflag-alarm').find('.message-list').css('height', 'auto');
+			});
+			
+		}		
+	}
+});
Index: /trunk/prototype/modules/mail/templates/followupflag_configure.ejs
===================================================================
--- /trunk/prototype/modules/mail/templates/followupflag_configure.ejs	(revision 5575)
+++ /trunk/prototype/modules/mail/templates/followupflag_configure.ejs	(revision 5576)
@@ -45,5 +45,5 @@
 		<legend><%= get_lang("Basic")%></legend>
 		<div class="followupflag-configure">
-			<input type="hidden" class="id-followup" value="<%= (data.messages[0].followupflagged) ? data.messages[0].followupflagged.id : '' %>" name="followupFlag">
+			<input type="hidden" class="id-followup" value="<%=(followupflagged.id)?followupflagged.id:''%>" name="followupflagId">
 			<label class="label-name"><%= get_lang("Flag as")%>:</label> 
 			<select name="name">
Index: /trunk/expressoMail1_2/templates/default/index.tpl
===================================================================
--- /trunk/expressoMail1_2/templates/default/index.tpl	(revision 5571)
+++ /trunk/expressoMail1_2/templates/default/index.tpl	(revision 5576)
@@ -207,5 +207,5 @@
 <div id="sandbox" class="expresso-calendar-container hidden">
 </div>
-<div id="windowLabels" class="label-configure-win"></div>
-<div id="followupFlag" class="followupflag-configure-win"></div>
+<div id="windowLabels" class="label-configure-win" style="display: none;"></div>
+<div id="followupFlag" class="followupflag-configure-win" style="display: none;"></div>
 <!-- END list -->
Index: /trunk/expressoMail1_2/js/draw_api.js
===================================================================
--- /trunk/expressoMail1_2/js/draw_api.js	(revision 5575)
+++ /trunk/expressoMail1_2/js/draw_api.js	(revision 5576)
@@ -219,5 +219,5 @@
 				}
 			}
-
+		/*
 		tree_folders.draw(Element('content_folders'));
 		if (!expresso_offline) {
@@ -229,5 +229,5 @@
 			tree_folders.getNodeById('local_Inbox')._select();
 		}
-		
+		*/
 		var trash_span = document.getElementById(mount_url_folder(["lINBOX",special_folders["Trash"]])  + 'tree_folders'); 
 		var draft_span = document.getElementById(mount_url_folder(["lINBOX",special_folders["Drafts"]]) + 'tree_folders'); 
@@ -346,5 +346,5 @@
 	myLabels.html("<div class='my-labels'>" +
 				"<img src='../prototype/modules/mail/img/tag.png'/>" +
-				"<a class='title-my-labels' tabindex='0' role='button' aria-expanded='false' title='"+get_lang("My Label")+"'>"+get_lang("My Label")+"</a>" +
+				"<a class='title-my-labels' tabindex='0' role='button' aria-expanded='false' title='"+get_lang("My Labels")+"'>"+get_lang("My Label")+"</a>" +
 				
 				"<span class='status-list-labels ui-icon ui-icon-triangle-1-s'></span>" +
@@ -355,5 +355,5 @@
 		var labelId = $(this).parents("li").attr('class').match(/label-item-([\d]+[()a-zA-Z]*)/)[1];
 		search_emails("UNDELETED KEYWORD \"$Label"+labelId+"\"");
-	})
+	}).end()
 	.find(".square-color").css("display","").click(function(){
 		$(this).parent(".label-item").each(function(){
