var vPostId = '';

$(document).ready(function(){
	
//Code to display reply boxes
	$('.replyToThisLink').click(function(){
		
		newvPostId = $(this).attr('id').replace(/replyToThisLink/,'');
		if (newvPostId == vPostId) {
			// it's the same ID, so just close up the box
			$('#discussionReplyBox').slideUp(100); // hide existing box
			vPostId = '';
			return false;
		} else {
			vPostId = newvPostId;						
		}
		
		$('#discussionReplyBox').hide(100); // hide existing box
	
		$('#oneComment' +vPostId).after($('#discussionReplyBox')); // move box into place

		// set the right image depending on whether it's a reply or a new message
		if (vPostId != '_0'){

			$('#yourReplyImg').attr('src','/images/txt/txtYourReply.gif');
			$('[name="replyToId"]', '#discussionReplyBox').val(vPostId);
			
			// set up "quote" inside the replybox
			/* ---- MO: Temporarily removed pending changes to community server's HTML escape setup ----
							
				quoteVal = '[quote user="'+ $('.user_name','#oneComment'+vPostId).html()   +'"]' + jQuery.trim($('.right','#discBox'+vPostId).html().replace(/<div .*>/gmi,'')) + '[/quote]\n\n';
	
			*/
			
			quoteVal = "";
			
			$('#replyText', '#discussionReplyBox').val(quoteVal);
			
		} else {
			// reply to a specific message
			$('#yourReplyImg').attr('src','/images/txt/txtJoinDiscussion.gif');
			$('[name="replyToId"]', '#discussionReplyBox').val('');
			$('#replyText', '#discussionReplyBox').val(''); // make sure the replybox is empty

		}

		// set our other field variables
		//$('[name="contentId"]', '#discussionReplyBox').val(vContentId);
		$('[name="titleText"]', '#discussionReplyBox').val(vTitleText);
		$('#textCharLimit').html(maxTextLength + ' characters remaining');
		

		$('#discussionReplyBox').slideDown(); // reveal the box

		return false;
	});
	
	//Code to handle "cancel reply" clicks
	$('.replyCancel').click(function(){
		$('#discussionReplyBox').hide(100); // hide the box
	});

	//Code to handle "Report This" clicks
	$('.reportThisLink').click(function(){
		var vReportPostId = $(this).attr('id').replace(/reportLink/,'');
		var thisLink = this;
	
		// now send an ajax request to flag this
		$.get(
			$(thisLink).attr('href'),
			 function(){
				// replace the text
				$(thisLink).parent().append('<div>This comment has been flagged.</div>');
				$(thisLink).remove();
			 });
		return false;
		
	
	}); // end of click handler
	
	// make sure blank comments don't submit
	$('#submit').click(function(){
		if ($('#replyText').val() == ''){
			alert('You must enter a comment before clicking submit.');
			return false;
		}
	});

	// handle the character count
	var maxTextLength = 800;
	$('#textCharLimit').html(maxTextLength + ' characters remaining');
	$('#replyText').bind('keyup',function(){
		var currentLength = $('#replyText').val().length;
		if (currentLength >= maxTextLength){
			$('#replyText').val($('#replyText').val().substring(0,maxTextLength));	
			currentLength--;
		}
		var diff = maxTextLength - currentLength;
		$('#textCharLimit').html(diff + " characters remaining");
		
	});

}); // end of document ready function


