/**********************************************
 * Copyright (c) 2005-2006 Agora Games
 * All rights reserved
 **********************************************/ 
function elt(id)
{
	return document.getElementById(id);
}

function markVoting(id, vote)
{
	for(var i = 1; i <= 5; ++i) {
		var elem = elt("customartrating_" + id + "_" + i);
		var img;
		
		if (i > vote)
			img = "off.gif";
		else
			img = "hover.gif";
			
		elem.src = "http://ds.downhill-jam.com/images/" + img;
	}
	return true;
}
function restore(id, value)
{
	for(var i = 1; i <= 5; ++i) {
		var elem = elt("customartrating_" + id + "_" + i);
		var gap = i - value;
		var img;

		// at 4.8, round up to 5.0
		// the .001 is to deal with floating point issues
		if (gap < 0.2001)
			img = "on.gif";
		// at 4.3, round up to 4.5
		else if (gap < 0.7001)
			img = "half.gif";
		// sorry no luck
		else
			img = "off.gif";
		
		elem.src = "http://ds.downhill-jam.com/images/" + img;
	}
}

function goVote(id, value, vote)
{
	// disable re-voting
	for(var i = 1; i <= 5; ++i) {
		var elem = elt("customartrating_" + id + "_" + i);
		elem.onclick = "";
		elem.onmouseover = "";
		elem.onmouseout = "";
		elem.style.cursor = "default";
		if (elem.removeAttribute) {
			elem.removeAttribute("onclick");
			elem.removeAttribute("onmouseover");
			elem.removeAttribute("onmouseout");
		}
		elem.style.cursor = "default";
	}
	
	// set it to the value we have right now
	restore(id, value);

	// tell the server our vote
	informServer(id, vote);
}

function informServer(id, vote)
{
	// global - hmm, what do we do if there is already a request in progress?
	g_HTTPRequest = false;
	
	if (window.XMLHttpRequest) {
		g_HTTPRequest = new XMLHttpRequest();
		if (g_HTTPRequest.overrideMimeType)
			g_HTTPRequest.overrideMimeType("text/xml");
	}
	else if (window.ActiveXObject) {
		try {
			g_HTTPRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				g_HTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}

	// fail back: submit a regular request
	if (!g_HTTPRequest) {
		var url = "http://" + location.hostname + "/custom_art/rate_image/" + id + "/?value=" + vote;
		window.location = url;
		return false;
	}
	
	var url = "http://" + location.hostname + "/custom_art/rate_ajax/" + id + "/?value=" + vote;
	g_HTTPRequest.onreadystatechange = this.serverResponse;
	g_HTTPRequest.open('GET', url, true);
	g_HTTPRequest.send(null);
}

function serverResponse()
{
	if (g_HTTPRequest.readyState == 4 && g_HTTPRequest.status == 200) {
		//alert(g_HTTPRequest.responseText);
		var response = g_HTTPRequest.responseText.split(" ");			
		var id = response[0];
		var rating = response[1];
		var number_votes = response[2];
		
		// 0 = error
		if (id != 0) {
			// new rating
			restore(id, rating);
			
			// update text
			elt("customartrating_" + id + "_numvotes").innerHTML = 
				number_votes + (number_votes == 1 ? " vote" : " votes");
		}
	}
	else if (g_HTTPRequest.readyState == 4) {
		// error :( should I redirect to non-ajax URL?
	}
}