/*
$(document).ready(function () {
	$("#workslink").click(function () {
		$('html, body').animate({
			scrollTop: $("#works").offset().top
		}, 1000);
	});
});
*/

function showMoreWorks()
{
	$('#worksmore').hide();
	$('#loading').html($('<div id="loader">'))
	
	setTimeout(function()
	{
		$('#loading').load('/webAjax/pages/load/?tmpl=works_ajax.html');
	}, 500);
	
}

function addComment()
{
	var name = $('#user_name').val();
	var email = $('#user_email').val();	
	var web = $('#user_web').val();
	var content = $('#comment_content').val();
	
	var error = false;
	
	if (isEmpty(name))
	{
		error = true;
		$('#user_name').addClass('error_ins');
	}
	else $('#user_name').removeClass('error_ins');
	
	if (isEmpty(content))
	{
		error = true;
		$('#comment_content').addClass('error_ins');
	}
	else $('#comment_content').removeClass('error_ins');
	
	if (isEmpty(email) || isEmail(email))
	{
		error = true;
		$('#user_email').addClass('error_ins');
	}
	else $('#user_email').removeClass('error_ins');			
	
	if (error === false)
	{
		$.ajax({
			url: '/webAjax/comments/add/',
			type: 'post',
			data: 'comment_content=' + escape(encodeURIComponent(content)) + '&post_id=' + $('#post_id').val() + '&user_name=' + escape(encodeURIComponent(name)) + '&user_web=' + web + '&user_email=' + email + '&tmpl=comment.html',
			success: function(data)
			{
				$('#comment_content').val('');
				$('#comments_list').append($(data));
				$('#add_comments_box').fadeOut();
				
				setTimeout(function()
				{
					$('#add_comments_box').show();
				}, 40000);
			}
		});
	}	
}

function cancelCommentOwner()
{
	$('#add_comment_owner').remove();
}

function addCommentOwner(comment_id)
{
	$('#add_comment_owner').remove();
	var html = '<div id="add_comment_owner"><p class="pause"><textarea class="w100" id="comment_content_owner" style="height: 200px;"></textarea></p><p><input type="button" onclick="_addCommentOwner(' + comment_id + ');" value="Добавить" />&nbsp;&nbsp;&nbsp;<a href="javascript:void(null);" onclick="cancelCommentOwner();" class="soft small">Отменить</a></p></div>';
	
	$('#comment_content_' + comment_id).after($(html));
	$('#comment_content_owner').focus();
}

function _addCommentOwner(comment_id)
{
	var content = $('#comment_content_owner').val();

	$.ajax({
		url: '/webAjax/comments/add/',
		type: 'post',
		data: 'comment_content=' + escape(encodeURIComponent(content)) + '&post_id=' + $('#post_id').val() + '&comment_parent=' + comment_id + '&tmpl=comment.html',
		success: function(data)
		{
			$('#add_comment_owner').fadeOut();
			$('#comment_' + comment_id).after($(data));
		}
	});	
}

function checkComment(comment_id)
{
	$('#comment_' + comment_id).fadeOut();
	$.ajax({ url: '/webAjax/comments/checkComment/?comment_id=' + comment_id });
}


function isEmail(str)
{
	return !str.match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i);
}

function isEmpty(str)
{
	str = $.trim(str);
	str = str.replace(/ /g,'').replace(/<(.*?)>/gi,'').replace(/<\\(.*?)>/gi,'');
	if (str == '') return true;
	else return false;
}
