Drupal.checkPlain = function (text) {
	if(text.replace) {
		return text.replace('&', '&amp;')
						 .replace('<', '&lt;')
						 .replace('"', '&quot;')
						 .replace(/^\s+/g, '')
						 .replace(/\s+$/g, '')
						 .replace('\n', '<br />');
	} else return text;
}
 
Drupal.serialize = function (data, prefix) {
	prefix = prefix || '';
	var out = '';
	for (i in data) {
		var name = prefix.length ? (prefix +'[' + i +']') : i;
		if (out.length) out += '&';
		if (typeof data[i] == 'object') {
			out += Drupal.serialize(data[i], name);
		}
		else {
			out += name +'=';
			out += Drupal.encodeURIComponent(data[i]);
		}
	}
	return out;
}

if (Drupal.jsEnabled) {
	$(document).ready(function () {
		// Note: all tag fields are autocompleted, and have already been initialized at this point.
		$('input.form-tags').each(function () {
			// Fetch settings.
			var o = Drupal.settings.communityTags;
			var sequence = 0;
		
			// Patch for drupal_to_js() bug in Drupal 5.1
			if (typeof o.tags.push == 'undefined') {
				o.tags = [];
			}

			// Show the textfield and empty its value.
			var textfield = $(this).val('').css('display', 'inline');

			// Prepare the add Ajax handler 
			var addHandler = function () {
				// Send existing tags and new tag string.
				$.post(o.url, Drupal.serialize({ sequence: ++sequence, tags: o.tags, add: textfield[0].value }), function (data) {
					data = Drupal.parseJson(data);
					if (data.status && sequence == data.sequence) {
						widgetlist.empty();
						$.each(data.tags, function (i,n) {
							addTag(n, widgetlist);
//							alert( "Item #" + i + ": " + n );
						});
						plainlist.empty();
						$.each(data.taglist, function (i,n) {
							addTag(n, plainlist);
//							alert( "Item #" + i + ": " + n );
						});

						prepTagDelete();

					}
				});

				// Add tag to local list
				o.tags.push(textfield[0].value);
				o.tags.sort(function (a,b) { a = a.toLowerCase(); b = b.toLowerCase(); return (a>b) ? 1 : (a<b) ? -1 : 0; });
			//	updateList();
				
				// Clear field and focus it.
				textfield.val('').focus();
			};

			
			//	 add the button.
			var button = $('<input type="button" class="form-button" value="'+ Drupal.checkPlain(o.add) +'" />').click(addHandler);
			$(this.form).submit(function () { addHandler(); return false; });


			// Callback to update the tag lists.
			function addTag(newtag, addlist) {
			//	list.empty();
				addlist.append('<li>'+ newtag +'</li>');
			}
			
			
			

			// Create widget markup.
			var widget = $('<div class="tag-widget"></div>');
			textfield.before(widget);
			widget.append(textfield).append(button);
			var widgetlist = $('ul.tags_block', "#tagcloud");
			var plainlist = $('ul.taglist', "#taglist");

		});

		// Prepare the delete Ajax handler.
		var prepTagDelete = function() {
			$('a.tags_delete').click(function(linkdata){
				var taglink = $('a[@href$='+this.getAttribute('href')+']').next().attr('href');
				
				$('#tags_shortlist li > a[@href$='+taglink+']').parent().remove();
				$(this.parentNode).remove();
				
				$.get(this.href, null, null);
				return false;
				// Clear textfield and focus it.
				textfield.val('').focus();
			});
		};
		
		prepTagDelete();

		
	});
}
