/*

Helper Script for Profile Notes

*/


jQuery(document).ready(function() {

if ($("#note_usernotes").length > 0)
{
	var userID = $("#note_usernotes").attr("class");
	
	$("#note_usernotes").load('notes/profilenotes/'+userID,function(responseText, textStatus, XMLHttpRequest){initNotes(); initAdd();});


	function initNotes()
	{
		////////////////////////////////////////////////////// DELETE AN ENTRY
	
		$("#note_usernotes .delete").off("click");
		$("#note_usernotes .edit").off("click");
	
		$("#note_usernotes .delete").on("click",function(){
			
			$("#note_usernotes .inlinenote").css('display','block');
			$('.note_inlineform').remove();
			
			if (window.confirm("Soll der Eintrag gelÃ¶scht werden?"))
			{
				var noteID = $(this).parent().attr('class').split(' ');
						noteID = noteID[1];

				var thisObj = $(this)

				var postObj=new Object();
						postObj.note_id=noteID;
				
				$.post('notes/deletenote/', postObj, function (data){
					thisObj.parent().slideUp("slow",function(){
						$(this).remove();
					});
				});			
			}
			
			return (false);
		});


		////////////////////////////////////////////////////// EDIT AN ENTRY

		$("#note_usernotes .edit").on("click",function(){
		
			var noteID = $(this).parent().attr('class').split(' ');
					noteID = noteID[1];
			var noteCnt= $(this).parent().first().text();
			


			// Clean up
			
			$("#note_usernotes .inlinenote").css('display','block');
			$('.note_inlineform').remove();
		
			$(this).parent().css('display','none');
		
			$(this).parent().after(noteGetEditForm());
			
			$('[name="note_content"]').val(noteCnt);
			$('[name="note_id"]').val(noteID);

			$('.note_inlineform').submit(function(){
				
				$('.note_inlineform .save').click();
				
				return (false);
			});


			$('.note_inlineform .save').click(function(){
			
				var jqSelector = "#note_usernotes ."+$('[name="note_id"]').val()+" span";
				
				if ($('[name="note_content"]').val() == '')
				{
					$(jqSelector).parent().remove();
				}
				else
				{
					$(jqSelector).text($('[name="note_content"]').val());
				}

				var postObj=new Object();
						postObj.note_id=$('[name="note_id"]').val();
						postObj.note_content=$('[name="note_content"]').val();
				
				
				$.post('notes/updatenote/', postObj, function (data){
				
					$("#note_usernotes .inlinenote").css('display','block');
					$('.note_inlineform').remove();
				
				});			

				
			
				return (false);
			});


			$('.note_inlineform .cancel').click(function(){
				
				$("#note_usernotes .inlinenote").css('display','block');
				$('.note_inlineform').remove();
			
				return (false);
			});

			return (false);
		});
	}

	// Logik für das Hinzufügen-Formular....	
	
	function initAdd()
	{
		if ($('#note_addnote').length > 0)
		{
			$('#note_addnote').click(function(){
				
				//if ($('#note_addnote').attr("class") == 'extended')
				//{
					$('#note_addform').modal();
					
					
					////////////////////////// SAVE CONTENT
					
					$('#note_addform a').click(function(){

						var postObj=new Object();
								postObj.note_user=userID;
								postObj.note_content=$('#notenew_content').val();
						
					/// Send update to server
					
					if (postObj.note_content != '')
					{
					
						$.post('notes/createnote/', postObj, function (data){
								if (data != '')	
								{
									$('#note_usernotes').prepend(data);
									initNotes();
								}
								
								$('#note_addform').modal();
								
								$.modal.close();
								
							});
						}
						else
							$.modal.close();
						
						return (false);
					});
					
					////////////////////////// WHEN SUBMITTING FORM: Call save and cancel Submit
					
					$('#note_addform form').submit(function(){
						$('#note_addform a').click();
						return(false);
					});
					
					
//				}
//				else
//				{
//					$('#render_extensionMsg').modal();
//				}
			
				return (false);
			})
		}	
	}
}
})


function noteGetEditForm()
{
	var r = '';
	
	r+='<form action="" method="post" class="note_inlineform">';
	r+='<input type="hidden" name="note_id" />';
	r+='<input type="text"  name="note_content" />';
	r+='<a href="#" class="save" title="Speichern"></div>';
	r+='<a href="#" class="cancel" title="Abbrechen"></div>';
	r+='</form>';
	
	
	return (r);

}
