﻿function Tagger(tagger, options)
{
	constructor();
	
	var savingTextTimeout;
	var errorTimeout;
	
	function constructor()
	{
		addJavaScriptToTagger();
	}
	
	function addJavaScriptToTagger()
	{
		Event.observe(options.addButton, "click", function (event) { addButtonClick(); Event.stop(event); } );
		Event.observe(options.textBox, "keydown", function (event) { if (event.keyCode == Event.KEY_RETURN) { addButtonClick(); Event.stop(event); } });
	}
	
	// Click handler for the add tags button
	function addButtonClick()
	{
		if (options.textBox.value != "")
		{
			makeRequest(options.remoteFunction, handleResponse, handleError, getParameters());
		}
	}
	
	// Gets a list of parameters to pass to the ajax function
	function getParameters()
	{
		var parameters = new Object();
		parameters.uniqueId = options.uniqueId;
		parameters.tags = options.textBox.value;
		parameters.userId = options.userId;
		
		return parameters;
	}
	
	// Makes an Ajax request to the web service
	function makeRequest(remoteFunctionName, responseHandler, errorHandler, parameters)
	{	
		// Build a post body based on all of the parameters in the list
		var postBody;
		for (var parameter in parameters)
		{
			if (!postBody)
				postBody = parameter + "=" + parameters[parameter];
			else
				postBody += "&" + parameter + "=" + parameters[parameter];
		}
		
		var url = "/AJAX/AjaxHandler.asmx/" + remoteFunctionName;		
		var ajaxOptions =
		{
			method : "post",
			postBody : postBody,
			onSuccess : responseHandler,
			onFailure : errorHandler,
			on404 : errorHandler
		}

		ajaxRequest = new Ajax.Request(url, ajaxOptions);
		
		// Show some status text if request takes longer than 1 second, cancel the request after 10 seconds 
		savingTextTimeout = setTimeout(function () { options.statusArea.innerHTML = options.savingText; }, 1000);
		errorTimeout = setTimeout(function () { handleError(); ajaxRequest.transport.abort(); }, 10000); 
	}
	
	// Handles the response from the AJAX request
	function handleResponse(response)
	{
		var returnValues = response.responseXML;	
		
		// Parse the response for a list of tags
		var currentTagsList = returnValues.getElementsByTagName("CurrentTags")[0];
		var tagArray = $A(currentTagsList.getElementsByTagName("string"));
		
		/* This section updates the list of tags on the page
		   It currently causes several problems, so it won't be used at this point
		if (options.tagListArea)
		{
			// Clear the list of tag links and then add a link for each of the tags
			$A(options.tagListArea.childNodes).each(function (childNode) { options.tagListArea.removeChild(childNode); });
			
			var tagListHeader = document.createElement("strong");
			tagListHeader.innerHTML = "Tags: ";
			options.tagListArea.appendChild(tagListHeader);
			
			tagArray.each(function(tag) { addTagLink(tag.firstChild.nodeValue); });
		}
		*/
		
		clearTimeout(savingTextTimeout);
		clearTimeout(errorTimeout);
		
		options.statusArea.innerHTML = options.savedText;
		options.textBox.value = "";
		
		setTimeout(function () { options.statusArea.innerHTML = ""; }, 2000);
	}
	
	// Adds a new link for to the tag list for the tag
	function addTagLink(tag)
	{
		var newLink = document.createElement("a");
		newLink.innerHTML = tag;
		newLink.href = "#";
		
		options.tagListArea.appendChild(newLink);
	}
	
	// Handles any errors from the AJAX call
	function handleError()
	{
		clearTimeout(savingTextTimeout);
		options.statusArea.innerHTML = options.errorText;
		setTimeout(function () { options.statusArea.innerHTML = ""; }, 3000);
	}
}


