		    
function replaceTagBlock( typeOfTagBlock, idOfResource, urlEncodedGroupNamesString, idOfUser, headline ) {
	jQuery.ajax({
        url: "/AloeView/ajax/getTagBlock",
        type: 'get',  // get is the default type
        data: {
		    resourceId: idOfResource,
		    urlEncodedGroupNames: urlEncodedGroupNamesString,
		    userId: idOfUser,
		    tagBlockHeadline: headline,
		    tagBlockType: typeOfTagBlock
		},
		success: function( data, textStatus, jqXHR ) {
			handleNewTagBlock( data );
		},
		error: function( jqXHR, textStatus, errorThrown ) {
			// do nothing
		}
	});
}

function handleNewTagBlock(response) {
	var whereToReplace = document.getElementById("tagsInside");
	whereToReplace.innerHTML = response;
	return response;
}


function deleteUserTag( idOfResource, tagName, headline ) {
	jQuery.ajax({
        url: "/AloeView/ajax/deleteUserTag",
        type: 'get',  // get is the default type
        data: {
		    resourceId: idOfResource,
		    userTag: tagName
		},
		success: function( data, textStatus, jqXHR ) {
			document.getElementById('new_tag').value = '';
			replaceTagBlock( "", idOfResource, "", "", headline );
		},
		error: function( jqXHR, textStatus, errorThrown ) {
			// do nothing
		}
	});
}

function addUserTag( idOfResource, headline, syntaxWarning ) {
	var newTag = document.getElementById('new_tag').value;
	var executeTagging = true;

	if(newTag != null && newTag.indexOf(",") >= 0){
		executeTagging = window.confirm(syntaxWarning); 
	}

	if(executeTagging){
		jQuery.ajax({
	        url: "/AloeView/ajax/addUserTag",
	        type: 'get',  // get is the default type
	        data: {
				resourceId: idOfResource,
				userTag: document.getElementById('new_tag').value
			},
			success: function( data, textStatus, jqXHR ) {
				document.getElementById('new_tag').value = '';
				replaceTagBlock( "", idOfResource, "", "", headline );
			},
			error: function( jqXHR, textStatus, errorThrown ) {
				// do nothing
			}
		});
	}
}

function changeTagSortOrder( tagBlockType, order, idOfResource, idOfGroup, idOfUser, headline ) {
	jQuery.ajax({
        url: "/AloeView/ajax/tagSortOrder",
        type: 'get',  // get is the default type
        data: {
	        tagSortOrder: order
		},
		success: function( data, textStatus, jqXHR ) {
	        replaceTagBlock( tagBlockType, idOfResource, idOfGroup, idOfUser, headline );
		},
		error: function( jqXHR, textStatus, errorThrown ) {
			// do nothing
		}
	});
}


//Offer autocomplete functionality for the element specified via jquerySelector.
function offerTagAutocomplete( jquerySelector ) {
	
	// Two helper functions
	function splitTagString( val ) {
		return val.split( /\s+/ );
	}
	function extractLast( term ) {
		return splitTagString( term ).pop();
	}

	// Request parameters to use
	var dataFillerFunction = function( request ) {
		var dataToUse = {};
		dataToUse.searchTagsString = extractLast( request.term );
		return( dataToUse );
	};

	// Fill in result of AJAX call into array for autocompletion
	var getCompletionArrayFunction = function( dataFromResponse ) {
		return( dataFromResponse );
	};

	// What to do, if an element is selected
	var selectFunction = function( referenceElement, ui ) {

		var terms = splitTagString( referenceElement.attr( 'value' ) );
		// remove the current input
		terms.pop();
		// add the selected item
		terms.push( ui.item.value );
		// add placeholder to get the comma-and-space at the end
		terms.push( "" );
		var newValue = terms.join( " " );
		
		referenceElement.attr( 'value', newValue );
		
		return( false );
	};

	// *********************************************
	// Additional parameters for jquery autocomplete
	// *********************************************
	var additionalAutocompleteParameters = {}; // additional parameters for autocomplete
	additionalAutocompleteParameters.search = function() {
		// custom minLength
		var term = extractLast( this.value );
		if ( term.length < 1 ) {
			return false;
		}
	};
	additionalAutocompleteParameters.focus = function() { /* prevent value inserted on focus */ return false; };
	//additionalAutocompleteParameters.minLength = 3;
	
	offerAloeAutocomplete( jquerySelector, "/AloeView/ajax/getVisibleUserTags", additionalAutocompleteParameters, dataFillerFunction,
			getCompletionArrayFunction, selectFunction );
}

