var Comments = {
	init: function()
	{
		if($('sendcomment') && use_xmlhttprequest == 1)
		{
			Event.observe($('sendcomment'), "click", Comments.sendComment.bindAsEventListener(this));
		}
	},

	sendComment: function(e)
	{
		// Don't use ajax if the user is replying to someone
		if($('reply_to_profile').value != 'default' && $('reply_to_profile').value != current_user)
		{
			return false;
		}

		Event.stop(e);

		if(this.sending_comment)
		{
			return false;
		}

		this.sending_comment = 1;
		this.spinner = new ActivityIndicator("body", {image: imagepath + "/spinner_big.gif"});

		// Get form data
		postData = "uid=" + $('to_uid').value + "&reply_to_profile=" + $('reply_to_profile').value.replace(/\+/g, "%2B");

		// Get form data
		if(editorloaded == 1)
		{
			postData = postData + "&message=" + encodeURIComponent($('message_new').value);
		}
		else
		{
			postData = postData + "&message=" + encodeURIComponent($('message').value);
		}
		
		new Ajax.Request('xmlhttp.php?action=profile&section=comments', {method: 'post', postBody: postData, onComplete: function(request) { Comments.sendCommentDone(request); }});

		if(this.spinner)
		{
			this.spinner.destroy();
			this.spinner = '';
		}
		this.sending_comment = 0;

		return false;
	},

	sendCommentDone: function(request)
	{
		if(request.responseText.match(/<redirect>(.*)<\/redirect>/))
		{
			url = request.responseText.match(/<redirect>(.*)<\/redirect>/);
			window.location = url[1];
		}
		else if(request.responseText.match(/<error>([^<]*)<\/error>/))
		{
			message = request.responseText.match(/<error>([^<]*)<\/error>/);

			if(!message[1])
			{
				message[1] = "An unknown error occurred.";
			}

			if(this.spinner)
			{
				this.spinner.destroy();
				this.spinner = '';
			}
			alert('There was an error posting your comment:\n\n'+message[1]);
		}
		else if(request.responseText.match(/id="comment-([0-9]+)"/))
		{
			$('comment_form').insert({
				'after': request.responseText
			});

			// Delete the "no comments" message
			if($('no_comments_found'))
			{
				$('no_comments_found').remove();
			}

			Form.reset('newcomment');
		}
		else
		{
			alert('Unknown response');
		}
	},

};

if(comments_ajax == 1)
{
	Event.observe(document, 'dom:loaded', Comments.init);
}

Event.observe(window, 'load', function() {
	$$("a.reply").each(function(a)
	{
		a.observe('click', function(c) {
			var username = $(this).readAttribute('username');
			var uid = $(this).readAttribute('userid');
			$('reply_to_profile').value = uid;

			if(editorloaded == 1)
			{
				clickableEditor.performInsert('[b]' + username + '[/b], ', '', true, false);
			}
			else
			{
				$('message').value += '[b]' + username + '[/b], ';
			}

			$('replyingto').update(replying_message.replace('{1}', username)).show();

			$('linkcancel').observe('click', function(c) {
				$('replyingto').hide();
				$('reply_to_profile').value = 'default';
				Event.stop(c);
			});

			Event.stop(c);
		});
	});

	$$("a.delete_link").each(function(a)
	{
		a.observe('click', function(c) {
			if(!confirm(confirm_delete_message))
			{
				Event.stop(c);
			}
		});
	});

});

