/**************************************************************
 * Рейтинг (с) WebInside.RU
 **************************************************************/
 
var width_star = 30; // Ширина рисунка звездочки





function Rating_AJAX(id, stars, enable)
	{
	request_ajax_r(id, "rating.php", "action=view&obj=" + id);	
	write_box(id, stars, enable);
	
		
	}
function Votes(id, stars, i)
	{
	$(id).innerHTML = "";
	request_ajax_r(id, "rating.php", "action=setval&obj=" + id + "&rate=" + i);
	}	

function write_box(id, stars, enable)
	{
	var w = stars * width_star;
	html = '<div style="border: 1px solid #CCC; width: ' + w + 'px; padding: 10px;">';
	html += '<div style="border: 0px solid #000; width: ' + w + 'px; height: ' + width_star + 'px; background: url(img/star_off.gif) left top repeat-x">';
	html += '<div id="' + id + '" style="border: 0px solid #CCC; width: 1px; height: ' + width_star + 'px; background: url(img/star_on.gif)">';
	
	if (enable==1)
		{
		html += '<table border=0 cellpadding=0 cellspacing=0 style="position: absolute;"><tr>';
		for (i=1; i <= stars; i++)
		html += '<td style="padding: 0; margin: 0"><img style="cursor: pointer;" border=0  id="' + id + "__" + i + '" src="img/button_rat.gif" width="'+width_star+'" height="'+width_star+'" onMouseOver="over_star(\'' + id + '\', \'' + i + '\')"; onMouseOut="out_star(\'' + id + '\', \'' + i + '\')"; onClick="Votes(\''+id+'\', \''+stars+'\', \''+i+'\')";></td>';
		html += '</tr></table>';
		}	
	html += '</div></div>';
	html += '<div id="'+id+'_text" style="font-size: 11px; font-family: Arial">---</div>';
	html += '</div>';
	document.write(html);
	}	

function over_star(id, n)
	{
	for (i=1; i<=n; i++)	
	$(id + "__" + i).src = "img/star_over.gif";	
	}	

function out_star(id, n)
	{
	for (i=1; i<=n; i++)		
	$(id + "__" + i).src = "img/button_rat.gif";	
	}	
	
function getAnswer(id, num)
{
//$(id).innerHTML = num;	
animate(id, num, 0);	
}	

function animate(id, num, cur)
{
if (cur<(num*width_star))
	{
	$(id).style.width = cur+"px";	
	cur += 2;
	var t = setTimeout(function(){animate(id, num, cur)}, 1);	
	
	}
	else
	{
	clearTimeout(t);
	//alert(id);	
	}	
}

/*********AJAX**********************************************************/
function request_ajax_r(id, script, options) 
 		{  
 		var request_r;	
 		if (window.XMLHttpRequest) 
 				{  
 				request_r = new XMLHttpRequest();  
 				request_r.onreadystatechange = function() {processRequestChange_r(id, request_r);};  
 				request_r.open("GET", script + "?" + options, true);  
 				request_r.send(null);  
 				} 
 				else if (window.ActiveXObject) 
 				{  
 				request_r = new ActiveXObject("Microsoft.XMLHTTP");  
 				if (request_r) 
 						{  
 						request_r.onreadystatechange = function() {processRequestChange_r(id, request_r);};  
 						request_r.open("GET", script + "?" + options, true);  
 						request_r.send();  
 						}  
 				}  
 		}
 		
 		
 		

function processRequestChange_r(id, request_r) 
		{ 
		var the_object;	
		//abortRequest = window.setTimeout("request.abort();", 10000);  
		if (request_r.readyState == 4) 
				{  
				//clearTimeout(abortRequest);  
				// если успешно  
				if (request_r.status == 200) 
						{
						the_object = eval( "(" + request_r.responseText + ")" );
						if (the_object["old"]==1) $(id).innerHTML = "";	
						getAnswer(id, the_object.val);	
						$(id+"_text").innerHTML = "Проголосовало: " + parseInt(the_object["votes"]) + "<br>Рейтинг: " + the_object.val;
						} 
						else 
						{  
						alert("Не удалось получить данные:\n" + request_r.statusText);  
						}  
				}  
		// иначе, если идет загрузка или в процессе - показываем слой "Загружаются данные"  
			else if (request_r.readyState == 3 || request_r.readyState == 1) 
				{  
				//$(id).innerHTML = "Загрузка...";
				}  
		} 		
	
