/*******************************************************
 *
 *	Singleton Rating
 *		Contains list of rating objects (R).
 *		Each rating object has an index value in the list
 *		Given index value, this singleton simply forwards
 *		the call to the actual rating object.
 *
 *******************************************************/
var Rating = {
	'list': new Array(), 
	
	'add': function(ratingObject) {
		var index = Rating.list.length;
		
		// List of rating objects (R)
		Rating.list[index] = ratingObject;
		ratingObject.index = index;
	},
	
	'display': function() {
		for (var i=0; i<Rating.list.length; i++) {
			Rating.list[i].setStars();
			
			Rating.list[i].currentRating();
		}
	}, 
	
	'markStars': function(index, stars) {
		Rating.list[index].markStars(stars);
	}, 
	
	'rate': function(index, stars) {
		Rating.list[index].rate(stars);
	}, 
	
	'currentRating': function(index) {
		Rating.list[index].currentRating();
	}
}




/*******************************************************
 *
 *	Rating class
 *
 *******************************************************/
function R(divID, rating, starNumber, imgDir, ratingURL) {
	this.divID = divID; 
	this.rating = rating;
	this.starNumber = starNumber;
	this.imgDir = imgDir;
	this.postURL = ratingURL;
	
	this.statusName = ['Dårligt spil', 'I mangel på bedre spil', 'Godt spil', 'Meget populært spil', 'Stærkt vanedannende spil!'];
	
	this.onlineGameID; // Set by TopGames.updateGameContent
	this.index; // Set by Rating.add()
	this.stars = new Array();
	
	this.messageBox;
}

R.prototype.setStars = function() {
	// Get the container to append images into
	var div = document.getElementById(this.divID);
	
	// Create images of white stars iteritively and append into the container
	var img;
	for (var i=0; i<this.starNumber; i++) {
		// Create new image of white star
		img = document.createElement('img');
		img.src = this.imgDir + '/starGrey.png';
		img.style.width = 25;
		img.style.height = 25;
		img.style.cursor = 'pointer';
		img.title = this.statusName[i];
		
		img.ratingID = this.index; // Identify the index number where this R object is placed within Rating.list[]
		img.starNumber = (i + 1);
		
		// Attach mouseover event listener to the image object
		img.onmouseover = function() {
			Rating.markStars(this.ratingID, this.starNumber);
		};
		
		// Attach mouseout event listener to the image object
		img.onmouseout = function() {
			Rating.currentRating(this.ratingID);
		}
		
		// Attach click event listener to the image object
		img.onclick = function() {
			Rating.rate(this.ratingID, this.starNumber);
		}
				
		// Append into container
		div.appendChild(img);
		
		// Hold a reference of the star
		this.stars.push(img);
	}
}

R.prototype.reset = function() {
	for (var i=0; i<this.starNumber; i++) {
		this.stars[i].src = this.imgDir + '/starGrey.png';
	}
}

R.prototype.currentRating = function() {
	this.reset(); 
	
	for (var i=0; i<Math.round(this.rating); i++) {
		this.stars[i].src = this.imgDir + '/starYellow.png';
	}
}

R.prototype.markStars = function(starNumber) {
	this.currentRating();
	
	// Color the stars blue
	for (var i=0; i<starNumber; i++) {
		this.stars[i].src = this.imgDir + '/starBlue.png';
	}
}

R.prototype.setMessageBox = function(messageBox) {
	this.messageBox = messageBox;
}

// Making AJAX call to this.postURL
R.prototype.rate = function(rating) {
	// Ajax is included from level1.php as prototype.x.x.x.js
	new Ajax.Request(this.postURL,
	{
		method:'post', 
		parameters: {'index': this.index, 'id': this.onlineGameID, 'value': rating}, 
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();

			var messageBox = Rating.list[json.index].messageBox;
			if (messageBox) {
				messageBox.setTitle('Rating resultat');
				messageBox.setContent(json.message);
				messageBox.display();
			}
			else {
				window.alert(response);
			}
		},
		onFailure: function() {
			alert('Something went wrong...');
		}
	});
}

