
$(document).ready(function(){var dialog=$('#note_edit_dialog');if(dialog.length==0)
return;var idtag=$('#note_id');var title=$('#note_title');var body=$('#note_body');var task=$('#note_task');var editFields=$([]).add(idtag).add(title).add(body).add(task);title.limit('255','#note_edit_counter');dialog.dialog({bgiframe:true,autoOpen:false,modal:true,width:600,buttons:{'Delete Note':function(){var doit=confirm("Are you sure you want to delete this note?");if(doit){$.ajax({type:'GET',url:'/Note/Delete/'+idtag.val(),success:function(result){var ops=JSON.parse(result);for(var i=0;i<ops.length;i++){var obj=$(ops[i].Id);if(ops[i].Operation=='remove'){obj.remove();}}
var taskLine=$('#t'+task.val());if(taskLine.length>0){tlSetNoteCounter(taskLine,tlGetNoteCounter(taskLine)-1);}
dialog.dialog('close');},error:function(XMLHttpRequest,textStatus,errorThrown){var errObj=JSON.parse(XMLHttpRequest.responseText);alert("There was a problem deleting your note: "+errObj.Error);}});}},'OK':function(){var valid=true;var newNote=idtag.val()==''?true:false;var sumtxt=title.val();if(sumtxt.length==0){alert("Oops, you forgot to name your note!");return false;}
if(sumtxt.search(/^\\s$/g)!=-1){alert("You can't have a whitespace-only title!");return false;}
if(valid){$.ajax({type:'POST',url:!newNote?'/Note/Edit/'+idtag.val():'/Note/Add',data:({title:sumtxt,body:body.val(),task:task.val()}),success:function(result){var note=JSON.parse(result);if(note.Error!==undefined){alert("There was a problem updating your note: "+note.Error);return;}
if(newNote){if(note.RelatedTask!==undefined&&note.RelatedTask!=null){var taskLine=$('#t'+note.RelatedTask);if(taskLine.length>0){tlSetNoteCounter(taskLine,tlGetNoteCounter(taskLine)+1);}}
var noteList=$('#notes > ul');if(noteList.length>0){$.ajax({type:'GET',url:'/Note/View/'+note.Id,success:function(result){domElement=$(result);if(domElement.length>0){liElement=$('<li>');liElement.append(domElement);noteList.append(liElement);hookupNoteLine(domElement);}
dialog.dialog('close');},error:function(XMLHttpRequest,textStatus,errorThrown){var errObj=JSON.parse(XMLHttpRequest.responseText);alert("There was a problem: "+errObj.Error+"\n\nDon't worry though, your note is safe!");dialog.dialog('close');}});}else{dialog.dialog('close');}}else{var noteLine=$('#n'+note.Id);noteLine.find('.title').html(linkTagString(note.Title));noteLine.find('.body').markdown(note.Body);var dateline=noteLine.find('.updated.date');dateline.html('last updated <abbr class="timeago" title="'+note.UpdatedTime+'">'+note.UpdatedTime+'</abbr>');dateline.find('.timeago').timeago();dialog.dialog('close');}},error:function(XMLHttpRequest,textStatus,errorThrown){var errObj=JSON.parse(XMLHttpRequest.responseText);alert("There was a problem updating your note: "+errObj.Error);}});}},Cancel:function(){$(this).dialog('close');}},close:function(){editFields.val('').removeClass('ui-state-error');}});});function nlShowButtonClick(btn){btn.toggleClass('hidden');btn.siblings('.hideButton').toggleClass('hidden');btn.parents('.noteLine').find('.details').toggleClass('hidden');}
function nlHideButtonClick(btn){btn.toggleClass('hidden');btn.siblings('.showButton').toggleClass('hidden');btn.parents('.noteLine').find('.details').toggleClass('hidden');}
function nlEditButtonClick(btn){var id=btn.parent().siblings('input[type="hidden"].id').attr('value');btn.attr('src','/Content/icons/progress.gif');$.ajax({type:'GET',url:'/Note/View/'+id,data:'json=true',success:function(result){var note=JSON.parse(result);if(note.Error!==undefined){alert("There was a problem getting your note: "+note.Error);return false;}
var dialog=$('#note_edit_dialog');$('#note_id').val(note.Id);$('#note_title').val(note.Title);$('#note_body').val(note.Body);$('#note_task').val(note.RelatedTask);dialog.dialog('option','title','Editing "'+note.Title+'"');dialog.dialog('open');btn.attr('src','/Content/icons/edit.png');},error:function(XMLHttpRequest,textStatus,errorThrown){var errObj=JSON.parse(XMLHttpRequest.responseText);alert("There was a problem getting your task: "+errObj.Error);btn.attr('src','/Content/icons/edit.png');}});}
$(document).ready(function(){$('.noteLine .showButton').click(function(){nlShowButtonClick($(this));});$('.noteLine .hideButton').click(function(){nlHideButtonClick($(this));});$('.noteLine .editNote').click(function(){nlEditButtonClick($(this));});$('.markdown').markdown();});function hookupNoteLine(noteLine){noteLine.find('abbr[class*=timeago]').timeago();noteLine.find('.markdown').markdown();noteLine.find('.showButton').click(function(){nlShowButtonClick($(this));});noteLine.find('.hideButton').click(function(){nlHideButtonClick($(this));});noteLine.find('.editNote').click(function(){nlEditButtonClick($(this));});var related=noteLine.find('input.task');if(related.length>0){var taskid=related.val();var urlbits=document.URL.split('/');if(urlbits[urlbits.length-1].toLowerCase()==taskid.toLowerCase()){noteLine.find('.parentTask').hide();}}};