var Voto = { };
 
Voto.Base = Class.create({
	initialize: function(item, url, options) {
		this.options           		= options;
		this.options.votoCorrente 	= options.votoCorrente || 0.0;
		this.options.numVoti 		= options.numVoti || 0;
		this.options.postBody		= options.postBody;
		this.options.observing		= (options.observing == null) ? true : options.observing;
		this.options.traduzioni		= options.traduzioni || new Array();
		this.options.traduzioni.votazione = this.options.traduzioni.votazione || "Votazione: ";
		this.options.traduzioni.numVoti	  = this.options.traduzioni.numVoti || "Voti:";	
					
		this.url					= url;			
					
		this.contenitore 			= item;
		this.indicatore				= null;
		this.votoUtente				= null;
		this.datiVoto				= null;
		this.datiNumVoti			= null;
		this.stelle					= null;
		this.msgVotazione 			= null;
		this.msg					= null;
		
		this.boundedMouseOver 		= this.mouseover.bindAsEventListener(this);
		this.boudedMouseOut			= this.mouseout.bindAsEventListener(this);
		this.render();			
	},
	
	render: function() {
				this.contenitore.update();
	
				this.indicatore = new Element("div", {"class": "indicatore"});
												
				var indice 		= new Element("div", {"class": "indice"}).setStyle({"width": (this.options.votoCorrente*10) + "%"});
				var maschera 	= new Element("div", {"class": "maschera"});
				var voto 		= new Element("div", {"class": "voto"});
				this.stelle 	= new Element("div", {"class": "stelle"});
				
				if (this.options.observing) {
					for (i=1;i<=10;i++) {
						classe = "";
						if (this.options.observing) {
							classe = "left";
							if (i%2 == 0) classe = "right";
						}
													
						var votazione = new Element("a", {"href": "#null", "class": classe, "voto": i});
						votazione.observe("click", this.clickStella.bind(this));
						voto.insert({bottom: votazione});
					} 
				}	
					
				this.indicatore.insert({bottom: indice})
							   .insert({bottom: maschera})
							   .insert({bottom: voto})
							   .insert({bottom: this.stelle});
				
				
				var labelDatiVoto = new Element("strong").update(this.options.traduzioni.votazione);
				var labelNumVoti = new Element("strong").update(this.options.traduzioni.numVoti);
				this.datiVoto = new Element("span").update(this.options.votoCorrente);
				this.datiNumVoti = new Element("span").update(this.options.numVoti);
				
				var dati = new Element("div", {"class": "dati"});
				dati.insert({bottom: labelDatiVoto})
					.insert({bottom: this.datiVoto})
					.insert({bottom: labelNumVoti})
					.insert({bottom: this.datiNumVoti});					
									
				this.votoUtente	= new Element("div", {"class": "voto_corrente"});
				this.msgVotazione = new Element("div", {"class": "msg_votazione"}).update(this.msg);
				
				this.contenitore.insert({bottom: this.indicatore})
								.insert({bottom: dati})
								.insert({bottom: this.votoUtente})
								.insert({bottom: this.msgVotazione});
								
				if (this.options.observing) {
					Event.observe(this.indicatore, "mouseover", this.boundedMouseOver);
					Event.observe(this.indicatore, "mouseout", this.boudedMouseOut);
				}
			},
	
	mouseover: function(event) {
					var element = event.element();
					var voto = parseInt(element.readAttribute("voto"));
					
					if (voto > 0) {
						var numStelleIntere = parseInt(voto / 2);
						var clear = new Element("div", {"class": "clear"});
							
						this.stelle.update();
						
						for (i=0;i<numStelleIntere;i++) {
							var stella = new Element("div", {"class": "stella"});
							this.stelle.insert({bottom: stella});
						}
						this.stelle.insert({bottom: clear});
						
						this.votoUtente.update(voto + " / 10");
					}
				},
	
	mouseout: function(event) {
				this.stelle.update();
				this.votoUtente.update();	
			  },
				
	clickStella: function(event) {
						var element = event.element();
						var voto = parseInt(element.readAttribute("voto"));
							
						if (voto > 0) {
							new Ajax.Request(this.url, {
										method: 'post',
										evalJSON: true,
										postBody:  (this.options.postBody == null ? "" : this.options.postBody + "&") + "voto=" + voto,
										onCreate: function() {
											this.options.observing = false;
											this.render();
										}.bind(this),
										onSuccess: function(response) {
											var risposta = response.responseJSON ;
											this.options.votoCorrente = risposta.votoCorrente;
											this.options.numVoti = risposta.numVoti;
											
											if (risposta.observing != null) {
												this.options.observing = risposta.observing;
											}
											
											if (risposta.msg != null) {
												this.msg = risposta.msg;
											}
											
											Event.stopObserving(this.indicatore, "mouseover", this.boundedMouseOver);
											Event.stopObserving(this.indicatore, "mouseout", this.boundedMouseOut);
											this.stelle.update();
											this.render();											
										}.bind(this)										
							});
						}
				 }			
});

