var NOTATIONS_COUNT = 5;
var NOTATION_PREFIX = "notationImg";

var notationFlags = new Array();
var userDefined = false;
var currentNote = -1;
var userNote = -1;

function loadNotation() {
	if (document.getElementById(NOTATION_PREFIX + "1") == undefined) {
		setTimeout("loadNotation()", 200);
	} else {
		var img;
		var i;
		for (i = 1; i <= NOTATIONS_COUNT; i++) {
			notationFlags[i - 1] = false;
			img	= document.getElementById(NOTATION_PREFIX + i);
			img.alt	= 'Donner la note de ' + i + '/' + NOTATIONS_COUNT;
			img.title = 'Donner la note de ' + i + '/' + NOTATIONS_COUNT;
			img.onclick	= function() {notationNote(this);};
			if (userDefined) {
				img.onmouseover	= function() {notationOver(this);};
				img.onmouseout = function() {notationOut(this);};
			}
		}
	}
}

function notationNote(image) {
	if (!userDefined) {
		if (confirm("Vous devez vous identifier pour donner une note.")) {
			executeLoggedAction(false, "loginForm");
		}
	} else {
		var index = getNotationIndex(image);
		var updateNote = false;
		if (userNote > 0) {
			if (index == userNote) {
				alert("Vous avez déjà donné la note de " + userNote + ".");
			} else {
				updateNote = confirm("Vous allez remplacer votre précédente note de " + userNote + " par " + index + ".");
			}
		} else {
			updateNote = true;
		}
		if (updateNote) {
			currentNote = index;
			document.forms["notationForm"].elements["newNote"].value = currentNote;
			document.forms["notationForm"].submit();
		}
	}
}

function notationOver(image) {
	var limit = getNotationIndex(image);
	notationFlags[limit - 1] = true;
	var i;
	for (i = 1; i <= NOTATIONS_COUNT; i++) {
		document.getElementById(NOTATION_PREFIX + i).className = "notation_" + (i <= limit ? "on" : "off");
	}
}

function notationOut(image) {
	var limit = getNotationIndex(image);
	notationFlags[limit - 1] = false;
	var i;
	limit = (inOnNotation() ? limit : currentNote);
	for (i = 1; i <= NOTATIONS_COUNT; i++) {
		document.getElementById(NOTATION_PREFIX + i).className = "notation_" + (i <= limit ? "on" : "off");
	}
}

function inOnNotation() {
	var i;
	for (i = 0; i < NOTATIONS_COUNT; i++) {
		if (notationFlags[i]) {
			return true;
		}
	}
	return false;
}

function getNotationIndex(image) {
	return parseInt(image.id.substring(NOTATION_PREFIX.length));
}