﻿function addCommentButtonClick(event)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#myComments').addClass('invisible');
	$('#myCommentsUpdateProgress').text('Adding comment, please wait...');
	$('#myCommentsUpdateProgress').removeClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		commentHandlerUrl,

		data: {
			Action:		'Save',
			ID:			myCommentId,
			ItemID:		$('#itemID').val(),
			Value:		$('#myCommentsValue').val(),
			IsOfficial:	false,
			IsPublic:	true, //new Boolean($('#myCommentsArePublic').attr('checked')),
			IsBanned:	false
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Comment.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#myCommentsUpdateProgress'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#myCommentsUpdateProgress').text('Your comments could not be added at this time, please try again later.');

			setTimeout(function() {
				$('#myCommentsUpdateProgress').addClass('invisible');
				$('#myComments').removeClass('invisible');
			}, 10000);
		}
	});
}

function onAddCommentCallback(success, id)
{
	if (!success)
	{
		// If the call logged us out, don't try showing/hiding anything since it's already been done

		var cookie = $.cookie('blLogin');

		if (!cookie)
			return;

		$('#myCommentsUpdateProgress').text('Your comments could not be added at this time, please try again later.');

		setTimeout(function() {
			$('#myCommentsUpdateProgress').addClass('invisible');
			$('#myComments').removeClass('invisible');
		}, 10000);

		return;
	}

	$('#myCommentsUpdateProgress').addClass('invisible');
	$('#myComments').removeClass('invisible');

	updateCommentsList();
}

function removeCommentButtonClick(event, commentID)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	$('#myComments').addClass('invisible');
	$('#myCommentsUpdateProgress').text('Removing comments, please wait...');
	$('#myCommentsUpdateProgress').removeClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		commentHandlerUrl,

		data: {
			Action:		'Remove',
			IDs:		myCommentId
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Comment.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#myCommentsUpdateProgress'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#myCommentsUpdateProgress').text('Your comments could not be removed at this time, please try again later.');

			setTimeout(function() {
				$('#myCommentsUpdateProgress').addClass('invisible');
				$('#myComments').removeClass('invisible');
			}, 10000);
		}
	});
}

function onRemoveCommentCallback(success)
{
	if (!success)
	{
		// If the call logged us out, don't try showing/hiding anything since it's already been done

		var cookie = $.cookie('blLogin');

		if (!cookie)
			return;

		$('#myCommentsUpdateProgress').text('Your comments could not be removed at this time, please try again later.');

		setTimeout(function() {
			$('#myCommentsUpdateProgress').addClass('invisible');
			$('#myComments').removeClass('invisible');
		}, 10000);

		return;
	}

	myCommentId = -1;

	$('#myCommentsUpdateProgress').addClass('invisible');
	$('#myComments').removeClass('invisible');

	updateCommentsList();
}

function reportOffensiveCommentLinkClick(event, commentId)
{
	if (typeof(event) == 'undefined')
		event = jQuery.event.fix(window.event);
	else
		event = jQuery.event.fix(event);

	event.preventDefault();

	var offensiveDiv = $('#offensiveCommentNotes');

	var x = event.pageX, y = event.pageY, $window = $(window);

	var maxX = $window.width(), maxY = $window.height();

	if ((event.clientX + offensiveDiv.width()) >= maxX)
		x -= offensiveDiv.width();

	if ((event.clientY + offensiveDiv.height()) >= maxY)
		y -= offensiveDiv.height();

	offensiveDiv.css('top',  y + 'px');
	offensiveDiv.css('left', x + 'px');

	var cookie = $.cookie('blLogin');

	if (cookie)
	{
		$('#reportOffensiveCommentName').addClass('invisible');
		$('#reportOffensiveCommentEmail').addClass('invisible');
	}
	else
	{
		$('#reportOffensiveCommentName').removeClass('invisible');
		$('#reportOffensiveCommentEmail').removeClass('invisible');
	}

	$('#offensiveCommentId', offensiveDiv).val(commentId);

	offensiveDiv.show();
}

function reportOffensiveComment(comments, name, email)
{
	$('#reportOffensiveCommentButtons').addClass('invisible');
	$('#reportOffensiveCommentStatus').text('Reporting your concern, please wait...');

	if (typeof(name) != 'undefined')
	{
		comments = 'From: ' + name + ' <' + email + ">\n\n" + comments;
	}

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		commentHandlerUrl,

		data: {
			Action:		'ReportOffensive',
			IDs:		$('#offensiveCommentId').val(),
			Comments:	comments,

			Tag:		''
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Comment.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#reportOffensiveCommentStatus'
			});

			$('#reportOffensiveCommentButtons').removeClass('invisible');
			$('#reportOffensiveCommentStatus').addClass('invisible');
			$('#offensiveCommentNotes').hide();

			$('#offensiveCommentNotes textarea').val('');
			$('#reportOffensiveCommentName input').val('');
			$('#reportOffensiveCommentEmail input').val('');
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			$('#reportOffensiveCommentStatus').text('The comment could not be reported at this time, please try again later...');

			setTimeout(function() {
				$('#reportOffensiveCommentStatus').addClass('invisible');
				$('#reportOffensiveCommentButtons').removeClass('invisible');
			}, 10000);
		}
	});
}

function onReportOffensiveCommentCallback(success, commentId)
{
/*
	if (!success)
	{
		alert("There was an error communicating with the server and the comment could\nnot be reported at this time, please try again later.");
		$('#commentOffensive' + commentId).removeClass('invisible');

		return;
	}
*/
}

function updateCommentsList()
{
	var commentsList = $('#commentsList');

	if (!commentsList || !commentsList.length)
		return;

	commentsList.text('Retrieving comments, please wait...');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		commentHandlerUrl,

		data: {
			Action:				'GetList',
			ItemIDs:			$('#itemID').val(),
			IsPublic:			true,
			IsBanned:			false,
			IncludeUserDetails:	true,

			Tag:				'<label>All</label><searchtype>regular</searchtype>'
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Comment.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#commentsList'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			commentsList.text('The comments list could not be retrieved at this time, please try again later.');
		}
	});
}

function updateMyPrivateComments()
{
	var myPrivateComments = $('#myPrivateComments');

	if (!myPrivateComments || !myPrivateComments.length)
		return;

	myPrivateComments.text('Retrieving your private comments, please wait...');
	myPrivateComments.removeClass('invisible');

	$.ajax({
		cache:		false,

		type:		'POST',
		url:		commentHandlerUrl,

		data: {
			Action:				'GetList',
			ItemIDs:			$('#itemID').val(),
			UserIDs:			myUserId,
			IsBanned:			false,
			IncludeUserDetails:	true,

			Tag:				'<label>User</label><searchtype>regular</searchtype>'
		},

		dataType:	'text',

		success:	function(data, textStatus)
		{
			myPrivateComments.addClass('invisible');

			$.xslt({
				xml:			data,
				xslUrl:			xslBaseUrl + 'Comment.xsl',
				xslCache:		isLive,		/* Don't cache if debugging */
				dataTypeXML:	true,
				target:			'#myPrivateComments'
			});
		},

		error:		function(XMLHttpRequest, textStatus, error)
		{
			myPrivateComments.text('Your private comments could not be retrieved at this time, please try again later.');
		}
	});
}

function renderMyComments()
{
	var myComments = $('#myComments');

	var cookie = $.cookie('blLogin');

	$('#commentsHeader').removeClass('invisible');

	if (cookie)
	{
		if (!myComments || !myComments.length)
		{
			myComments = $('#commentList');

			if (myComments && myComments.length > 0)
				myComments.removeClass('invisible');
		}
		else
			myComments.removeClass('invisible');
	}
	else
	{
		if (!myComments || !myComments.length)
		{
			myComments = $('#commentList');

			if (myComments && myComments.length > 0)
				myComments.addClass('invisible');
		}
		else
			myComments.addClass('invisible');
	}
}

function showAllComments(event, show)
{
	if (event && typeof(event.preventDefault) != 'undefined')
		event.preventDefault();

	if (show)
	{
		$('#showAllCommentsLink').hide();
		$('#showAllCommentsNotLink').show();
		$('#allComments').show();
	}
	else
	{
		$('#allComments').hide();
		$('#showAllCommentsNotLink').hide();
		$('#showAllCommentsLink').show();
	}
}

