$(document).ready(function () {

	var container = $('#rightcontent'), left = $('#leftcontent'), textArea = container.children('textarea');

	left.on('click', 'a', function () {
		var li = $(this).parent();
		$('#leftcontent').find('li').removeClass('active');
		Notes.save.current();
		console.log('click');
		if (li.data('new')) {
			console.log('new');
			Notes.active = '';
			textArea.val('');
		} else {
			var note = li.data('note');
			Notes.active = note;
			Notes.loadNote(note);
		}
		textArea.focus();
	});

	var note = location.hash.substr(1);
	if (note) {
		Notes.loadNote(note);
		Notes.active = note;
	}
	textArea.focus();
	textArea.keydown(Notes.onType);
	$(document).keydown(Notes.handleKey);

	setInterval(Notes.save.auto, 10 * 1000);
	setInterval(Notes.onType, 1000);

	$(window).on('beforeunload', function () {
		Notes.save.current(true);
	});
});

Notes = {};
Notes.category = '';
Notes.active = '';
Notes.oldContent = '';

Notes.get = function (category, note) {
	return $.get(OC.filePath('notes', 'ajax', 'get.php'), {category: category, note: note});
};

Notes.loadNote = function (note) {
	Notes.get(Notes.category, note).then(function (text) {
		$('#rightcontent').children('textarea').val(text);
		if (text) {
			var title = Notes.getTitle(text);
			$('#leftcontent').find('li[data-note="' + note + '"]').addClass('active');
			Notes.setTitle(title);
			Notes.oldContent = text;
		} else {
			Notes.active = '';
			location.hash = '';
		}
	});
};

Notes.getTitle = function (text) {
	return text.split('\n').shift();
};

Notes.newNote = function (title) {
	if (!title) {
		return;
	}
	var right = $('#rightcontent'),
		li = $('<li/>'),
		link = $('<a/>');
	li.append(link);
	li.attr('data-note', '');
	link.text(title);
	li.addClass('active');
	$('#leftcontent').children().first().after(li);
};

Notes.onType = function () {
	setTimeout(function () {
		var li, left = $('#leftcontent'), right = $('#rightcontent'), text = right.find('textarea').val(),
			title = Notes.getTitle(text),
			link = left.find('a.active');
		link.text(title);
		Notes.setTitle(title);
		li = left.find('li[data-note="' + Notes.active + '"]');
		if (li.length) {
			li.children('a').text(title);
			if (title) {
				li.show();
			} else {
				li.hide();
			}
		} else {
			Notes.newNote(title);
		}
		Notes.save.auto();
	}, 100);
};

Notes.setTitle = function (title) {
	document.title = title + ' | ownCloud(' + OC.currentUser + ')';
};

Notes.rename = function (old, newName) {
	var left = $('#leftcontent'), li = left.find('li[data-note="' + old + '"]');
	li.attr('data-note', newName);
	li.children('a').attr('href', '#' + newName);
	left.children().first().after(li);
};

Notes.remove = function (old) {
	var li = $('#leftcontent').find('li[data-note="' + old + '"]').remove();
};

Notes.save = function (oldName, content, sync) {
	if (!sync) {
		var def = $.post(OC.filePath('notes', 'ajax', 'save.php'), {oldname: oldName, content: content, category: Notes.category});
		Notes.save.last = (new Date()).getTime();
		Notes.save.active = true;
		Notes.oldContent = content;
		def.then(function (newName) {
			Notes.save.active = false;
			if (content.trim()) {
				Notes.rename(oldName, newName);
			} else {
				Notes.remove(oldName);
			}
		});
		return def;
	} else {
		url = OC.filePath('notes', 'ajax', 'save.php');
		$.ajax({
			type: 'POST',
			url: url,
			data: {
				oldname: oldName,
				content: content,
				category: Notes.category
			},
			'async': false
		});
		return null;
	}
}
;

Notes.save.current = function (sync) {
	var content = $('#rightcontent').find('textarea').val();
	if (content != Notes.oldContent && !Notes.save.active) {
		Notes.save(Notes.active, content, sync);
	}
};

Notes.handleKey = function (event) {
	if (event.which === 9) { //tab
		event.preventDefault();
	}
};

Notes.save.last = (new Date()).getTime();
Notes.save.active = false;

Notes.save.auto = function () {
	now = (new Date()).getTime();
	if ((now - Notes.save.last) > 5 * 1000) {
		Notes.save.current();
		Notes.save.last = now;
	}
};

@@ -283,76 +283,29 @@
 ;%0A%09%09
-console.log('click');%0A%09%09if (li.data('new')) %7B%0A%09%09%09console.log('new');
+if (li.data('new')) %7B
 %0A%09%09%09
@@ -2322,16 +2322,31 @@
 itle) %7B%0A
+%09if (title) %7B%0A%09
 %09documen
@@ -2402,16 +2402,19 @@
  + ')';%0A
+%09%7D%0A
 %7D;%0A%0ANote
@@ -2635,32 +2635,89 @@
 st().after(li);%0A
+%09if (Notes.active == old) %7B%0A%09%09Notes.active = newName;%0A%09%7D%0A
 %7D;%0A%0ANotes.remove

fix some issues with active note