Index: trunk/prototype/modules/calendar/js/helpers.js
===================================================================
--- trunk/prototype/modules/calendar/js/helpers.js	(revision 5353)
+++ trunk/prototype/modules/calendar/js/helpers.js	(revision 5354)
@@ -223,5 +223,4 @@
 									 */
 									if (!clicked) {
-										zebraDiscardEventDialog = true;
 										window.setTimeout(function() {
 											zebraDiscardEventDialog = false;
@@ -298,5 +297,5 @@
 		UI.dialogs.addEvent.find('input[name="defaultAlarm"]').click(function(){
 		    if($(this).attr("checked")){
-				$('input[name = "isDefaultAlarm[]"]').parent().remove();
+				$('input[name="isDefaultAlarm[]"]').parent().remove();
 				var calendarSelected = Calendar.signatureOf[$('select[name="calendar"]').val()];
 				calendarSelected.useAlarmDefault = 1;
@@ -310,6 +309,7 @@
 						});
 				}
-			}else
-			    $('input[name = "isDefaultAlarm[]"]').parent().remove();
+			} else {
+			    $('input[name="isDefaultAlarm[]"]').parent().remove();
+			}
 	    });
 	    /* Checkbox allday */
@@ -329,25 +329,25 @@
 
 	 // ==== validation events ====
-	    $(".input-group .h1").Watermark("Evento sem título");
-		if(User.preferences.hourFormat.length == 5)
-			$(".new-event-win.active .end-time, .new-event-win.active .start-time").mask("99:99");
-		else{
+	    UI.dialogs.addEvent.find(".input-group .h1").Watermark("Evento sem título");
+		if(User.preferences.hourFormat.length == 5) {
+			UI.dialogs.addEvent.find(".end-time, .start-time").mask("99:99");
+		} else {
 			$.mask.definitions['{']='[ap]';
 			$.mask.definitions['}']='[m]';
-			$(".new-event-win.active .end-time, .new-event-win.active .start-time").mask("99:99 {}",
-				{completed:function(){
+			UI.dialogs.addEvent.find(".end-time, .start-time").mask("99:99 {}", {
+				completed:function(){
 					$(this).val(defaultToAmPm($(this).val()));
 					$(this).timepicker("refresh");
 					$(this).val($(this).val().replace(/[\.]/gi, ""));
-				}});
+				}
+			});
 		}
-		$(".number").numeric();
+		UI.dialogs.addEvent.find(".number").numeric();
 		if(User.preferences.dateFormat.indexOf('-') > 0)
-			$(".date").mask("99-99-9999");
+			UI.dialogs.addEvent.find(".date").mask("99-99-9999");
 		else
-			$(".date").mask("99/99/9999");
+			UI.dialogs.addEvent.find(".date").mask("99/99/9999");
 
 	    UI.dialogs.addEvent.find(".menu-addevent")
-		
 	    .children(".delete").click(function(){
 			$.Zebra_Dialog('Tem certeza que deseja excluir o evento?', {
@@ -371,10 +371,9 @@
 	    .children(".save").click(function(){
 			/* Validação */
-			
-		var msg = false;			
-		if(msg = validDateEvent()){
-			$(".new-event-win.active").find('.messages-validation').removeClass('hidden').find('.message label').html(msg); 
-			return false;
-		}
+			var msg = false;			
+			if(msg = validDateEvent()){
+				$(".new-event-win.active").find('.messages-validation').removeClass('hidden').find('.message label').html(msg); 
+				return false;
+			}
 			
 			canDiscardEventDialog = true;
@@ -419,11 +418,11 @@
 	    });
 	    
-	    UI.dialogs.addEvent.find('.button-add-attachment').click(function(){
-		    var li_attach = 
-			    '<li class="input_label">' + 
-				    '<input type="file" name="attachments[]"" />' +
-			    '</li>';
-		    jQuery('.attachments-list').append(li_attach);
-	    });
+//	    UI.dialogs.addEvent.find('.button-add-attachment').click(function(){
+//		    var li_attach = 
+//			    '<li class="input_label">' + 
+//				    '<input type="file" name="attachments[]"" />' +
+//			    '</li>';
+//		    jQuery('.attachments-list').append(li_attach);
+//	    });
 	    
 	    DataLayer.render( 'templates/attendee_add.ejs', {event:objEvent}, function( attendeeHtml ){
@@ -441,10 +440,10 @@
 			//blkAddAtendee.find('.block-attendee-list #attendees-users li').click(show_permissions_attendees); 
 
-			$(".attendee-list-add .add-attendee-input input").Watermark("digite um email para convidar");
+			UI.dialogs.addEvent.find(".attendee-list-add .add-attendee-input input").Watermark("digite um email para convidar");
 			/* Trata a edição de um novo participante adicionado
 			* 
 			*/
 			var hasNewAttendee = false;
-			$('.attendee-list-add .add-attendee-input input').keydown(function(event) {
+			UI.dialogs.addEvent.find('.attendee-list-add .add-attendee-input input').keydown(function(event) {
 				
 				if (event.keyCode == '13' && $(this).val() != '') {
@@ -453,5 +452,6 @@
 					
 					newAttendeeEmail = false;
-					newAttendeeName = false;
+					newAttendeeName  = false;
+					skipAddNewLine   = false;
 
 					var info = $(this).val();
@@ -475,4 +475,38 @@
 					var user = DataLayer.get('user', ["=", "mail", $(this).val()], true);
 					
+					
+					/**
+					 * guarda o último tooltip aberto referente à lista de participantes 
+					 */
+					lastEditAttendeeToolTip = [];
+
+					/**
+					 * Valida email e salva um participante externo 
+					 */
+					var saveContact = function() {
+						Encoder.EncodeType = "entity";
+
+						var currentTip = $('.qtip-active');
+						newAttendeeName  = currentTip.find('input[name="name"]').val();
+						newAttendeeEmail = currentTip.find('input[name="mail"]').val();
+
+						var isValidMail = newAttendeeEmail.match(/^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/);
+						if (!isValidMail) {
+							currentTip.find('.messages').removeClass('hidden').find('.message label').append('Email inválido.');
+							return false;
+						}
+
+						DataLayer.put('user', {id:newAttendeeId, name:newAttendeeName, mail:newAttendeeEmail, isExternal:isExternal});
+
+						$('dd.attendee-list ul.attendee-list li:last label')
+						.filter('.name').html(Encoder.htmlEncode(newAttendeeName)).attr('title', Encoder.htmlEncode(newAttendeeName)).end()
+						.filter('.mail').html(Encoder.htmlEncode(newAttendeeEmail)).attr('title', Encoder.htmlEncode(newAttendeeEmail));
+
+						return true;
+					}
+						
+					/**
+					 * Formata e adequa um tootip abert para edição de um participante na lista
+					 */
 					var onShowToolTip = function(arg0) {
 						$('.qtip-active .button.close').button({
@@ -483,5 +517,5 @@
 						.filter('.save').click(function(event, ui) {
 							if (saveContact()) {						
-								$('dd.attendee-list li:last').qtip("destroy");
+								lastEditAttendeeToolTip.qtip("destroy");
 							} else {
 								return false;
@@ -489,6 +523,5 @@
 						}).end()
 						.filter('.cancel').click(function(event, ui) {
-
-							$('dd.attendee-list li:last').qtip("destroy");
+							lastEditAttendeeToolTip.qtip("destroy");
 						})
 
@@ -500,8 +533,8 @@
 							if (event.keyCode == '13') {						
 								if (saveContact()) {						
-									$('dd.attendee-list li:last').qtip("destroy");
+									lastEditAttendeeToolTip.qtip("destroy");
 								}
 
-								$('dd.attendee-list li:last').qtip("destroy");
+								lastEditAttendeeToolTip.qtip("destroy");
 								event.preventDefault();
 							}
@@ -512,10 +545,27 @@
 
 					
-					if($('label.mail[title="' + $(this).val() + '"]').length) {
-						$(".new-event-win.active").find('.email-validation').removeClass('hidden')
+					/**
+					 * Se o email digitado já foi adicionado na lista,
+					 * o usuário deve ser avisado e um botão de edição deve ser exibido
+					 */
+					if(blkAddAtendee.find('label.mail[title="' + newAttendeeEmail + '"]').length) {
+						newAttendeeName = blkAddAtendee.find('label.mail[title="' + newAttendeeEmail + '"]').parents('li').find('label.name').attr('title');
+
+						blkAddAtendee.find('.email-validation').removeClass('hidden')
 						.find('.message label').html("O usuário acima já foi adicionado! <a class=\"small button\">Editar</a>")
 						.find(".button").button().click(function () { 
-							$("ul.attendee-list").scrollTo('label.mail[title="' + $(".add-attendee-input input").val() + '"]');
-							blkAddAtendee.find('ul.attendee-list label.mail[title="' + $(".add-attendee-input input").val() + '"]').parents('li').qtip({
+							/**
+							 * Se o usuário optar por editar o participante anteriormente adicionado,
+							 * uma tooltip deve ser aberta para este participante, viabilizando a edição
+							 */
+							blkAddAtendee.find("ul.attendee-list").scrollTo('label.mail[title="' + newAttendeeEmail + '"]');
+							/**
+							 * Remove tooltip possivelmente existente
+							 */
+							if (hasNewAttendee && lastEditAttendeeToolTip.length && lastEditAttendeeToolTip.data('qtip'))
+								lastEditAttendeeToolTip.qtip('destroy');
+					
+							lastEditAttendeeToolTip = blkAddAtendee.find('label.mail[title="' + newAttendeeEmail + '"]').parents('li');
+							lastEditAttendeeToolTip.qtip({
 								show: {ready: true, solo: true, when: {event: 'click'}},
 								hide: false,
@@ -536,7 +586,7 @@
 								}
 							});
-						}); 	
-						blkAddAtendee.find('ul.attendee-list label.mail[title="' + $(".add-attendee-input input").val() + '"]').parents('li').qtip("api").onShow = onShowToolTip;
-						return false;
+							lastEditAttendeeToolTip.qtip("api").onShow = onShowToolTip;
+						});
+						skipAddNewLine = true;
 					}
 					
@@ -547,6 +597,6 @@
 					 * Remove tooltip possivelmente existente
 					 */
-					if (hasNewAttendee && blkAddAtendee.find('dd.attendee-list li:last').data('qtip'))
-						blkAddAtendee.find('dd.attendee-list li:last').qtip('destroy');
+					if (hasNewAttendee && lastEditAttendeeToolTip.length && lastEditAttendeeToolTip.data('qtip'))
+						lastEditAttendeeToolTip.qtip('destroy');
 
 					var newAttendeeId = '';
@@ -558,18 +608,20 @@
 							newAttendeeId = 'internal:'+user[0].id;
 
-						blkAddAtendee.find('dd.attendee-list ul.attendee-list').append(
-							DataLayer.render('templates/participants_add_itemlist.ejs', [{id:newAttendeeId, name: user[0].name, mail: user[0].mail}])
-						)
-						.find('.button.close').addClass('small').button({text:false, icons:{primary:'ui-icon-close'}}).click(function() {
-							var participant =  DataLayer.get('participant', ['AND',['=', 'user', $(this).parent().find('[type=checkbox]').val().replace(/[a-z:]/g, "")],['=', 'schedulable',$(this).parents().find('.id-event').first().val()]]);
-							if(participant)
-								DataLayer.remove('participant', participant[0].id);
-							$(this).parent().remove();
-						}).end()
-						.scrollTo('max');
+						if (!skipAddNewLine) {
+							blkAddAtendee.find('dd.attendee-list ul.attendee-list').append(
+								DataLayer.render('templates/participants_add_itemlist.ejs', [{id:newAttendeeId, name: user[0].name, mail: user[0].mail}])
+							)
+							.find('.button.close').addClass('small').button({text:false, icons:{primary:'ui-icon-close'}}).click(function() {
+								var participant =  DataLayer.get('participant', ['AND',['=', 'user', $(this).parent().find('[type=checkbox]').val().replace(/[a-z:]/g, "")],['=', 'schedulable',$(this).parents().find('.id-event').first().val()]]);
+								if(participant)
+									DataLayer.remove('participant', participant[0].id);
+								$(this).parent().remove();
+							}).end()
+							.scrollTo('max');
+						}
 						
 						$(this).val('');
 
-					} else {		
+					} else if (!skipAddNewLine) {		
 						/**
 						 * a) salva como novo usuario externo no banco (apenas com email) e...
@@ -579,4 +631,5 @@
 						newAttendeeId = DataLayer.put('user', {name: newAttendeeName, mail: newAttendeeEmail, isExternal: isExternal});
 
+						 
 						blkAddAtendee.find('dd.attendee-list ul.attendee-list').append(
 							DataLayer.render('templates/participants_add_itemlist.ejs', [{id:newAttendeeId, name: newAttendeeName, mail: newAttendeeEmail}])
@@ -595,5 +648,6 @@
 						 * recém adicionado.
 						 */
-						blkAddAtendee.find('dd.attendee-list li:last').qtip({
+						lastEditAttendeeToolTip = blkAddAtendee.find('dd.attendee-list li:last');
+						lastEditAttendeeToolTip.qtip({
 							show: {ready: true, solo: true, when: {event: 'click'}},
 							hide: false,
@@ -614,36 +668,14 @@
 							}
 						});
-
-						var saveContact = function() {
-							Encoder.EncodeType = "entity";
-			
-							var currentTip = $('.qtip-active');
-							newAttendeeName  = currentTip.find('input[name="name"]').val();
-							newAttendeeEmail = currentTip.find('input[name="mail"]').val();
-
-							var isValidMail = newAttendeeEmail.match(/^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/);
-							if (!isValidMail) {
-								currentTip.find('.messages').removeClass('hidden').find('.message label').append('Email inválido.');
-								return false;
-							}
-
-							DataLayer.put('user', {id:newAttendeeId, name:newAttendeeName, mail:newAttendeeEmail, isExternal:isExternal});
-
-							$('dd.attendee-list ul.attendee-list li:last label')
-							.filter('.name').html(Encoder.htmlEncode(newAttendeeName)).attr('title', Encoder.htmlEncode(newAttendeeName)).end()
-							.filter('.mail').html(Encoder.htmlEncode(newAttendeeEmail)).attr('title', Encoder.htmlEncode(newAttendeeEmail));
-
-							return true;
-						}
+			
+						lastEditAttendeeToolTip.qtip("api").onShow = onShowToolTip;
+
+						$(this).val('');
 
 						
-						$('dd.attendee-list li:last').qtip("api").onShow = onShowToolTip;
-
-
-						$(this).val('');
-
-						event.preventDefault();
 					}
+					event.preventDefault();
 				}
+				
 			});
 
