function vote_for_content(obj, path, uid, mark)
{
  jQuery.ajax({
           type: "POST",
		   url: noderating_link,
		   data: "ajax=1&path="+path+"&uid="+uid+"&mark="+mark+'&action=rate',
		   success: function(res){
		     if (res)
			 {
			   eval(res);
			   obj.find(".rating_overall").html(res.total_mark);
			   obj.find(".rating_count").html(res.count).parent().css('display','block');
			 }
		   }
		  });
}

function get_for_content(path, obj, canedit, starsize, id)
{
  jQuery.ajax({
           type: "POST",
		   url: noderating_link,
		   data: "ajax=1&path="+path+'&action=get&canedit='+canedit+starsize+'&id='+id,
		   success: function(res){
		     if (res)
			 {
			   var res = res.replace(/\n/g, " ");
			   obj.html(res);
			 }
		   }
		  });
}

function get_vote_from_content()
{
  jQuery('.noderating_all').each(function(index){
    var obj = jQuery(this);
	var path = obj.attr('path');
	var starsize = '';
	if (obj.attr('starsize'))
	{
	  starsize = "&starsize="+obj.attr('starsize');
	}
	var canedit;
	if (obj.attr('canedit')== 1)
	{
	  canedit = 1;
	}
	else
	{
	  canedit = 0;
	}
	if (jQuery(this).attr('type') != 'loaded')
	{
	  jQuery(this).attr('type', 'loaded');
	  var id = 'noderating_'+index;
	  jQuery(this).attr('id', id);
	  get_for_content(path, obj, canedit, starsize, id);
	}	
  });
}

/*Функция для отображения звезд и привязки к ним действий мышкой
 * cl - Класс, в который мы будем выводить звезды
 * module_path - путь к модулю
 * height - высота звезды
 * user_mark - начальная оценка пользователя
 * max_count - максимально-возможная оценка
 * noderating_link - ссылка на страницу по аяксу для записи оценки
 * path - путь до оцениваемого объекта
 * uid - идентификатор оценивающего пользователя
*/
current_mark = new Array();

function change_stars(obj, mark, a_star, na_star)
{
  obj.children().each(function(index){
   if (index<mark) jQuery(this).attr("src",a_star);
   else jQuery(this).attr("src",na_star);
  });
}

function showstars(cl, module_path, height, user_mark, max_count, path, uid, canedit)
{
  var a_star = module_path+'/img/star.png';
  var na_star = module_path+'/img/star_na.png';
    var newitem = "<div class='noderating_stars'>";
    if (!current_mark[path]){current_mark[path] = user_mark;}
	
    for (var i=0;i<max_count;i++)
    {
      if (i<user_mark)
	    newitem += "<img class='noderating_star' src='"+a_star+"' alt='"+(i+1)+"' title='"+(i+1)+"' height='"+height+"px' style='cursor:pointer'>";
	  else
  	    newitem += "<img class='noderating_star' src='"+na_star+"' alt='"+(i+1)+"' title='"+(i+1)+"' height='"+height+"px' style='cursor:pointer'>";
    }
    newitem += "<\/div>"
    jQuery(cl).html(newitem);
	//jQuery(cl).parent().before('<div class="clear"><\/div>');
  if (canedit)
  {  
    jQuery(cl).bind("mouseleave",function(){
	    change_stars(jQuery(this).children(), current_mark[path], a_star, na_star);
	});
    jQuery(cl).children().children().each(function(index){
      if (index<user_mark) jQuery(this).attr("src",a_star);
	  else jQuery(this).attr("src",na_star);
	  jQuery(this).bind({
	    mouseover: function(){
	      change_stars(jQuery(this).parent(), index+1, a_star, na_star);
	    },
	    click: function(){
	      vote_for_content(jQuery(cl).parent(),path,uid,index+1);
		  current_mark[path] = index+1;
	    }
	  });
    });
  }
  else
  {
    jQuery(cl).find('img').css('cursor', 'default');
  }
  
}

jQuery(document).ready(function(){
  setInterval(function(){get_vote_from_content()},100);
});;

