// classe principale attivata al load della pagina
var stelleVotoPost =
{

	_srcRossa : '/images/stella-rossa.gif',
	_srcGrigia : '/images/stella-grigia.gif',
	
	init: function()
	{
		var stelle = document.getElementsByClassName("stella");
		
		for (var i = 0; i < stelle.length; i++)
    	{
			Core.addEventListener(stelle[i], "mouseover", stelleVotoPost.attivaStelleListener);
			Core.addEventListener(stelle[i], "focus", stelleVotoPost.attivaStelleListener);
			Core.addEventListener(stelle[i], "mouseout", stelleVotoPost.resettaStelleListener);
			Core.addEventListener(stelle[i], "blur", stelleVotoPost.resettaStelleListener);
			
			// assegno valutazione
			if ( i == 0 ) { 
				stelle[i]._valutazione = 'Scarso';
				stelle[i]._votoNumerico = 2;
			} else if ( i == 1 ) {
				stelle[i]._valutazione = 'Niente di speciale';
				stelle[i]._votoNumerico = 4;
			} else if ( i == 2 ) {
				stelle[i]._valutazione = 'Degno di nota';
				stelle[i]._votoNumerico = 6;
			} else if ( i == 3 ) {
				stelle[i]._valutazione = 'Bello';
				stelle[i]._votoNumerico = 8;
			} else {
				stelle[i]._valutazione = 'Strepitoso!';
				stelle[i]._votoNumerico = 10;
			}

			Core.addEventListener(stelle[i], "mouseover", stelleVotoPost.visualizzaValutazioneListener);
			Core.addEventListener(stelle[i], "focus", stelleVotoPost.visualizzaValutazioneListener);
			Core.addEventListener(stelle[i], "mouseout", stelleVotoPost.nascondiValutazioneListener);
			Core.addEventListener(stelle[i], "blur", stelleVotoPost.nascondiValutazioneListener);

			Core.addEventListener(stelle[i], "click", stelleVotoPost.inoltraVotoListener);
			
    	}
	},
	
	/* metodi per accensione stelle al passaggio del mouse */
	attivaStella: function(stella) {
		stelleVotoPost.resettaStella(stella);

		stella._src = stella.getAttribute("src");
		stella.setAttribute("src", stelleVotoPost._srcRossa);
	},

	resettaStella: function(stella) {
		if (stella._src) {
			stella.setAttribute("src", stella._src);
			stella._src = null;
		}
	},

	spegniStella: function(stella) {
		stelleVotoPost.resettaStella(stella);

		stella._src = stella.getAttribute("src");
		stella.setAttribute("src", stelleVotoPost._srcGrigia);
	},

	
	attivaStelleListener: function(event) {
		stelleVotoPost.attivaStella(this);
		
		var node = this;

		while(node.previousSibling != null) {
			if (node.previousSibling.nodeType == 1) {
				node = node.previousSibling;
				stelleVotoPost.attivaStella(node);
			} else {
				node = node.previousSibling;
			}
		}
		
		var node = this;
		
		while(node.nextSibling != null) {
			if (node.nextSibling.nodeType == 1) {
				node = node.nextSibling;
				stelleVotoPost.spegniStella(node);
			} else {
				node = node.nextSibling;
			}
		}

		Core.preventDefault(event);
	},
	
	resettaStelleListener: function(event) {
		stelleVotoPost.resettaStella(this);
		
		var node = this;
		
		while(node.previousSibling != null) {
			if (node.previousSibling.nodeType == 1) {
				node = node.previousSibling;
				stelleVotoPost.resettaStella(node);
			} else {
				node = node.previousSibling;
			}
		}
		
		var node = this;
		
		while(node.nextSibling != null) {
			if (node.nextSibling.nodeType == 1) {
				node = node.nextSibling;
				stelleVotoPost.resettaStella(node);
			} else {
				node = node.nextSibling;
			}
		}

	},

	/* metodi per visualizzazione valutazione al passaggio del mouse */
	visualizzaValutazioneListener: function(event) {
		stelleVotoPost.visualizzaValutazione(this);
		Core.preventDefault(event); // non servirebbe
	},
	
	nascondiValutazioneListener: function(event) {
		stelleVotoPost.nascondiValutazione(this);
	},

	visualizzaValutazione: function(stella) {
		stelleVotoPost.nascondiValutazione(stella);

		document.getElementById("valutazionePost").firstChild.nodeValue = stella._valutazione;
	},
	
	nascondiValutazione: function(stella) {
		document.getElementById("valutazionePost").firstChild.nodeValue = "";
	},

	/* metodi per inoltro valutazione AJAX */
	inoltraVotoListener: function(event) {
		stelleVotoPost.inoltraVoto(this);
		Core.preventDefault(event);
	},

	inoltraVoto: function(stella) {
		var salvataggio = new salvaVotoAjax(stella);
	},

};

// definizione della classe prototype per gestire comunicazione AJAX
salvaVotoAjax = Class.create();
salvaVotoAjax.prototype = {
	url : null,
	initialize : function(stella) {
    
		form = $('form-votoAjax');
		this.url = form.action;

		var options = {
            method     : 'post',
            parameters : 'post_id='+$('input-post_id').getValue()+'&voto='+stella._votoNumerico,
            onSuccess  : this.onSaveSuccess.bind(this),
            onFailure  : this.onSaveFailure.bind(this)
		}

		//message_write('Salvataggio voto...'); se lo inserisco incasino l'effetto
		new Ajax.Request(this.url, options);

	},
	
	onSaveFailure : function(transport) {
		var messaggio = 'Salvataggio voto fallito!';
		message_write(messaggio);
		setTimeout("message_clear()", 5000);
		notifica_write(messaggio);
		setTimeout("notifica_clear()", 2000);
	},

	onSaveSuccess : function(transport) {
		var json = transport.responseText.evalJSON(true);
		if (json.messaggio) {
			var messaggio = json.messaggio;
		}
		if (json.notifica) {
			var notifica = json.notifica;
		}
		message_write(messaggio);
		setTimeout("message_clear()", 5000);
		notifica_write(notifica);
		setTimeout("notifica_clear()", 2000);
	},


};

Core.start(stelleVotoPost);