/**
голосует и в блоки sid,mid записывает возвращенные количество голосов и оценку
*/
var currentMark=Array();

function markIt(guidType,guidId,mark,sid,mid)
{
  //$('#off'+guidId+'_' + mark).attr('src','/images/icon/rating/'+(isset('small')?'small/':'')+'star_down.gif');
  $('#off'+guidId+'_' + mark).attr('src','/images/icon/rating/small/star_down.gif');
  $.get("/ajax/vote/mark/"+guidType+"/"+guidId+"/"+mark+"/",'',
  function(data)
  {
     if (data)
     {
        arr = data.split('|');
     	$(sid).html(arr[0]);
     	$(mid).html(arr[1]);
     	
     	currentMark[guidId] = star_round(arr[1]);
     	MarkOut2(guidId);
     }  
  });
}

function MarkOver2(mark,guidId)
{
	for (i=1; i<=10; i++) 
	{
		if (i>mark)
		  $('#off'+guidId+'_' + i).attr('src','/images/icon/rating/small/'+'star_off.gif');
		else
		  $('#off'+guidId+'_' + i).attr('src','/images/icon/rating/small/'+'star_on.gif');
	}
}

function MarkOut2(guidId)
{
	for (i=1; i<=10; i++) 
	{
		if (i>currentMark[guidId])
		{
		  if ((-0.5+i) > currentMark[guidId])
		    //$('#off'+guidId+'_' + i).attr('src','/images/icon/rating/'+(isset('small')?'small/':'')+'star_off.gif');
		    $('#off'+guidId+'_' + i).attr('src','/images/icon/rating/small/'+'star_off.gif');
		  else 
		    $('#off'+guidId+'_' + i).attr('src','/images/icon/rating/small/'+'star_half.gif');
		}  
		else
		  $('#off'+guidId+'_' + i).attr('src','/images/icon/rating/small/'+'star_on.gif');
	}
}


function isset( mixed_var ) {    // Determine whether a variable is set
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
    //хрень написал этот ваш Kevin
    //рефакторить!!!
 
    var i = 0, argc = arguments.length, argv = arguments, set=true;
 
    for (i = 0; i< argc; i++){
        if( argv[i] === undefined ){
            set = false;
            break;
        }
    }
 
    return set;
}

function star_round(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;
}

// 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');
			var _showRate = _rate.parent().find('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(){
	initStarsX();
});
