// Variables //

	var ajax = null;
	var firstTime = false;

// Events //

	window.onload = function() {
		ajax = new AJAX();
	}

// Classes //

	function AJAX() {
		this.connection = null;
		this.queue = new Array();
	}
	
	AJAX.prototype.createRequest = function() {
		if (window.XMLHttpRequest) {
			this.connection = new XMLHttpRequest();
			return true;
		}
		return false;
	}
	
	AJAX.prototype.request = function(data) {
		if (this.connection == null) {
			if (this.createRequest()) {
				var self = this;
				this.connection.onreadystatechange = function() {
					if (self.connection.readyState == 4) {
						if (self.connection.responseText != null && self.connection.responseText != "") {
							var rating = parseInt(self.connection.responseText);
							if (rating > 0) {
								for (var i = 1; i <= 5; i++)
									document.getElementById("rating" + i).src = "images/rating_" + ((i <= rating) ? "on.png" : "none.gif");
								if (firstTime)
									document.getElementById("votes").innerHTML = (parseInt(document.getElementById("votes").innerHTML) + 1);
							}
						}
						else
							alert("AJAX error: no data returned.");
						self.connection = null;
						if (self.queue.length > 0)
							self.request(self.queue.splice(0, 1));
					}
				}
				this.connection.open("POST", "rating.php");
				this.connection.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.connection.send(data);
			}
			else
				alert("Your browser does not support AJAX requests.");
		}
		else
			this.queue[this.queue.length] = data;
	}

// Functions //

	function rateMe(torrent, rating) {
		for (var i = 1; i <= 5; i++)
			document.getElementById("my_rating" + i).src = "images/rating_" + ((i <= rating) ? "on" : "off") + ".png";
		if (ajax == null)
			ajax = new AJAX();
		ajax.request("torrent=" + torrent + "&rating=" + rating);
		firstTime = (currentRating == 0);
		currentRating = rating;
	}
	
	function rateHover(rating, over) {
		var rating = over ? rating : currentRating;
		for (var i = 1; i <= 5; i++)
			document.getElementById("my_rating" + i).src = "images/rating_" + ((i <= rating) ? (over ? "hover" : "on") : "off") + ".png";
	}