/**
 * Copyright (c) 2014, Raghu Nayyar <beingminimal@gmail.com>
 * This file is licensed under the Affero General Public License version 3 or later.
 * See the COPYING-README file.
 */

 var GroupList = {

	delete_group: function (gid) {
		if(GroupList.deleteGid !=='undefined') {
			GroupList.finishDelete(null);
		}

		//Set the undo flag
		GroupList.deleteCanceled = false;

		//Provide an option to undo
		$('#notification').data('deletegroup', true);
		OC.Notification.showHtml(t('settings', 'deleted') + ' ' + escapeHTML(gid) + '<span class="undo">' + t('settings', 'undo') + '</span>');
	},

	showGroup: function (gid) {
		UserList.empty();
		UserList.update(gid);
		$('#app-navigation li').removeClass('active');
		if(gid !== undefined) {
			//TODO: treat Everyone properly
			$('#app-navigation li').filterAttr('data-gid', gid).addClass('active');
		}
	},

	finishDelete: function (ready) {
		if (!GroupList.deleteCanceled && GroupList.deleteGid) {
			$.ajax({
				type: 'POST',
				url: OC.filePath('settings', 'ajax', 'removegroup.php'),
				async: false,
				data: { groupname: GroupList.deleteGid },
				success: function (result) {
					if (result.status === 'success') {
						// Remove undo option, & remove user from table
						OC.Notification.hide();
						$('li').filterAttr('data-gid', GroupList.deleteGid).remove();
						GroupList.deleteCanceled = true;
						if (ready) {
							ready();
						}
					} else {
						OC.dialogs.alert(result.data.message, t('settings', 'Unable to remove group'));
					}
				}
			});
		}

	},

}

$(document).ready( function () {
	$('ul').on('click', 'span.utils>a', function (event) {
		var li = $(this).parent().parent();
		var gid = $(li).attr('data-gid');
		$(li).hide();
		// Call function for handling delete/undo on Groups
		GroupList.delete_group(gid);
	});

	// Display or hide of Create Group List Element
	$('#newgroup-form').hide();
	$('#newgroup-init').on('click', function (e) {
		e.stopPropagation();
		$('#newgroup-form').show();
		$('#newgroup-init').hide();
		$('#newgroupname').focus();
	});

	// Responsible for Creating Groups.
	$('#newgroup-form form').submit(function (event) {
		event.preventDefault();
		var groupname = $('#newgroupname').val();
		if ($.trim(groupname) === '') {
			OC.dialogs.alert(
				t('settings', 'A valid groupname must be provided'),
				t('settings', 'Error creating group'));
			return false;
		}
		$.post(
			OC.filePath('settings', 'ajax', 'creategroup.php'),
			{
				groupname : groupname
			},
			function (result) {
				if (result.status !== 'success') {
					OC.dialogs.alert(result.data.message,
						t('settings', 'Error creating group'));
				} else {
					if (result.data.groupname) {
						var addedGroups = result.data.groupname;
						UserList.availableGroups = $.unique($.merge(UserList.availableGroups, addedGroups));
					}
				}
			}
		)
	});

	// click on group name
	// FIXME: also triggered when clicking on "remove"
	$('ul').on('click', 'li[data-gid]', function (event) {
		var li = $(this);
		var gid = $(li).attr('data-gid');
		// Call function for handling delete/undo on Groups
		GroupList.showGroup(gid);
	});

	// Implements Groupname editing.
	$('#app-navigation').on('click', 'img.rename', function (event) {
		event.stopPropagation();
		var img = $(this);
		var gid = img.parent().parent().attr('data-gid');
		var groupname = escapeHTML(img.parent().parent().attr('data-gid'));
		var input = $('<input type="text" value="' + groupname + '">');
		img.css('display', 'none');
		img.parent().children('span').replaceWith(input);
		input.focus();
		input.keypress(function (event) {
			if (event.keyCode === 13) {
				if ($(this).val().length > 0) {
					$.post(
						OC.filePath('settings', 'ajax', 'changegroupname.php'),
						{	groupname: gid,
							groupname: $(this).val()
						}
					);
					input.blur();
				} else {
					input.blur();
				}
			}
		});
		input.blur(function () {
			var input = $(this), groupname = input.val();
			input.closest('li').attr('data-gid', groupname);
			input.replaceWith('<span>' + escapeHTML(groupname) + '</span>');
			img.css('display', '');
		});
	});

	// Implements Quota Settings Toggle.
	$('#app-navigation').find('.settings-button').on('click', function (e) {
		e.stopPropagation();
		$('#app-settings').removeClass('open');
		$('#app-settings').toggleClass('open');
		$(document).click(function() {
			$('#app-settings').removeClass('open');
    	});
	});
});
@@ -860,16 +860,328 @@
 %09%7D%0A%09%7D,%0A%0A
+%09toggleAddGroup: function(event) %7B%0A%09%09if($('#newgroup-init').is(%22:visible%22)) %7B%0A%09%09%09event.stopPropagation();%0A%09%09%09$('#newgroup-form').show();%0A%09%09%09$('#newgroup-init').hide();%0A%09%09%09$('#newgroupname').focus();%0A%09%09%7D else %7B%0A%09%09%09$('#newgroup-form').hide();%0A%09%09%09$('#newgroup-init').show();%0A%09%09%09$('#newgroupname').val('');%0A%09%09%7D%0A%09%7D,%0A%0A
 %09finishD
@@ -1202,24 +1202,24 @@
 n (ready) %7B%0A
-
 %09%09if (!Group
@@ -2261,116 +2261,34 @@
 %7B%0A%09%09
-e.stopPropagation();%0A%09%09$('#newgroup-form').show();%0A%09%09$('#newgroup-init').hide();%0A%09%09$('#newgroupname').focus(
+GroupList.toggleAddGroup(e
 );%0A%09
@@ -3062,31 +3062,64 @@
 edGroups));%0A
-
 %09%09%09%09%09%7D%0A
+%09%09%09%09%09GroupList.toggleAddGroup();%0A
 %09%09%09%09%7D%0A%09%09%09%7D%0A%09

put Add Group elements hiding and showing into its own function