/*******************************************************************************
 *												FORUM 
 ******************************************************************************/

function checkThreadMessage()
{
	if ($('#title').val() == '')
	{
		alert(N_JS_ENTER_SUBJECT);
		return false;
	}

	if ($('#content').val() == '')
	{
		alert(N_JS_ENTER_TEXT);
		return false;
	}

	if ($('#voting').attr('checked'))
	{
		if ($('#question').val() == '')
		{
			alert(N_JS_ENTER_QUESTION);
			return false;
		}

		var badoptions = false;
		$('#votelist input').each(function(el){
			if ($(this).val() == '')
			{
				alert(N_JS_ENTER_ALL_ANSWERS);
				
				badoptions = true;
				return false;
			}
		});
		
		if (badoptions)	return false;
	}
	
	return true;
}

function saveThread(target, threadId, mode, targetId)
{
	if (!checkThreadMessage()) return;

	$('#createBtn').hide();

	if (mode == 'edit') {
		var request_url = 'ajax.php?page=forum&action=editthread&thread_id='+threadId+'&target='+target+'&tid='+targetId;
	} else {
		var request_url = 'ajax.php?page=forum&action=createThread'+'&target='+target+'&tid='+targetId;
	}

	var parameters = $('#createThreadForm').formSerialize();  
	
	$.post(request_url, parameters, function(response) {

		eval( "var reply = "+response+";");
		if (reply.result) {
			$('#savebad').hide();
			location.href = target + 'Thread'+reply.thread_id;
		} else {
			$('#createBtn').show();
			$('#error').html(reply.message);
			$('#error').show();
		}
	});

};

function addTargetComment(target, threadId, redirect)
{
	$('#error').hide();
	
	if ($('#content').val() == '')
	{
		alert(N_JS_ENTER_TEXT);
		return;
	}
	
	var parameters = $('#fForumComment').formSerialize();

	$('#replyBtn').hide();

	$.post('ajax.php?page=forum&action=addComment&threadId='+threadId+'&target='+target, parameters, function(reply) {
		if (reply.result) {
			$('#error').hide();
			if ($('#message'+reply.parent_id+'children').attr('id') == undefined)
				reply.parent_id = 0;
			if (redirect) {
				location.href = '/' + target + 'Thread' + reply.forumThreadId + '&m=' + reply.forumCommentId + '#mycomment' + reply.forumCommentId;
			} else {
				$('#thread_comments').append(reply.info);//( $('#forumComments').html() + reply.info);
				$('#comment'+reply.forumCommentId).fadeIn(1000);
				$('#groupCommentArea').val('');
				returnReplyForm();
				$('#replyBtn').show();
			}
		} else {
			showNotification(reply.message, 'error');
		}
	}, "json");

	return;
};


function deleteGroupComment(target, targetId, threadId, commentId, is_thread)
{
	var confirmText = is_thread == 1 ? Q_JS_DELETE_THREAD : Q_JS_DELETE_COMMENT;
	if (!confirm(confirmText)) return false;

	$.post('ajax.php?page=forum&action=deleteComment&commentId='+commentId+'&threadId='+threadId+'&target='+target, {p:1}, function(reply) {
		if (reply.result) {
			if (reply.thread)
			{
				location.href = target + targetId;
			} else
				$('#comment' + commentId).fadeOut(200);
		} else {
			showNotification(reply.message, 'error');
		}
	}, "json");

	return;
};

function replyThread(target)
{
	$('#groupCommentPost').show();
	location.href = '#groupCommentPost';
	$('#content').focus();
};

function showEditComment(target, commentId)
{
	$('#content'+commentId).hide();	
	$('#eddiv'+commentId).show();

	$.post('ajax.php?page=forum&action=getCommentText&commentId='+commentId+'&target='+target, $('#edContent'+commentId).fieldSerialize(), function(reply) {
		if (reply.result) {
			//$('#replyForm').appendTo($('#eddiv'+commentId));
			$('#eddiv'+commentId).show();
			$('#edContent'+commentId).val(reply.info);
		}
	}, "json");
};

function cancelEditComment(commentId)
{
	$('#eddiv'+commentId).hide();
	$('#content'+commentId).show();	
};

function showPopupComment(target, commentId)
{
	$.post( 'ajax.php?page=forum&action=getcomment', {"commentId":commentId,"target":target}, function(reply){

            if(reply.commentLength > 500){
                modalWidth = 570;
            }else{
                modalWidth = false;
            }

            reply.content = '<div class="social_thread social_thread_popup"><div class="thread_comments">' + reply.content + '</div></div>';
            reply.title = 'Комментарий';
            completeDialogBox(reply,modalWidth);
	}, "json");
}

function editComment(target, commentId)
{
	$.post('ajax.php?page=forum&action=editComment&commentId='+commentId+'&target='+target, $('#edContent'+commentId).fieldSerialize(), function(response) {
		eval( "var reply = "+response+";");
		if (reply.result) {
			$('#messagecontent'+commentId).html(reply.info);
			//$('#content'+commentId).html(reply.info);
		}
	});

	cancelEditComment(commentId);
};

function quoteMessage(target, commentId)
{
	var selectedText = quoteSelection();
	
	if (selectedText == '') {
		$.post('ajax.php?page=forum&action=getCommentText&commentId='+commentId+'&target='+target, $('#edContent'+commentId).fieldSerialize(), function(response) {
			eval( "var reply = "+response+";");
			if (reply.result) 
			{
				selectedText = reply.info;	
				selectedText = '[QUOTE]' + selectedText + '[/QUOTE]' + "\n\n";
				$('#content').val(selectedText);
				$('#replyForm').appendTo($('#formHolderComment'+commentId));
				$('#parent_reply_id').val(commentId);
				$('#cancel_button').show();
				
			} else {
				showNotification(reply.message, 'error');
			}
		});
	} else {
		selectedText = '[QUOTE]' + selectedText + '[/QUOTE]' + "\n\n";
		$('#content').val(selectedText);
		$('#replyForm').appendTo($('#formHolderComment'+commentId));
		$('#parent_reply_id').val(commentId);
		$('#cancel_button').show();
	}
};


function quoteSelection() 
{
    if (document.selection) 
    {
	    theSelection = document.selection.createRange().text;
        return theSelection;
    }else
    {
	    theSelection1 = window.getSelection();
        return theSelection1;
    }
	
	return '';
};

/*******************************************************************************
 *												FORUM POLLS 
 ******************************************************************************/

function addVoteOption()
{
	var voteCounter = $('#voteCounter').val();

	if (voteCounter == 20) 
	{
		return;
	}
	
	var elem = $('#voteItem').clone();

	var elemA = $('a', elem);
	elemA.attr('href', 'javascript:removeVoteOption('+voteCounter+');');
	elem.attr('id', 'voteItem'+voteCounter);
	elem.css('display', '');

	$('#votelist').append(elem);

	$('#voteCounter').val(parseInt(voteCounter)+1);
};

function removeVoteOption(id)
{
	$('#voteItem'+id).remove();
};

function vote(target, threadId)
{
	var optionId = 0;
	$('input:radio[@name=voteoption]').each(function(el){
		if ($(this).attr('checked'))
		{
			optionId = $(this).val();
		}
	});

	if (optionId > 0)
	{
		$.post('ajax.php?page=forum&action=vote&threadId='+threadId+'&optionId='+optionId+'&target='+target, {p:1}, function(response) {
			eval( "var reply = "+response+";");
			if (reply.result) 
			{
				location.href = target+'Thread' + threadId;
			}
		});
		
	}
};

function showVoteResults()
{
	$('#voteResults').show();
	$('#voteQuestion').hide();
};

function showVoteQuestion()
{
	$('#voteQuestion').show();
	$('#voteResults').hide();
	
};

/*******************************************************************************
 *												VISITS 
 ******************************************************************************/

function setTargetRead(target, targetId)
{
	$.post('ajax.php?page=forum&action=setTargetRead&tid='+targetId+'&target='+target, {p:1}, function(response) {
		eval( "var reply = "+response+";");
		if (reply.result) {
			window.location.reload();
		}
	});
};

function setTargetsRead(target)
{
	$.post('ajax.php?page=forum&action=setTargetsRead'+'&target='+target, {p:1}, function(response) {
		eval( "var reply = "+response+";");
		if (reply.result) {
			window.location.reload();
		}
	});
};
