function voteForTopic(topic_id, vote_factor) {
	if(!topic_id || !vote_factor) return false;

	var userActionPoints = $('userActionPoints').get('text').toInt();

	if(userActionPoints <= 1) return false;
	
	if (parseInt(sspNgr) == 1) {
		var ht = '\
		<div id="popUpWinClose" style="float:right;"><a onclick="ngr_common.hideForm();return false;" href="#" style="font-size:10px;">закрыть</a></div><div style="font-size:16px; color:#eb3d00;font-family:Tahoma;margin-bottom:10px;width:600px;">Поддержите тему, пожалуйста</div>\
			<div id="social-like-block"></div>\
		</div>';
													
		ngr_common.showGreyBack(ht, true);
		socialNgr = socialNgrClass.initialize({ pageTitle: ngrTopicTitle, pageDescription: "", pageUrl: "http://www.nashgorod.ru/forum/viewtopic.php?t=" + topic_id, pageImage: "", text: "" });
	}

	options = {
		url: '/forum/ajax/points.php?action=vote_topic',
		data: { 'topic':topic_id, 'factor':vote_factor },
		onRequest: function() {
			$('topic' + topic_id + 'down').destroy();
			$('topic' + topic_id + 'up').destroy();
		},
		onSuccess: function(result) {
			if(!result) return false;
			if(result['result']) {
				// кармпа
				$('topic' + topic_id + 'points').set('html', (result['points'] > 0) ? '<span style="color:#008000">' + result['points'] + '</span>' : (result['points'] < 0) ? '<span style="color:#800000">' + result['points'] + '</span>' : result['points']);

				// заряды
				$('userActionPoints').set('html', userActionPoints - 2);
			}
			else if(result['error']) {
				alert(result['error']);
			}
		},
		onFailure: function(xhr) {
			return false;
		}
	};
	new Request.JSON(options).send();
}

function voteForUser(user_id, vote_factor) {
	if(!user_id || !vote_factor) return false;

	//var userActionPoints = $('userActionPoints').get('text').toInt();
	var userActionPoints = parseInt(document.getElementById('userActionPoints').innerHTML);
	if(userActionPoints <= 2) return false;

	options = {
		url: '/forum/ajax/points.php?action=vote_user',
		data: { 'user':user_id, 'factor':vote_factor },
		onRequest: function() {
			if($('user' + user_id + 'down') != null) $('user' + user_id + 'down').destroy();
			if($('user' + user_id + 'up') != null) $('user' + user_id + 'up').destroy();
		},
		onSuccess: function(result) {
			if(!result) return false;
			if(result['result']) {
				// карма
				if($('user' + user_id + 'points') != null) $('user' + user_id + 'points').set('html', (result['points'] > 0) ? '<span style="color:#008000">' + result['points'] + '</span>' : (result['points'] < 0) ? '<span style="color:#800000">' + result['points'] + '</span>' : result['points']);

				// заряды
				if($('userActionPoints') != null) $('userActionPoints').set('html', userActionPoints - 3);

				// сила
				if($('user' + user_id + 'power') != null) {
					var userPower = $('user' + user_id + 'power').get('text').toFloat();
					userPower = (vote_factor == 'bad') ? userPower - 0.1 : userPower + 0.1;
					userPower = userPower.round(2);
					$('user' + user_id + 'power').set('html', (userPower > 0) ? '<span style="color:#008000">' + userPower + '</span>' : (userPower < 0) ? '<span style="color:#800000">' + userPower + '</span>' : userPower);
				}
			}
			else if(result['error']) {
				alert(result['error']);
			}
		},
		onFailure: function(xhr) {
			return false;
		}
	};
	new Request.JSON(options).send();
}

function voteForUserInPost(user_id, post_id, vote_factor) {
	if(!user_id || !vote_factor || !post_id) return false;

	var userActionPoints = $('userActionPoints').get('text').toInt();

	if(userActionPoints <= 2) return false;

	options = {
		url: '/forum/ajax/points.php?action=vote_user',
		data: { 'user':user_id, 'factor':vote_factor },
		onRequest: function() {
			$('userInPost' + post_id + 'down').destroy();
			$('userInPost' + post_id + 'up').destroy();
		},
		onSuccess: function(result) {
			if(!result) return false;
			if(result['result']) {
				// карма
				$('userInPost' + post_id + 'points').set('html', (result['points'] > 0) ? '<span style="color:#008000">' + result['points'] + '</span>' : (result['points'] < 0) ? '<span style="color:#800000">' + result['points'] + '</span>' : result['points']);

				// карма других постов юзера
				$$('.carma' + user_id).each(function(el) {
					el.set('html', $('carma' + post_id).get('html'));
				});

				// заряды
				$('userActionPoints').set('html', userActionPoints - 3);
			}
			else if(result['error']) {
				alert(result['error']);
			}
		},
		onFailure: function(xhr) {
			return false;
		}
	};
	new Request.JSON(options).send();
}

function voteForPost(post_id, vote_factor) {
	if(!post_id || !vote_factor) return false;

	var userActionPoints = $('userActionPoints').get('text').toInt();

	if(userActionPoints <= 0) return false;
	
	if (parseInt(sspNgr) == 1) {
		var ht = '<div id="popUpWinClose" style="float:right;"><a onclick="ngr_common.hideForm();return false;" href="#" style="font-size:10px;">закрыть</a></div><div style="font-size:16px; color:#eb3d00;font-family:Tahoma;margin-bottom:10px;width:600px;">Поддержите сообщение, пожалуйста</div><div id="social-like-block"></div></div>';
		ngr_common.showGreyBack(ht, true);
		socialNgr = socialNgrClass.initialize({ pageTitle: eval("ngrSocialMessage" + post_id), pageDescription: "", pageUrl: "http://www.nashgorod.ru/forum/viewtopic.php?p=" + post_id + "#p" + post_id, pageImage: "", text: "" });	
	}
	
	options = {
		url: '/forum/ajax/points.php?action=vote_post',
		data: { 'post':post_id, 'factor':vote_factor },
		onRequest: function() {
			$('post' + post_id + 'down').destroy();
			$('post' + post_id + 'up').destroy();
		},
		onSuccess: function(result) {
			if(!result) return false;
			if(result['result']) {
				// кармпа
				$('post' + post_id + 'points').set('html', (result['points'] > 0) ? '<span style="color:#008000">' + result['points'] + '</span>' : (result['points'] < 0) ? '<span style="color:#800000">' + result['points'] + '</span>' : result['points']);

				// заряды
				$('userActionPoints').set('html', userActionPoints - 1);
			}
			else if(result['error']) {
				alert(result['error']);
			}
		},
		onFailure: function(xhr) {
			return false;
		}
	};
	new Request.JSON(options).send();
}

function flipVoteForUser(user_id) {
	if(!user_id) return false;

	var userActionPoints = $('userActionPoints').get('text').toInt();
	var userPower = $('user' + user_id + 'power').get('text').toFloat();

	if(userActionPoints <= 2) return false;

	options = {
		url: '/forum/ajax/points.php?action=flip_user',
		data: { 'user':user_id },
		onRequest: function() {
			$('flip' + user_id + 'points').destroy();
		},
		onSuccess: function(result) {
			if(!result) return false;
			if(result['result']) {
				// карма
				$('user' + user_id + 'points').set('html', (result['points'] > 0) ? '<span style="color:#008000">' + result['points'] + '</span>' : (result['points'] < 0) ? '<span style="color:#800000">' + result['points'] + '</span>' : result['points']);

				// заряды
				$('userActionPoints').set('html', userActionPoints - 3);

				// сила
				userPower = userPower + result['power_diff'];
				userPower = userPower.round(2);
				$('user' + user_id + 'power').set('html', (userPower > 0) ? '<span style="color:#008000">' + userPower + '</span>' : (userPower < 0) ? '<span style="color:#800000">' + userPower + '</span>' : userPower);
			}
			else if(result['error']) {
				alert(result['error']);
			}
		},
		onFailure: function(xhr) {
			return false;
		}
	};
	new Request.JSON(options).send();
}

/* blogs */
function voteForBlog(blog_id, vote_factor) {
	if(!blog_id || !vote_factor) return false;

	var userActionPoints = $('userActionPoints').get('text').toInt();
	if(userActionPoints <= 1) return false;

	options = {
		url: '/forum/ajax/points.php?action=vote_blog',
		data: { 'blog':blog_id, 'factor':vote_factor },
		onRequest: function() {
			$('blog' + blog_id + 'down').destroy();
			$('blog' + blog_id + 'up').destroy();
		},
		onSuccess: function(result) {
			if(!result) return false;
			if(result['result']) {
				// карма
				$('blog' + blog_id + 'points').set('html', result['points']);

				// заряды
				$('userActionPoints').set('html', userActionPoints - 2);
			}
			else if(result['error']) {
				alert(result['error']);
			}
		},
		onFailure: function(xhr) {
			return false;
		}
	};
	new Request.JSON(options).send();
}






if(!window.getMouseXY) {
	function getMouseXY(event) {
		if(!event) return false;

		var x = 0;
		var y = 0;
		if(!event) var event = window.event;
		x = event.pageX || (event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft));
		y = event.pageY || (event.clientY + (document.body.scrollTop || document.documentElement.scrollTop));

		return {'x':x, 'y':y};
	}
}

var ratingInfoTimer = false;
var ratingInfoStore = [];

function showRatingInfo(id, type, event) {
	if(!id) return false;
	if(!type) return false;
	if(!event) return false;

	coords = getMouseXY(event);
	if(ratingInfoTimer) $clear(ratingInfoTimer);
	ratingInfoTimer = showRatingInfoTimed.delay(500, 1, [id, type, coords]);
}

function hideRatingInfo() {
	if(ratingInfoTimer) $clear(ratingInfoTimer);
	ratingInfoTimer = hideRatingInfoTimed.delay(500);
}

function showRatingInfoTimed(id, type, coords) {
	if(!id) return false;
	if(!type) return false;
	if(!coords) return false;

	getRatingInfoBlock(id, type, coords);
}

function hideRatingInfoTimed() {
	if($defined(document.id('ratingInfoBlock'))) document.id('ratingInfoBlock').destroy();
}

function getRatingInfoBlock(id, type, coords) {
	if(!id) return false;
	if(!type) return false;
	if(!coords) return false;

	if(ratingInfoTimer) $clear(ratingInfoTimer);

	var x = coords.x + 8;
	var y = coords.y + 8;

	// draw block
	if(!$defined(document.id('ratingInfoBlock'))) {
		new Element('div', {
			'id': 'ratingInfoBlock'
		}).inject(document.body, 'top').set('opacity', 0.9).addEvent('mouseover', function() {
		    if(ratingInfoTimer) $clear(ratingInfoTimer);
		}).addEvent('mouseout', function() {
		    hideRatingInfo();
		});
	}
	document.id('ratingInfoBlock').setStyles({
		'top': y,
		'left': x
	});

	if(!ratingInfoStore[type + '_' + id]) {
		// request
		options = {
			url: '/forum/ajax/ratinginfo.php?action=ratinginfo',
			data: { 'id':id, 'type':type },
			onRequest: function() {
				document.id('ratingInfoBlock').set('html', '<img src="http://pics.nashgorod.ru/images/ajax.gif" style="vertical-align:middle" /> загрузка');
			},
			onSuccess: function(result) {
				if(result['result']) {
					if($defined(document.id('ratingInfoBlock'))) document.id('ratingInfoBlock').set('html', result['html']);
					ratingInfoStore[type + '_' + id] = result['html'];
					fixRatingInfoBlockPosition();
				}
				else if(result['errors']) {
					if($defined(document.id('ratingInfoBlock'))) document.id('ratingInfoBlock').set('html', result['errors']);
				}
				else {
                	document.id('ratingInfoBlock').destroy();
				}
			}
		};
		new Request.JSON(options).send();
	}
	else {
		if($defined(document.id('ratingInfoBlock'))) document.id('ratingInfoBlock').set('html', ratingInfoStore[type + '_' + id]);
		fixRatingInfoBlockPosition();
	}
}

function fixRatingInfoBlockPosition() {
	if(!$defined(document.id('ratingInfoBlock'))) return false;

	document.id('ratingInfoBlock').setStyle('left', Math.min(document.getCoordinates().width.toInt() - document.id('ratingInfoBlock').getStyle('width').toInt() - 40, document.id('ratingInfoBlock').getStyle('left').toInt()) + 'px');
	document.id('ratingInfoBlock').setStyle('top', Math.min(document.getScroll().y + document.getCoordinates().height.toInt() - document.id('ratingInfoBlock').getStyle('height').toInt() - 20, document.id('ratingInfoBlock').getStyle('top').toInt()) + 'px');
}
