(function($) {
	//Animation plugin for the rating
	$.fn.rateanimate = function(speed, callback, callbackend) {
		callback = (typeof(callback) == 'function' ? callback : function(){});
		callbackend = (typeof(callbackend) == 'function' ? callbackend : function(){});
		speed = speed || 'normal';
		$(this).css({
			'position': 'absolute', 
			'left': '-'+$(window).width()+'px'
		});
		callback.call(this);
		$(this).css({
			'display': 'inline',
			'width': 'auto',
			'margin-left': '0px'
		});
		var e_width = $(this).width();
		var p_width = $(this).parent().css('text-align', 'left').width();
		$(this).css({
			'white-space': 'nowrap',
			'width': e_width
		});
		$(this).hide().css({
			'position': '',
			'left': ''
		});
		$(this).animate({ 
			width: 'show',
			marginLeft: ((p_width - e_width) / 2)+'px'
		}, speed, function() {
			callbackend.call(this);
		});
		return this;
	}
	
	$(document).ready(function() {
		//Ajax messages		
		ajaxmsg = function(text, stop) {
			if (!text) return;
			var self = this;
			self.text = text;
			if (!($().data('events') ? $().data('events').ajaxStart : false)) {
				$().ajaxStart(function() {
					$.showmsg(self.text);
				});
			}
			if (!($().data('events') ? $().data('events').ajaxStop : false)) {
				$().ajaxStop(function() {
					$.hidemsg();
				});
			}
		};
		ajaxmsgclear = function() {
			if ($().data('events').ajaxStart) delete $().data('events').ajaxStart;
			if ($().data('events').ajaxStop) delete $().data('events').ajaxStop;
		};
		//"Global" vars
		var tid = parseInt($("#tid").html());
		var token = $('#rating_token').html();
		//Rating topics code with hidden variables scope
		(function() {
			if (!tid) return;
			var parent = $('.rating-parent');
			if ($(parent).size() > 1) {
				alert('Something wrong... to rating tables for the topic');
				return;
			}
			var ratingspan = $('.ratingscore', parent);
			var img = $('#topicrating');
			var opts = [
				{
					'name': 'good', 'num': 1
				}, {
					'name': 'bad', 'num': -1
				}, {
					'name': 'neut', 'num': 0
				}
			];
			var inprogress = false;
			function ratetopic(val) {
				if (val == undefined) return;
				var self = this;
				if (self.inprogress) {
					setTimeout(function() {
						ratetopic(val);
					}, 500);
					return;
				}
				ajaxmsg(rating_lang.addingvote);
				$.getJSON($().attr('URL'), {
					action: 'rateajax',
					what: 'topic',
					id: tid,
					rate: val.toString(),
					token: token
				}, function(data) {
					if (data.result) {
						self.inprogress = true;
						var unbind;
						var num;
						if(!data.good) {
							unbind = 'good';
						} else if(!data.bad) {
							unbind = 'bad';
						} else if(!data.neut) {
							unbind = 'neut';
						} else {
							return;
						}
						$('.postrate'+unbind, parent).unbind('click').addClass('rating-rate-disabled');
						for (i = 0; i < opts.length; i++) {
							if (opts[i].name == unbind) continue;
							var binder = $('.postrate'+opts[i].name, parent);
							var events = $(binder).data('events') || {};
							if (!events.click) {
								num = opts[i].num;
								$(binder).bind('click', function() {
									ratetopic(num);
								}).removeClass('rating-rate-disabled');
							}
						}
						$(ratingspan).rateanimate('normal', function() {
							$('span', this).removeClass('ratingscore'+(data.rating < 0 ? 'good' : 'bad')).html(data.rating)
								.addClass('ratingscore'+(parseInt(data.rating) < 0 ? 'bad' : 'good'))
						}, function() {
							self.inprogress = false;
						});
						$(img).attr('src', data.image).attr('alt', data.votes).attr('title', data.votes);
	//						$(img).rateanimate(1000, function() {
	//							$('img', this).attr('src', data.image);
	//						});
					}
					token = data.token;
					ajaxmsgclear();
					$.showmsg(data.msg, 2000);
				});
			}
			$('.postrategood:not(.rating-rate-disabled)', parent).bind('click', function() {
				ratetopic(1);
			});
			$('.postratebad:not(.rating-rate-disabled)', parent).bind('click', function() {
				ratetopic(-1);
			});
			$('.postrateneut:not(.rating-rate-disabled)', parent).bind('click', function() {
				ratetopic(0);
			});
		})();
		
		//Rating users with hidden variables scope
		(function() {
			$('.addpower:not(.addpower-disabled)').bind('click', function() {
				if ($(this).data('done')) return;
				var uid = parseInt($(this).find('+ div.rating_uid').html());
				if (!tid && !uid) return;
				$(this).data('done', true);
				el = this;
				ajaxmsg(rating_lang.addingdealpower);
				$.getJSON($().attr('URL'), {
					action: 'rateajax',
					what: 'user',
					id: tid,
					uid: uid,
					token: token
				}, function(data) {
					//console.log(data);
					if (data.result) {
						$(el).unbind('click').addClass('addpower-disabled').attr('alt', data.alt).attr('title', data.alt).parent().find('.dealspower_num').fadeOut('normal', function() {
							$(this).html(data.rating).fadeIn('normal');
						});
					} else {
						$(el).data('done', false);
					}
					token = data.token;
					ajaxmsgclear();
					$.showmsg(data.msg, 2000);
				});
			});
		})();
	});
})(jQuery);