/*
 * Anthony DON - 27/11/2007
 */
var Ratings = Class.create({
  	initialize: function(instance, root_id, value, max, observer_id) {
  		this.instance = instance;	
  		this.root_id = root_id;
  		this.value = value;
  		this.max = max;
  		this.max_width = 69; /* pixels */
  		this.observer_id = observer_id;
  		this.old_value = value;
  		this.display();
	},
	compute_width: function() {
	    /* undefined */
		if(this.value == -1)
			return 0;
			
		var result = (this.value / this.max) * this.max_width;
		return result;
	},
	display: function() {
		var tpl = new Template('<div id="#{instance}" class="small_avg_rating" title="#{title}" onmouseover="#{instance}.mouse_over(event);" onmouseout="#{instance}.mouse_out();" onclick="#{instance}.on_click(event);"><div class="small_stars" style="width: #{width}px ! important;"/>');
		var o = new Object();
		o.instance = this.instance;
		o.title = "Note : " + this.value + "/" + this.max;
		o.width = this.compute_width();
		$(this.root_id).update(tpl.evaluate(o));
	},
	get_value: function() {
		return this.value;
	},
	set_value: function(v) {
		if(v <= this.max) {
			if($(this.observer_id))
				$(this.observer_id).value = v;
			this.value = v;
			this.display();
		}
	} ,
	mouse_over: function(event) {

	},
	mouse_out: function() {

	},
	on_click: function(event) {
		dom_obj = $(this.instance); 
		x0 = Position.page(dom_obj)[0];	// position x dans le même référentiel que la position de la souris
		x = event.clientX;
		var value = ((x - x0) * this.max) / this.max_width;
					
		this.set_value(Math.ceil(value));
	}
});


function display_rating(value, max)
{
	var tpl = new Template('<div class="small_avg_rating" title="#{title}"><div class="small_stars" style="width: #{width}px ! important;"></div></div>');
	var o = new Object();
	
	if(value == -1)
	{
		o.title = "Pas de notation";
		o.width = 0;
	} else {
		o.title = "Note : " + value + "/" + max;
		o.width = (value / max) * 69; 
	}
	return tpl.evaluate(o);
}

