function NoteViewHelper(scope){
	var _this=this;
	
	/*
	*	
	*/
	
	
	
	this.note_behaviour=function(model,view,controller){
		
		$('#add-note-action',scope.element).bind('click',function(e){
			// Do the spinny wheel !
			$('#add-note-action',scope.element).attr('disabled',true);
			$('#add-note-action',scope.element).hide();
			$('#add-note-action',scope.element).parent().parent().append('<li class="ajax-loading">Adding Note</li>');
			$('#add-note-body',scope.element).attr('disabled',true);
			
			controller.on_add_note_clicked({
				"body":$('#add-note-body',scope.element).val(),
				"update":on_note_added
			});
			
			return false;
		});
		
		
		// Need to pass in the sorted contacts list here!
		controller.render_template_with_view(controller.get_note_holder_template_name(),{},controller,function(notes_markup){
			view.html(notes_markup);
			if(model.notes) render_notes_from_groups(model.notes);
		});
		
		function render_notes_from_groups(notes){
			
			$.each(notes,function(note_index,note){
				add_note_to_group(note);
			});
		}

		// 
		function add_note_to_group(note){
			// Which group is it in?
			var group=controller.which_note_group(note);
			// If the group is invisible, make it visible
			$('.'+group.css,view).removeClass('hide');
			var m={note:note};
			controller.render_template_with_view(controller.get_note_template_name(),m,controller,function(notes_markup){
				// alert(notes_markup);
				var n=$(notes_markup);
				add_actions_to_note(n);
				var dt=$('.'+group.css+' dt',view);
				n.insertAfter(dt);
			});
		}

		
		function add_actions_to_note(note){
			note.hover(function(e){
	            $(this).addClass('hover');
	        },function(e){
	            $(this).removeClass('hover');
	        });
	        
	        //delete note
	        $('li#delete-action a',note).click(function() {
			  	  // Delete this note
               controller.raise_event('on_delete_note',{
                   note_id:note.attr('rel'),
                   paint:function(){
                       note.slideUp(function(e){
                          note.remove(); 
                       });
                   }
               });
				return false;
			});
			
			$('li#delete-action a',note).confirm({});

			$('li#delete-action a',note).bind('afterConfirm',function(e){
				note.removeClass('is_deleting');
				$('li#edit-action a',note).show();
			});

			$('li#delete-action a',note).bind('beforeConfirm',function(e){
				$('li#edit-action a',note).hide();
				note.addClass('is_deleting');
				//	Set the status of this note to delete so the action is always visible
			});
			
			//edit note
			$('li#edit-action a',note).bind('click',function(e){
				var _working_note=null;
				$.each(model.notes,function(i,n){
					if(n){
						if(n.id==note.attr('rel')){
							_working_note=n;
						}
					}
				});

				if(_working_note){
					$('textarea',note).val(_working_note.body);
				}

				$(note).addClass('editing');

				return false;
			});

			$('.note-meta em abbr',note).timeago();
			
			//confirm edit
			$('li#confirm-action a',note).bind('click',function(e){
				var _working_note=null;
				$.each(model.notes,function(i,n){
					if(n!=null){
						if(n.id==note.attr('rel')){
							_working_note=n;
						}
					}
				});

				if(_working_note!=null){
					_working_note.body = $('textarea',note).val();

					controller.raise_event('on_edit_note',{
						note:_working_note,
						paint:function(){
							$(note).removeClass('editing');
							$('#view-box #note-body-box',note).html(controller.parse_note_body(_working_note.body));
						}
					});
				}else{

				}

				return false;
			});
			
	        //cancel editing
	        $('li#cancel-action a',note).bind('click',function(e){
				$(note).removeClass('editing');
				$('textarea',note).val("");
				return false;
			});
            
		};
		
		
		
		function render_note(note){
			
			
		}
		// When a notes is added, put it into todays list!
		function on_note_added(note){
			if(note==null){
				//	Something went wrong
			}else{
				//	Something went right!
				add_note_to_group(note);
			}

			
			$('#add-note-body',scope.element).attr('disabled',false);
			$('#add-note-body',scope.element).val("");
			$('#add-note-action',scope.element).attr('disabled',false);
			$('#add-note-body',scope.element).removeAttr('disabled',false);
			
			$('#add-note-action',scope.element).show();
			$('.ajax-loading',$('#add-note-action',scope.element).parent().parent()).remove();
		}			
	
	}
	
}