var edited_notelist_id = null;

var toggleNotelistEditMode = function() {
	$$(".category").invoke("toggleClassName", "editable")
	edited_notelist_id = null;
}

var toggleNotelistEdit = function(obj, current_category_id, current_category_type) {
	var current_li = Element.up(obj, "li");

	if (edited_notelist_id && edited_notelist_id == obj.id) {
		return false;
	}

	var get_url = function(obj) {
		return ((Element.up(obj, "ul").id == "notelists") ? "/categories/" : "/smart_categories/") + obj.value;
	}

	// for toggle
	if (edited_notelist_id) {
		var edited_notelist_url = get_url($(edited_notelist_id));
		new Ajax.Updater($(edited_notelist_id), edited_notelist_url, {
			method: 'get',
			parameters: 'current_category_id=' + current_category_id + '&current_category_type=' + current_category_type
		});
	}

	var edit_notelist_url = get_url(obj) + "/edit";
	new Ajax.Updater(current_li, edit_notelist_url, {
		method: 'get',
		parameters: 'current_category_id=' + current_category_id + '&current_category_type=' + current_category_type
	});

	edited_notelist_id = obj.id;
}

var exitNotelistEditMode = function(url, current_category_id, current_category_type) {
	new Ajax.Request(url, {
		method: 'get',
		parameters: 'current_category_id=' + current_category_id + '&current_category_type=' + current_category_type,
		onSuccess: NotelistViewMode
	});
}

var NotelistViewMode = function() {
	$$(".category").invoke("removeClassName", "editable");
	edited_notelist_id = null;
}
