var commentsSent = false;
var firstPageLoaded = false;
lang = Array();

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}

function increaseTotalComments(x)
{
	$('totalCountComments').innerHTML = parseInt($('totalCountComments').innerHTML) + x;
	this.commentsCount += x;
}

function decreaseTotalComments(x)
{
	$('totalCountComments').innerHTML = parseInt($('totalCountComments').innerHTML) - x;
	this.commentsCount -= x;
}

function addComments(objectId, objectType, userId)
{
	if (!$('comment').value.trim())
		return false;

	if (this.commentsSent == true)
		return false;

	if ($('remainingChars').value <= 0)
		return false;//display error message?

	this.commentsSent = true;

	$('comment').disabled = true;
	$('commSendBtn').disabled = true;
	$('commClearBtn').disabled = true;

	captcha = (Object.isElement($('captcha')))? $F('captcha') : false ;
	captcha_key = (Object.isElement($('hidden')))? $F('hidden') : false ;
	var codes = (Object.isElement($('code')))?  $F('code') : 0;

	progress.show('commentsWrtWrapper');

	new Ajax.Request(PUBLIC_URL+'comments/add',
	{
		method: 'post',
		postBody: Object.toQueryString({ objectId: objectId, objectType: objectType, uid: userId, comment: $F('comment'), captcha_key:captcha_key, captcha: captcha, code: codes }),
		onSuccess: function(transport) {
			responseTexts = transport.responseText;

			var patt = /Error:captcha/g;
			if (responseTexts.match(patt)) {
				$('captcha_block').innerHTML = responseTexts;
				$('captcha_block').style.display = '';
				captchaImgSrc = $('captcha_img').src;
				f5captcha('captcha_img', captchaImgSrc);
				$('captcha').value = '';
				$('comment').disabled = false;
				$('commSendBtn').disabled = false;
				$('commClearBtn').disabled = false;
				progress.hide('commentsWrtWrapper');
				this.commentsSent = false;
				systemMessenger.set(lang['27716_captcha_profile_comments'], 0);
				systemMessenger.display();
				return false;
			}
			if (responseTexts.match(/Error:spam/g)) {
				$('comment').value = '';
				$('comment').disabled = false;
				$('commSendBtn').disabled = false;
				$('commClearBtn').disabled = false;
				progress.hide('commentsWrtWrapper');
				toggleCommentsWriteBlock();
				this.commentsSent = false;
				systemMessenger.set(responseTexts, 0);
				systemMessenger.display();
				return false;
			}

			$('comment').value = '';
			$('comment').disabled = false;
			$('commSendBtn').disabled = false;
			$('commClearBtn').disabled = false;

			$('comments').insert({top: transport.responseText});
			this.commentsSent = false;
			increaseTotalComments(1);
			progress.hide('commentsWrtWrapper');
			toggleCommentsWriteBlock();
		},
		onFailure: function() {
			progress.hide('commentsWrtWrapper');
			systemMessenger.set('Failed to send comment!', 0);
			systemMessenger.display();
		}
	});
}

function removeComment(objectId, objectType, commentId, userId)
{
	if (this.commentsSent == true)
		return false;

	this.commentsSent = true;

	new Ajax.Request(PUBLIC_URL+'comments/remove',
	{
		method: 'post',
		postBody: Object.toQueryString({ objectId: objectId, objectType: objectType, uid: userId, commentId: commentId}),
		onSuccess: function(transport) {
			decreaseTotalComments(1);
			new Effect.SwitchOff($('com'+commentId));
			this.commentsSent = false;

			if (this.commentsCount == 0) {
				getComments(objectId, objectType, 1);
			}
		},
		onFailure: function() {
			this.commentsSent = false;
			systemMessenger.set('Failed to delete comment!', 0);
			systemMessenger.display();
		}
	});
}

function getComments(objectId, objectType, userId, page)
{
	progress.show('commentsWrapper');

	new Ajax.Request(PUBLIC_URL + 'comments/get/objectId/' + objectId + '/objectType/' + objectType + '/uid/' + userId + '/page/' + page,
	{
		method: 'get',
		onSuccess: function(transport) {
			$('commentsBlock').replace(transport.responseText);
			progress.hide('commentsWrapper');

			if (this.firstPageLoaded)
				new Effect.ScrollTo('commentsApp');

			this.firstPageLoaded = true;
		},
		onFailure: function() {
			progress.hide('commentsWrapper');
			systemMessenger.set('Failed to load comments!', 0);
			systemMessenger.display();
		}
	});
}

function toggleCommentsWriteBlock() {
	if (!$('addcommentlink').visible()) {
		if (Object.isElement($('captcha_img'))) {
			f5captcha('captcha_img', $('captcha_img').src);
		}

		new Effect.SlideDown($('addcommentlink'), {duration: 0.7});
	} else {
		new Effect.SlideUp($('addcommentlink'), {duration: 0.7});
	}
}

function countRemainingChars(maxLength)
{
	currentLength = $('comment').value.length;

	if (!currentLength)
		return false;

	if (currentLength > maxLength) {
		//set to 0
		$('remainingChars').value = 0;

		if (!$('commSendBtn').disabled)
			$('commSendBtn').disabled = true;
	} else {
		//update
		$('remainingChars').value = maxLength - currentLength;

		if ($('commSendBtn').disabled)
			$('commSendBtn').disabled = false;
	}
}