//  @import com.hedgehoglab.application.view.BaseView;

function ActivityView(scope){
	BaseView.call(this,scope);
    var _this=this;
	var _template=$('#activity-templates').clone(false);
	
    _this.update=function(model){
		var mod=model.activity.aggregate();
		
		if(mod!=null){
			//	Add a class to all the items
			$('.activity',scope.element).addClass('__to_remove__');
			var _the_top_node=$('.activity:first',scope.element)[0];
			
			$.each(mod,function(i,n){
					var tmp=String(n.type).toLowerCase();
					var _helper={
						get_username:function(user_id){
							var user=model.users.user_by_id(user_id);
							var username="";
							if((user.firstName==null || user.firstName=="") && (user.lastName==null || user.lastName=="")){
								//	Revert to the username
								username=user.username;
							}else{
								username=user.firstName+" "+user.lastName;
							}
							return username;
						},
						get_created:function(date){
							var theDate = Date.parse(date);
							return date;
						},
						parse_text:function(text){
							return text.replace(/\n/gi,"<br/>");
						},
						parse_date:function(text){
							// 								// $(".date_field",node).val(Date.parse(n.dueOn).toString("dd MMM yyyy"));
						}
					};
		// 			
					//	Does the ID already exist?
					var the_node=$("dd[id='"+n.id+"']",scope.element)[0];
					
					if(the_node==null){
						//	Create it
						
							_this.render_template_with_view(tmp,{activity:n},_helper,function(rendered_template){
								the_node=$(rendered_template);
								the_node.hide();
								the_node.addClass("__to_fade_in__");
								
								$('.due-date',the_node).timeago();

								if(_the_top_node!=null){
									$(the_node).insertBefore(_the_top_node);
								}else{
									scope.element.append(the_node);
								}
							});
					}else{
						//	Remove the class from the node
						$(the_node).removeClass('__to_remove__');
					}
					
					_the_top_node=the_node;
					
			});
		// 	}
		$('.__to_remove__',scope.element).fadeOut(1000,function(e){
			$(this).remove();
		});
		
		$('.__to_fade_in__',scope.element).fadeIn(1000,function(e){
			// $(this).removeClass("__to_fade_in__");
		}).removeClass('__to_fade_in__');
			
		}
	};
}

ActivityView.enums={
	"TASK_CREATED":{
		"template":"activity-task"
	},
	"NOTE_CREATED":{
		"template":"activity-note"
	},
	"CONTACT_CREATED":{
		"template":"activity-contact"
	},
	"DEAL_CREATED":{
		"template":"activity-deal"
	}
};