var ajaxVote = new SAjax();

function vote(votetype, objectid, vote)
{
	var status_el = getEl("status_" + objectid);    
	status_el.innerHTML = "Voting...";

	ajaxVote.votetype = votetype;    
    ajaxVote.objectid = objectid;    
    var url = "vote_action.asp?action=vote&objectid=" + objectid + "&type=" + votetype + "&vote=" + vote;
	ajaxVote.makeRequest(url, true, voteHandler, true);
}
function voteHandler() 
{
    if (ajaxVote.isReady()) {
	    var status_el = getEl("status_" + ajaxVote.objectid);
	    
        if (ajaxVote.isGood())
        {
			var xmlDoc = ajaxVote.getXml();
			var errors = xmlDoc.documentElement.getElementsByTagName("error");
			if (errors.length > 0)
			{
				status_el.innerHTML = show_status(errors[0].getAttribute("text"), STATUS_ERROR);
			}
			else
			{
				var items = xmlDoc.documentElement.getElementsByTagName("item");
				if (items.length > 0)
				{
					var rating = parseFloat(items[0].getAttribute("rating"));
					var votes = parseInt(items[0].getAttribute("votes"));
					
					var rating_el = getEl("rating_" + ajaxVote.objectid);
					rating_el.innerHTML = getRating(ajaxVote.votetype, ajaxVote.objectid, rating, votes);
				}
			}
		}
        else
			status_el.innerHTML = ajaxVote.getErrorMessage();
    }
}

function getRatingOuter(votetype, objectid, rating, votes)
{
	var html = "<div id='rating_" + objectid + "'>";
	html += getRating(votetype, objectid, rating, votes);
	html += "</div>";
	return html;
}

function getRating(votetype, objectid, rating, votes)
{
	var html = "";
	var i;
	
	if (isNaN(rating))
	{
		for (i = 0; i < 5; i++)
		{
			html += "<img src='images/star_empty.gif'>";
		}
	}
	else
	{
		var whole = Math.floor(rating);
		var remainder = rating - whole;
		for (i = 0; i < whole; i++)
		{
			html += "<img src='images/star_full.gif'>";
		}
		if (whole < 5)
		{
			if (remainder < .25)
				html += "<img src='images/star_empty.gif'>";				
			else if (remainder < .5)
				html += "<img src='images/star_qtr.gif'>";
			else if (remainder < .75)
				html += "<img src='images/star_half.gif'>";
			else
				html += "<img src='images/star_tqtr.gif'>";
			
			for (i = whole; i < 5; i++)
			{
				html += "<img src='images/star_empty.gif'>";
			}
		}
		html += " " + rating.toFixed(2) + "<br><span style='color: darkgray;'>(" + votes + " votes)</span>";
	}
	
	html += "<br><span style='font-size: 7pt;'>Vote:";
	for (i = 1; i < 6; i++)
	{
		html += "<a href='javascript:vote(" + votetype + "," + objectid + "," + i + ");'>" + i + "</a>";
	}
	html += "</span>";
	html += "<div style='font-size: 7pt;' id='status_" + objectid + "'></div>";
	
	return html;
}
