// extended star rating
function initStarsX(){
	$('ul.x-stars').each(function(){
		var _rate = $(this);
		if(!_rate.hasClass('x-stars-disabled')){
			var _rateLi = _rate.find('li');
			var _rateA = _rate.find('a');
			var _showRate = $('div.rate-tip span.bg-left');
			_showRate.hide();
			_rate.hover(function(){
				$(this).addClass('x-stars-hovered');
				_rateLi.each(function(_el){
					if($(this).hasClass('hover')) {
						_rate.attr('_hover',_el);
						$(this).removeClass('hover');
					}
					if($(this).hasClass('active')) {
						_rate.attr('_active',_el);
						$(this).removeClass('active');
					}
				});
			},function(){
				$(this).removeClass('x-stars-hovered');
				if($(this).attr('_hover') && !_rateLi.eq($(this).attr('_hover')).hasClass('hover') && !$(this).attr('_active')) {
					_rateLi.eq($(this).attr('_hover')).addClass('hover');
				}
				if($(this).attr('_active') && !_rateLi.eq($(this).attr('_active')).hasClass('active')) {
					_rateLi.eq($(this).attr('_active')).addClass('active');
				}
				return false;
			});
			_rateA.hover(function(){
				_showRate.show().text($(this).attr('rel'));
			},function(){
				_showRate.hide();
			});
			_rateA.click(function(){
				var _rell = $(this).attr('rel');
				// vote processing
				markArray  = $(this).text().split('|');
				redirectTo = markArray[0];
				guidType   = markArray[1];
				guidId     = markArray[2];
				mark       = markArray[3];
				if(redirectTo)
				{
					window.location = redirectTo;
				}
				else
				{
					if(_rell) _rate.parent().find('.set-point').text('Ваша оценка: '+_rell);
					_rate.parent().find('a').remove();
					_rate.unbind('mouseenter mouseleave');
					_showRate.hide();
					_rate.removeClass('x-stars-hovered');
					$.get('/ajaxshop/vote/mark/'+guidType+'/'+guidId+'/'+mark+'/', '', function(data)
					{
						if(data)
						{
							newMarkArray = data.split('|');
							mark = starRound(newMarkArray[1]/2);
							$('#currpoint_'+guidType+'_'+guidId).html(mark);
							newMarkClass = 'x-'+Math.floor(mark)+'-'+Math.round(mark);
							$('#x_stars_'+guidType+'_'+guidId)
								.removeClass('x-0-0').removeClass('x-0-1')
								.removeClass('x-1-1').removeClass('x-1-2')
								.removeClass('x-2-2').removeClass('x-2-3')
								.removeClass('x-3-3').removeClass('x-3-4')
								.removeClass('x-4-4').removeClass('x-4-5')
								.removeClass('x-5-5').addClass(newMarkClass);
						}
					});
				}
				// end vote processing
				$(this).blur();
				return false;
			});
		}
	});
}

function starRound(i)
{
	var k = Math.floor(i);
	if (k <= i && i < k+0.25)
		return k;
	if (k+0.25 < i && i < k+0.75)
		return k+0.5;
	return k+1;
}

$(function(){
	initStarsX();
});
