$(document).ready(function () {
	$('.comment_anchor').css('opacity', 0.35);
	
	$('.comment_anchor').mouseover(function () {
		$(this).css('opacity', 1);
	});
	
	$('.comment_anchor').mouseout(function () {
		$(this).css('opacity', 0.35);
	});
	
	$('.post_info_inner').corner ('5px');
});

function votePost (postId, typeId, mark) {
	if (mark != 1) {
		mark = 0;
	}
	
	$.ajax ({
		url: baseURI + '/blogs/ajax_vote_post/' + typeId + '/' + postId + '/' + mark + '?scode=' + scode,
		dataType: 'xml',
		type: 'get',
		error: function () { new Error ('Произошла ошибка!');  },
		success: function (xml) {
			var result = $(xml).find('result').text();
			
			if (result == '0') {
				new Error ('Произошла ошибка!');
			} else {
				new Message ('Ваш голос учтен!');
				var rating = $(xml).find('rating').text();
				if (rating > 0) {
					$('#post_rating_'+typeId+'_'+postId).text ('+' + rating);
				} else {
					$('#post_rating_'+typeId+'_'+postId).text (rating);
				}
				
				if (mark == 1) {
					$('#pots_vote_plus_'+typeId+'_'+postId).find('img').attr ('src', baseURI + '/images/post-voted-plus.png');
					$('#pots_vote_minus_'+typeId+'_'+postId).find('img').attr ('src', baseURI + '/images/post-vote-minus-cant.png');
				} else {
					$('#pots_vote_plus_'+typeId+'_'+postId).find('img').attr ('src', baseURI + '/images/post-vote-plus-cant.png');
					$('#pots_vote_minus_'+typeId+'_'+postId).find('img').attr ('src', baseURI + '/images/post-voted-minus.png');
				}
				
				document.getElementById('pots_vote_plus_'+typeId+'_'+postId).onclick = function () {return false;};
				document.getElementById('pots_vote_minus_'+typeId+'_'+postId).onclick = function () {return false;};
				
				$('#pots_vote_plus_'+typeId+'_'+postId).click (function () { new Error ('Повторное голосование запрещено!') });
				$('#pots_vote_minus_'+typeId+'_'+postId).click (function () { new Error ('Повторное голосование запрещено!') });
			}
		}
	});
}

function voteBlog (blogId,  mark) {
	if (mark != 1) {
		mark = 0;
	}
	
	$.ajax ({
		url: baseURI + '/blogs/ajax_vote_blog/' + blogId + '/' + mark + '?scode=' + scode,
		dataType: 'xml',
		type: 'get',
		error: function () { new Error ('Произошла ошибка!');  },
		success: function (xml) {
			var result = $(xml).find('result').text();
			
			if (result == '0') {
				new Error ('Произошла ошибка!');
			} else {
				new Message ('Ваш голос учтен!');
				var rating = $(xml).find('rating').text();
				if (rating > 0) {
					$('#blog_rating_number').text (rating);
				} else {
					$('#blog_rating_number').text (rating);
				}
				
				if (mark == 1) {
					$('#blog_vote_plus').find('img').attr ('src', baseURI + '/images/post-voted-plus.png');
					$('#blog_vote_minus').find('img').attr ('src', baseURI + '/images/post-vote-minus-cant.png');
				} else {
					$('#blog_vote_plus').find('img').attr ('src', baseURI + '/images/post-vote-plus-cant.png');
					$('#blog_vote_minus').find('img').attr ('src', baseURI + '/images/post-voted-minus.png');
				}
				
				document.getElementById('blog_vote_plus').onclick = function () {return false;};
				document.getElementById('blog_vote_minus').onclick = function () {return false;};
				
				$('#blog_vote_plus').click (function () { new Error ('Повторное голосование запрещено!') });
				$('#blog_vote_minus').click (function () { new Error ('Повторное голосование запрещено!') });
			}
		}
	});
}

function subscribeBlog (blogId) {
	$.ajax ({
		url: baseURI + '/blogs/subscribe/' + blogId + '?scode=' + scode,
		dataType: 'xml',
		type: 'post',
		error: function () { new Error ('Произошла ошибка!');  },
		success: function (xml) {
			var result = $(xml).find('result').text();
			var idHref = '#subscribe_blog';
			
			if (result == '-1') {
				$(idHref).text('Подключиться');
			}
			else if (result == '1') {
				$(idHref).text('Покинуть блог');
			}
		}
	});
}

function showBlogDescription (blogId) {
	if (blogId < 1) {
		$('#blog_description').text ('Персональный блог');
		return;
	}
	
	$.ajax ({
		url: baseURI + '/blogs/ajax_blog_description/' + blogId,
		type: 'GET',
		dataType: 'xml',
		error: function () { new Error ('Произошла ошибка!'); },
		success: function (xml) {
			if ($(xml).find('error').text() == '1') {
				new Error ('Произошла ошибка!');
				return;
			}
			
			$('#blog_description').text($(xml).find('description').text());
		}
	});
}

function showBlogMembers (blogId) {
	if ($('#blog_members').text() == '') {
		$('#blog_members').load(baseURI + '/blogs/ajax_load_members/' + blogId).show();
		return;
	}
	
	if ($('#blog_members').css('display') == 'none') {
		$('#blog_members').show();
	} else {
		$('#blog_members').hide();
	}
}