starRating = Class.create();

starRating.prototype = {
	initialize: function(elm, init, star_height) {
		if (typeof init != 'undefined') { 
			this.actual_note = init 
		} else {
			this.actual_note = 0;
		}
		if (typeof star_height != 'undefined') {
			this.star_height = star_height
		} else {
			this.star_height = 20;
		}
		this.elm = elm;
		this.name = elm.id;
		this.all_stars = this.elm.getElementsByTagName('a');
		
		for (i=0; i < this.all_stars.length; i++)
		 {
		 	 this.all_stars[i].onmouseover = this.getAndShowNote.bindAsEventListener(this);
		 	 this.all_stars[i].onclick = this.getAndSetNote.bindAsEventListener(this);
		 }
		 this.elm.onmouseout = this.showInitialState.bindAsEventListener(this);
	},
	showNote: function(note) {
		 var note = Math.round(note);
		 for (i=0; i < this.all_stars.length; i++)
		 {
		 	if (i < note) { 
		 		this.all_stars[i].style.backgroundPosition = 'left ' + (-note*this.star_height) + 'px';
		 	} else { 
		 		this.all_stars[i].style.backgroundPosition = 'left top';
		 	}
		 }
	}, 
	getNote: function(elm) {
		if (elm.srcElement) {
			return elm.srcElement.firstChild.nodeValue;
		} else {
			return elm.target.firstChild.nodeValue;
		}
	},
	getAndShowNote: function(elm) {
		this.showNote(this.getNote(elm));
	},
	showInitialState: function() {
		this.showNote(this.actual_note);
	},
	setNote: function(note) {
		this.actual_note = note;
	},
	getAndSetNote: function(elm) {
		this.setNote(this.getNote(elm));
		this.setHiddenInput(this.actual_note);
		if (elm.srcElement) {
			elm.srcElement.blur();
		} else {
			elm.target.blur();
		}
		return false;
	},
	setHiddenInput: function(note) {
		var parent = this.elm.parentNode;
		if (parent.className == 'notation-box') {
			parent.getElementsByTagName('input')[0].value = note;
		}
	} 
}


 Event.observe(window,'load', function() { 
 	$$('.notation-box').each(function(box){ 
 		var elm = box.getElementsByTagName('ul')[0];
 		new starRating(elm);
 	});
 });
 
 function validComment() {
 	avis_positif = document.getElementById('comment_plus').value;
 	avis_negatif = document.getElementById('comment_minus').value;
 	
 	note1 = document.getElementById('js-note[1]').value;
 	note2 = document.getElementById('js-note[2]').value;
 	note3 = document.getElementById('js-note[3]').value;
 	note4 = document.getElementById('js-note[4]').value;
 	
 	if ((avis_positif!="")||(avis_negatif!="")) {
 		if (((note1!="") && (note2!="")) && ((note3!="") && (note4!=""))) {
 			this.form.submit();
 		} else {
 			message = '- Vous n\'avez pas rempli toute la partie notation\r\n';
 			window.alert(message);
 		}
 	} else {
 		message = '- Vous devez remplir au moins un des deux champs de commentaire\r\n';
 		window.alert(message);
 	}
 }

/* ]]> */