/*******************************************************************************
* +--------------------------[ File Revision Info ]--------------------------+ *
* | $Revision::                                                           $: | *
* | $Date::                                                               $: | *
* | $Author::                                                             $: | *
* +--------------------------------------------------------------------------+ *
* | $Id::                                                                 $: | *
* +--------------------------------------------------------------------------+ *
*******************************************************************************/
jQuery(document).ready(function() 
{	
	$jQ('.commentAdd').click(function(){
		$jQ('#addCommentForm').show();
	});

	$jQ('#commentSubmitBtn').click(function(){
		addComment();
	});

	$jQ('#commentCancelBtn').click(function(){
		clearCommentForm();
		$jQ('#addCommentForm').hide();
	});

	$jQ('.replyAdd').click(function(){
		$id = $jQ(this).attr("id").substr("reply".length);
		
		$msgQuote = $jQ('#comment'+$id+" .msg").attr("innerHTML");
		$usrQuote = $jQ('#comment'+$id+" .comment-author").attr("innerHTML");
		
		$quotMsg = '<em>'+$usrQuote+'</em> wrote:<br />' + '<span class="indent">&quot;'+$msgQuote+'&quot;</span>';

		$jQ('#quotedMessage').attr("innerHTML", $quotMsg);
		$jQ('#quotedMessage').show();
		
		$jQ('#_replyID').attr("value", $id);
		$jQ('#addCommentForm').show();
	});
	
	$jQ('.flagComment').click(function(){
		$id = $jQ(this).attr("id").substr("flag".length);

		$msgQuote = $jQ('#comment'+$id+" .msg").attr("innerHTML");
		$usrQuote = $jQ('#comment'+$id+" .comment-author").attr("innerHTML");
		
		$quotMsg = '<em>'+$usrQuote+'</em> wrote:<br />' + '<span class="indent">&quot;'+$msgQuote+'&quot;</span>';

		$jQ('#flagQuotedMessage').attr("innerHTML", $quotMsg);
		$jQ('#flagQuotedMessage').show();

		$jQ('#f_replyID').attr("value", $id);
		$jQ('#flagCommentForm').show();
	});
	
	$jQ('#flagSubmitBtn').click(function(){
		flagComment();
	});

	$jQ('#flagCancelBtn').click(function(){
		clearFlagForm();
		$jQ('#flagCommentForm').hide();
	});
});

// 
function addComment()
{
	_entityType = $jQ('#_entityType').val();
	_entityID   = $jQ('#_entityID').val();
	_userID     = $jQ('#_userID').val();
	_replyID    = $jQ('#_replyID').val();
	_subject    = $jQ('#s2u2b2j2e2c2t').val();
	_comment    = $jQ('#m2e2s2s2a2g2e').val();
	_quoteMsg   = $jQ('#quotedMessage').html();

	$jQ.ajax({ 
		type: "POST", 
		url:  "../includes/ajax/comment-add.php",
		data: "userID="+_userID+"&entityType="+_entityType+"&entityID="+_entityID+"&subject="+_subject+"&comment="+_comment+"&replyID="+_replyID,
		success: function(data) 
		{
			retVal = String(data);
			if (retVal.match('Sorry') || retVal.match('Error')) 
			{
				$jQ.prompt(data);
			}
			else
			{
				clearCommentForm();

				// Increment onscreen comments count
				var numComments = parseInt($jQ('#numComments').html()) + 1;
				
				$jQ('#addCommentForm').hide();
				$jQ('#commentContainer').append(data);
				$jQ('#numComments').html(numComments);
			}
			return false;
		},
		error: function(data) {
			$jQ.prompt(data);
		},
	});
}

// 
function flagComment()
{
	_entityType = $jQ('#f_entityType').val();
	_entityID   = $jQ('#f_entityID').val();
	_userID     = $jQ('#f_userID').val();
	_flagID     = $jQ('#f_replyID').val();
	_reason     = $jQ('#r2e2a2s2o2n').val();
	_quoteMsg   = $jQ('#flagQuotedMessage').html();
	
	$jQ.ajax({ 
		type: "POST", 
		url:  "../includes/ajax/comment-flag.php",
		data: "userID="+_userID+"&entityType="+_entityType+"&entityID="+_entityID+"&flagID="+_flagID+"&reason="+_reason,
		success: function(data) 
		{
			retVal = String(data);
			if (retVal.match('Sorry') || retVal.match('Error')) 
			{
				$jQ.prompt(data);
			}
			else
			{
				clearFlagForm();
				$jQ('#flagCommentForm').hide();
				$jQ.prompt(data);
			}
			return false;
		},
		error: function(data) {
			$jQ.prompt(data);
		},
	});
}

function clearCommentForm()
{
	$jQ('#quotedMessage').attr("innerHTML","");
	$jQ('#s2u2b2j2e2c2t').attr("value","");
	$jQ('#m2e2s2s2a2g2e').attr("value","");
	$jQ('#_replyID').attr("value","");
	$jQ('#quotedMessage').hide();
}

function clearFlagForm()
{
	$jQ('#flagQuotedMessage').attr("innerHTML","");
	$jQ('#r2e2a2s2o2n').attr("value","");
	$jQ('#_flagID').attr("value","");
	$jQ('#flagQuotedMessage').hide();
}