var CommentWall =
{
	ReplyTo : function(commentId)
	{
		if ($('#comment_' + commentId + ' .replyform').length > 0)
		{
			$('#comment_' + commentId + ' .replyform').remove();
		}
		else
		{
			var base = $('#comment_reply_template').html();
			$('#comment_' + commentId + ' .actions').after('<div class="replyform">' + base + "</div>");
			$('#comment_' + commentId + ' #pci').val(commentId);
			$('#comment_' + commentId + ' .replyform input[name=cancel_button]').click(function(){
				CommentWall.ReplyTo(commentId)
			});
			CommentWall.EnablePost('#comment_' + commentId + ' form');			
		}
	},

	EnablePost : function(id)
	{
		$(id).ajaxForm({ 
	        dataType: 'json',
	   		clearForm: true,
	   		resetForm: true,
	   		beforeSubmit: CommentWall.Validate,
			success: CommentWall.OnPost
		});
	},
	OnPost : function(rsp, statusText)
	{
		if (rsp.status == 'pass')
		{
			if (rsp.pci > 0)
			{
				$('#comment_replies_' + rsp.pci).append(rsp.html);
				CommentWall.ReplyTo(rsp.pci);
			}
			else
			{
				$('#comments .comments').append(rsp.html);
			}
		}
		else if (rsp.status == 'fail')
		{
			alert('Alex - make this work!');
		}
		else
		{
			alert('Alex - make this work - modal login!');
		}
	},

	Validate : function(formData, jqForm, options)
	{
		var comment = jQuery.trim(jqForm[0].user_comment.value);
		var error = $(jqForm[0]).children('.comment_errors');
		if (comment.length > 2000)
		{
			$(error).html('<strong style="color:#f00">Comments are limited to 2000 characters.</strong>');
			return false;
		}
		else if (comment.length <= 0)
		{
			$(error).html('<strong style="color:#f00">Please provide a comment.</strong>');
			return false;
		}
		else
		{
			$(error).html('');
			return true;
		}
	},
	
	CommentRate : function(el, url)
	{
		$.get(url, function(data) {
			$(el).parent().children('span.prop_value').html(data);
			$(el).parent().children('a.prop').hide();
		});
		return false;
	},
	CommentFlag : function(el, id)
	{
		if (confirm("Do you really want to flag this comment?"))
		{
			$.get('/ajax/flag.php?t=comment&id=' +id, function(data){
					$(el).parent().parent().html(data);
			});	
		}
	}
}