// FTwitt Search Group Management
// Coded by Marian Busoi, 4 Copimaj

// Deletes a group (either a saved group or just the form)
function groups_delete(obj) {
	obj = $(obj).parent().parent();
	
	if(obj.hasClass("new_group")){
		obj.remove();
	} else { 
		if(window.confirm("Are you sure you want to delete this group?")) {
			// group is from db
			var groupID = obj.attr("id").substr(6); // fetch group id
			
			$.get('includes/ajax/groups_delete.php', {id: groupID},
				  function (data) {
					if(data!='1') {
						$('#basic-modal-content').html("Could not delete group. Please try again!");
						$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );
					} else {
						obj.remove();
					}
				  });
			  
		}
	}
}

// Displays a form for editing a group
function groups_edit(obj) {
	var txt = $(obj).parent().siblings("a");

	var editGroupForm = "<input size='10' value='" + txt.html() + "' onkeypress='if(event.keyCode==13 || event.keyCode==10) groups_update($(this).siblings(\".gr_controls\").find(\".gr_edit\"));' onfocus='if(this.value==\"Group name\") this.value=\"\";' onblur='if(this.value==\"\") this.value=\"Group name\";' />";
	
	txt.replaceWith( $(editGroupForm) );
	
	var saveBtn = $('<a class="gr_edit" href="#" title="save" onclick="groups_update(this)">save</a>');
	$(obj).replaceWith( saveBtn );	
}

// Updates an existing group
function groups_update(obj) {
	var groupID = $(obj).parent().parent().attr("id").substr(6); // fetch group id
	
	var inpt = $(obj).parent().siblings("input");
	
	if(inpt.val().length < 4) {
		$('#basic-modal-content').html("Group name must be at least 4 characters long!");
		$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );
	} 
	else {
		$.getJSON('includes/ajax/groups_edit.php?callback=?', {name: inpt.val(), id: groupID},
				function (data) {
					if(data && data.error) {
						$('#basic-modal-content').html(data.error);
						$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );					
					} 
					else if(data) {	// alter the DOM a bit
						inpt.replaceWith( $('<a class="gr_link" href="#">' + inpt.val() + '</a>') );
						var editBtn = $('<a class="gr_edit" href="#" title="edit" onclick="groups_edit(this)">edit</a>');
						$(obj).replaceWith( editBtn );
					}
				});
	}
}

// Displays a form for adding a new search group
function groups_new() {
	var addGroupForm = "<input size='10' value='Group name' onkeypress='if(event.keyCode==13 || event.keyCode==10) groups_save($(this).siblings(\".gr_controls\").find(\".gr_edit\"));' onfocus='if(this.value==\"Group name\") this.value=\"\";' onblur='if(this.value==\"\") this.value=\"Group name\";' />";
	$("<li></li>").addClass('li_gr_wrapper new_group')
				  .appendTo( $('#gr_listing') )
				  .append( $(addGroupForm) )
				  .append( $("<div></div>").addClass("gr_controls")
										   .append( $('<a class="gr_edit" href="#" title="save" onclick="groups_save(this)">save</a>') )
										   .append( $('<a class="gr_delete" href="#" title="delete group" onclick="groups_delete(this)">delete</a>') )
						  );
}

// Saves a new group
function groups_save(obj) {
	var inpt = $(obj).parent().siblings("input");
	
	if(inpt.val().length < 4) {
		$('#basic-modal-content').html("Group name must be at least 4 characters long!");
		$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );
	} 
	else {
		$.getJSON('includes/ajax/groups_add.php?callback=?', {name: inpt.val()},
				function (data) {
					if(data && data.error) {
						$('#basic-modal-content').html(data.error);
						$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );					
					} 
					else if(data) {	// alter the DOM a bit
						inpt.parent().attr("id", "group_" + data.id);
						inpt.parent().removeClass("new_group");
						inpt.replaceWith( $('<a class="gr_link" href="#">' + inpt.val() + '</a>') );
						$(obj).attr("title", "edit");
						$(obj).click(function () { groups_edit(this); });
						$(obj).html("edit");
						tm.wipeOut();
						bindGroupLinks();
					}
				});
	}
}