Index: /trunk/prototype/app/datalayer.js
===================================================================
--- /trunk/prototype/app/datalayer.js	(revision 5343)
+++ /trunk/prototype/app/datalayer.js	(revision 5376)
@@ -391,13 +391,59 @@
     remove: function( concept, id, oneSide ){
       
-	var bothSides = (typeof oneSide === "undefined");
-
-	if( bothSides || !oneSide )
-	    this.del( concept, id );
-
-	if( bothSides )
-	    this.report( concept, id, false );
+	var bothSides = (typeof oneSide === "undefined"),
+
+	links = this.links( concept ), ids = [],
+
+	current = this.check( concept, id );
+
+	if( !current ) return;
+	
+	if( id )
+	    current.id = id;
+
+	if( notArray = ( $.type( current ) !== "array" ) )
+	    current = [ current ];
+
+	for( var i = 0; i < current.length; i++ )
+	{
+	    var currentId = ids[ ids.length ] = current[i].id;
+
+	    if( bothSides )
+	      this.report( concept, currentId, false );
+
+	    if( bothSides || !oneSide )
+	      this.del( concept, currentId );
+
+	    for( var link in links )
+	    {
+		if( !current[i][link] )
+		    continue;
+
+		var nestedLinks = this.links( links[link], true );
+
+		if( isConcept = this.isConcept( concept, link ) )
+		    current[i][link] = [ current[i][link] ];
+
+		$.each( current[i][link], function( ii, el ){
+
+			el = DataLayer.storage.cache[links[link]][el];
+
+			if( notArrayNested = ( $.type( el[ nestedLinks[concept] ] ) !== "array" ) )
+			    el[ nestedLinks[concept] ] = [ el[nestedLinks[concept]] ];
+
+			el[ nestedLinks[concept] ] = $.grep( el[ nestedLinks[concept] ], function( nested, iii ){
+			    return ( currentId !== nested );
+			});
+
+			if( notArrayNested )
+			    el[ nestedLinks[concept] ] = el[ nestedLinks[concept] ][0] || false;
+			if(!el[ nestedLinks[concept] ] || !el[ nestedLinks[concept] ].length)
+				delete el[ nestedLinks[concept] ];
+		});
+	    }
+	}
+
 	if( oneSide )
-	    this.commit( concept, id );
+	    this.commit( concept, ids );
 
 	this.broadcast( concept, oneSide ? 'server' : bothSides ? 'serverclient' : 'client', false );
@@ -411,5 +457,5 @@
 	    current[ id ] = this.check( concept, id ) || {};
 	
-	this.store( ':current', concept, current[ id ] );
+	this.store( ':current', concept, current );
 
 	var diff = this.diff( current[ id ], data );
@@ -640,6 +686,22 @@
 	    
     },
-
-    
+    
+    unique: function( origArr ){ 
+
+	var newArr = [];
+      
+	for ( var x = 0; x < origArr.length; x++ )
+	{
+		var found = false;
+	    for ( var y = 0; !found && y < newArr.length; y++ ) 
+		if ( origArr[x] === newArr[y] )  
+		  found = true;
+
+	    if ( !found ) 
+		newArr[ newArr.length ] = origArr[x];
+	}
+
+	return newArr;
+    },
 
     merge: function( current, data ){
@@ -752,8 +814,9 @@
 			    }
 
-			    var thisPass = this;
-
 			    // It's now safe to let the comparator recursively deep copy its properties.
-			    comparator.populate( function(source, result) { return thisPass.copy(source, result, depth); }, source, result );
+			    var returned = comparator.populate( function(source, result) { return DataLayer.copy(source, result, depth); }, source, result );
+	
+				if(returned)
+					result = returned;
 
 			    depth.depth--;
@@ -1111,5 +1174,5 @@
 	this.put( concept, newId, this.check( concept, oldId ), false );
 
-	this.del( concept, oldId );
+	this.remove( concept, oldId, false );
     },
     
@@ -1921,4 +1984,5 @@
 			result.push( deepCopy(source[i], result[i]) );
 		}
+		result =  DataLayer.unique( result )
 		return result;
 	}
@@ -1983,4 +2047,5 @@
 		      }
 	      }
+		return result;
       }
 });
Index: /trunk/prototype/Sync.php
===================================================================
--- /trunk/prototype/Sync.php	(revision 5341)
+++ /trunk/prototype/Sync.php	(revision 5376)
@@ -144,5 +144,6 @@
 		  unset( $data[$oldURI] );
 
-		  $return[ $oldURI ] = $tx['rollback'] ? 'ROLLBACK' : array( 'id' => $tx['id'] );
+		  $return[ $oldURI ] = !$tx['rollback'] ? $dt ? 
+				       array( 'id' => $tx['id'] ) : false : 'ROLLBACK';;
 	  }
     }
Index: /trunk/prototype/config/alarm.ini
===================================================================
--- /trunk/prototype/config/alarm.ini	(revision 5341)
+++ /trunk/prototype/config/alarm.ini	(revision 5376)
@@ -11,4 +11,5 @@
 [after.find]
 decodeFindConcept = modules/calendar/interceptors/DBMapping.php
+decodeSignatureAlarmType = modules/calendar/interceptors/DBMapping.php
 
 [before.create]
Index: /trunk/prototype/modules/calendar/interceptors/DBMapping.php
===================================================================
--- /trunk/prototype/modules/calendar/interceptors/DBMapping.php	(revision 5366)
+++ /trunk/prototype/modules/calendar/interceptors/DBMapping.php	(revision 5376)
@@ -372,11 +372,8 @@
                     
                         if($participants[$ii]['user']['id'] == Config::me('uidNumber'))
-                            $participantLog = $vv['id'];  
+                           $participants[$ii]['alarms'] = Controller::find( array( 'concept' => 'alarm' ) , null , array('filter' => array('AND' , array('=', 'participant' ,$vv['id'] ) ,array('=' , 'schedulable', $v['id'])) ) ); 
                     }       
                     $result[$i]['participants'] = $participants;
-                }
-                
-                if(isset($participantLog))
-                    $result[$i]['alarms'] = Controller::find( array( 'concept' => 'alarm' ) , null , array('filter' => array('AND' , array('=', 'participant' ,$participantLog ) ,array('=' , 'schedulable', $v['id'])) ) );
+                }               
                 
            }
@@ -401,12 +398,10 @@
                            $participants[$ii]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $vv['user'] ));
 
-                         if($participants[$ii]['user']['id'] == Config::me('uidNumber'))
-                            $participantLog = $vv['id'];  
-                    }  
+                        if($participants[$ii]['user']['id'] == Config::me('uidNumber'))
+                           $participants[$ii]['alarms'] = Controller::find( array( 'concept' => 'alarm' ) , null , array('filter' => array('AND' , array('=', 'participant' ,$vv['id'] ) ,array('=' , 'schedulable', $result['id'])) ) ); 
+               }  
 
                 $result['participants'] = $participants;
-                if(isset($participantLog))
-                    $result['alarms'] = Controller::find( array( 'concept' => 'alarm' ) , null , array('filter' => array('AND' , array('=', 'user' , $participantLog ) ,array('=' , 'schedulable', $result['id'])) ) );
-            }
+                }
        }
 
Index: /trunk/prototype/modules/calendar/js/calendar.codecs.js
===================================================================
--- /trunk/prototype/modules/calendar/js/calendar.codecs.js	(revision 5365)
+++ /trunk/prototype/modules/calendar/js/calendar.codecs.js	(revision 5376)
@@ -109,10 +109,6 @@
 	  for( var i = 0; i < this.signatures.length; i++ )
 	  {
-	      this.signatureOf[ this.calendarIds[i] = this.signatures[i].calendar.id ] = this.signatures[i];
-	      //this.signatureOf[ this.calendarIds[i] = this.signatures[i].calendar ] = this.signatures[i];
+	      this.signatureOf[ this.calendarIds[i] = ( this.calendars[ this.calendars.length ] = this.signatures[i].calendar ).id ] = this.signatures[i];
 	  }
-
-	  this.calendars = DataLayer.get( "calendar", {filter: ["IN", "id", this.calendarIds], criteria: {order: 'name'}} );
-
 	  delete Calendar.currentViewKey;
       }
@@ -179,5 +175,5 @@
 Calendar.load();
 
-DataLayer.poll( "schedulable" );
+//DataLayer.poll( "schedulable" );
 
 DataLayer.listen( "schedulable", function( status ){
@@ -320,4 +316,8 @@
 	
 	encoder: function (evtObjt){
+	
+			if(notArray = typeof(evtObjt) == 'array' )
+				evtObjt = [evtObjt];
+			
 			var res = $.map(evtObjt, function( objEvent ){	
 				var timezone = User.timezones[objEvent.schedulable.timezone];
@@ -377,40 +377,29 @@
 
 		var res = $.map(evtObj, function( form ){
-			var event = {};
-			event.id = form.idEvent;
-			event.calendar = form.calendar;	
-			var isCurrentParticipant = function(user, event){
-				var participant =  DataLayer.get('participant', ['AND',['=', 'user', user],['=', 'schedulable', event]]);
-				if(participant != '')
-					return participant[0].id;
-				else
-					return false;
-			};  
-			
-			event.participants = [];
-			var participant = {};
-			participant.user = User.me.id,
-			participant.status = form.status;
-			if((form.idEvent) && (id = isCurrentParticipant(participant.user, form.idEvent)))
-				participant.id = id;
-			
-			if(typeof(form.alarmTime) != 'undefined'){
-				participant.alarms = $.map( form.alarmTime || [], function( alarmTime, i ){
-
-					if( alarmTime === "" )
-						return( null );
-
-					if(form.alarmId[i] === "")
-						return { type: form.alarmType[i],unit: form.alarmUnit[i], time: form.alarmTime[i] };
-					else
-						return { type: form.alarmType[i], unit: form.alarmUnit[i], time: form.alarmTime[i], id: form.alarmId[i] };
-				});	
+			
+			return {
+				id: form.idEvent,
+				calendar: form.calendar,
+				participants : [{
+					user: User.me.id,
+					status: form.status,
+					id: form.attendee_organizer[0],
+					isOrganizer: 0,
+					alarms: typeof(form.alarmTime) != 'undefined' ? 
+						$.map( form.alarmTime || [], function( alarmTime, i ){
+
+							if( alarmTime === "" )
+								return( null );
+
+							if(form.alarmId[i] === "")
+								return { type: form.alarmType[i],unit: form.alarmUnit[i], time: form.alarmTime[i] };
+							else
+								return { type: form.alarmType[i], unit: form.alarmUnit[i], time: form.alarmTime[i], id: form.alarmId[i] };
+						}) : []
+					}]
 			}
-			
-			event.participants[0] = participant;
-			
-			return event;
 		});
-	return notArray ? res[0] : res;
+		
+		return notArray ? res[0] : res;
     },
 
@@ -460,25 +449,11 @@
 				data.setTime(time);
 				return dateFormat(setDateTimezone(data, timezone, existsTimezone, idCalendar), User.preferences.hourFormat.replace(/m/g,'M'))			
-			}	
-			var temp = [];
-			var tempAlarm = [];
-					
-			var typeAlarm = function(obj){
-				var alarm = [];
-				for(var i = 0; i < obj.length; i++){
-					if(tempAlarm[obj[i].id]) 
-						continue; 
-					tempAlarm[obj[i].id] = true;
-					
-					if(obj[i].type == "1"){
-						obj[i].type = 'alert';
-					}else if(obj[i].type == "2"){
-						obj[i].type = 'mail';
-					}else{
-						obj[i].type = 'sms';
-					}
-					alarm.push(obj[i]);
+			}						
+			var alarmParticipant = function(obj){
+				var alarms = [];
+				for(var i = 0; i < obj.length; i++){ 
+					alarms.push(DataLayer.get('alarm', obj[i]));
 				}
-				return alarm;
+				return alarms;
 			}
 		    return {
@@ -499,11 +474,6 @@
 			      allDay: !!parseInt( objEvent.allDay ),
 	  // 				attachments: this.decode( "attachment:detail", form ),
-			      attendee: $.map(objEvent.participants || [], function( participant, i ){
-						if(participant.indexOf('java') >= 0) 
-							return (null); 
-						if(temp[participant]) 
-							return (null); 
-						temp[participant] = true;
-						
+			      attendee: $.map(objEvent.participants || [], function( participant, i ){						
+					
 					var attend = DataLayer.get('participant', participant);
 					attend.user = DataLayer.get('user', attend.user)
@@ -520,5 +490,6 @@
 					  {
 					      participantInfo.status = attend.status;
-					      participantInfo.alarms = typeAlarm(objEvent.alarms);
+					      participantInfo.alarms = alarmParticipant(attend.alarms);
+						  me.uid = attend.id;
 					  }
 					  if( parseInt( attend.isOrganizer ) )
Index: /trunk/prototype/modules/calendar/js/helpers.js
===================================================================
--- /trunk/prototype/modules/calendar/js/helpers.js	(revision 5364)
+++ /trunk/prototype/modules/calendar/js/helpers.js	(revision 5376)
@@ -267,5 +267,8 @@
 	    });
 		
-		UI.dialogs.addEvent.find('.button.remove').button({text:false, icons:{primary:'ui-icon-close'}}).click(function(el) {
+		UI.dialogs.addEvent.find('.button.remove').button({text:false, icons:{primary:'ui-icon-close'}}).click(function(el){
+			var id;
+			if( id = $(this).parent().find('input[name="alarmId[]"]').val())
+				DataLayer.remove('alarm', id);
 			if( $(this).parent().remove().find('li').is(':empty') )	{
 				//alert('nao tem');
@@ -303,4 +306,7 @@
 					var li_attach = DataLayer.render('templates/alarms_add_itemlist.ejs', {alarm:calendarSelected});
 						jQuery('.event-alarms-list').append(li_attach).find('.button.remove').button({text:false, icons:{primary:'ui-icon-close'}}).click(function(el) {
+							var id;
+							if( id = $(this).parent().find('input[name="alarmId[]"]').val())
+								DataLayer.remove('alarm', id);
 							if( $(this).parent().remove().find('li').is(':empty') )	{
 								//alert('nao tem');
@@ -608,4 +614,5 @@
 					if (user){
 						if (!skipAddNewLine) {
+							user[0].id =  DataLayer.put('participant', {user: user[0].id, isExternal: 1});
 							blkAddAtendee.find('dd.attendee-list ul.attendee-list').append(
 								DataLayer.render('templates/participants_add_itemlist.ejs', user)
@@ -860,5 +867,5 @@
 
 				form_content.find('.button-add-alarms').click(function(){
-					DataLayer.render( 'templates/alarms_add_itemlist.ejs', {}, function( template ){
+					DataLayer.render( 'templates/alarms_add_itemlist.ejs', {}, function( template ){						
 						jQuery('.preferences-alarms-list').append(template)
 						.find('li:last label:eq(0)').remove().end()
Index: /trunk/prototype/modules/calendar/templates/alarms_add_itemlist.ejs
===================================================================
--- /trunk/prototype/modules/calendar/templates/alarms_add_itemlist.ejs	(revision 5341)
+++ /trunk/prototype/modules/calendar/templates/alarms_add_itemlist.ejs	(revision 5376)
@@ -11,5 +11,5 @@
 			<select name="alarmUnit[]">
 				<option value="d" <%= data.alarm.defaultAlarms[i].unit=='d' ? 'selected="selected"':'' %>>Dias</option>
-				<option value="h" <%= data.alarm.defaultAlarms[i].unit=='H' ? 'selected="selected"':'' %>>Horas</option>
+				<option value="h" <%= data.alarm.defaultAlarms[i].unit=='h' ? 'selected="selected"':'' %>>Horas</option>
 				<option value="m" <%= data.alarm.defaultAlarms[i].unit=='m' ? 'selected="selected"':'' %>>Minutos</option>
 			</select>
@@ -31,5 +31,5 @@
 		<select name="alarmUnit[]">
 			<option value="d">Dias</option>
-			<option value="H">Horas</option>
+			<option value="h">Horas</option>
 			<option value="m">Minutos</option>
 		</select>
Index: /trunk/prototype/modules/calendar/templates/event_add.ejs
===================================================================
--- /trunk/prototype/modules/calendar/templates/event_add.ejs	(revision 5345)
+++ /trunk/prototype/modules/calendar/templates/event_add.ejs	(revision 5376)
@@ -127,5 +127,5 @@
 				<select name="alarmUnit[]">
 					<option value="d" <%= data.event.alarms[i].unit=='d' ? 'selected="selected"':'' %>>Dias</option>
-					<option value="h" <%= data.event.alarms[i].unit=='H' ? 'selected="selected"':'' %>>Horas</option>
+					<option value="h" <%= data.event.alarms[i].unit=='h' ? 'selected="selected"':'' %>>Horas</option>
 					<option value="m" <%= data.event.alarms[i].unit=='m' ? 'selected="selected"':'' %>>Minutos</option>
 				</select>
