var ratings = new Array;

function votio_getHTTPObject()
{
	var xmlhttp; 
	if (!xmlhttp && typeof XMLHttpRequest != "undefined")
	{ 
		try
		{ 
			xmlhttp = new XMLHttpRequest(); 
		}
		catch (e)
		{ 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp; 
} 

var votio_http = votio_getHTTPObject();

function votio_handleHttpResponse()
{
	if(votio_http.readyState == 4)
	{
		var s = votio_http.responseText;
		var res = s.split(",");
		var id = res[0];
		var index = res[1];
		var message = res[2].replace(/\./,",");
		var textmsg = document.createTextNode(message);
		var votediv = $('votediv');
		votediv.removeChild(votediv.firstChild);
		votediv.appendChild(textmsg);
		
		var halfset = false;
		var rating = String(index).split(".");
				
		for(z=1;z<=5;z++)
		{
			i = $("star_"+id+"_"+z);
			//im = (z <= index) ? "star1.gif" : "star0.gif";
			
			if (rating[0] >= z)
			{			
				im = "star1.gif";
			}
			else
			{
				im = "star0.gif";
				
				if (!halfset && rating[1] > 0)
				{
					im = (String(rating[1]).substr(0,1) >= 5) ? "star1.gif" : "half.gif";
					halfset = true;
				}
			}
			
			
			usei = PATH_IMG+im;
			i.setAttribute("src", usei);
			i.setAttribute("onmouseover", null);
			i.setAttribute("onmouseout", null);
		}
	}
}

function votio_vote(id,index)
{
	votio_http.open("GET",PATH_HTTP+"rating.php?id="+id+"&rt="+index,true);
	votio_http.onreadystatechange = votio_handleHttpResponse;
	votio_http.send(null);
}

function votio_hover(id,index,rating)
{
	var i;
	ratings[id] = rating;
	for (z=1; z<=5; z++)
	{
		i = $(id+"_"+z);
		im = (z <= index) ? "starvs.gif" : "starv.gif";
		usei = PATH_IMG+im;
		i.setAttribute("src",usei);
	}
}

function votio_unhover(id,index)
{
	var halfset = false;
	var rating = String(ratings[id]).split(".");
	var i;
	
	for (z=1; z<=5; z++)
	{
		i = $(id+"_"+z);
		
		if (rating[0] >= z)
		{			
			im = "star1.gif";
		}
		else
		{
			im = "star0.gif";
			
			if (!halfset && rating[1] > 0)
			{
				im = (String(rating[1]).substr(0,1) >= 5) ? "star1.gif" : "half.gif";
				halfset = true;
			}
		}
		usei = PATH_IMG+im;
		i.setAttribute("src", usei);
	}
}

function checkbox(_this,n)
{
	var src = _this.src;
	
	if (src.indexOf("off.gif") != -1)
	{
		src = src.replace(/off\.gif/,"on.gif");
		$("cb_"+n).value = 1;
	}
	else
	{
		src = src.replace(/on\.gif/,"off.gif");
		$("cb_"+n).value = 0;
	}
	_this.src = src;
}

function _initCheckbox(n,ve)
{
	for (i=1; i<=n; i++)
	{
		if ($("cb_"+i).value == ve)
		{
			$("ci_"+i).src = $("ci_"+i).src.replace(/off\.gif/,"on.gif");
		}
	}
}
