/**
 * Gestisce l'onmouseover sulle stellette per la votazione
 * @param	integer 	indexStar	indice della stelletta che ha causato l'evento
 */
 
// Per retrocompatibilità
if (path_img == undefined) {
    var path_img = '/file_generali/img/';
}

function overStar(indexStar) {
  if (parseInt(document.getElementById('evento_votato').value) > 0) {
	  return;
  }
	  
  while (parseInt(indexStar) > 0) {
	var star = document.getElementById('stella_votazione_'+indexStar);
	  
	if (star != null) {
	  star.src = path_img + 'stellina_accesa.gif';
	}    
    indexStar--;
  }  
}

/**
 * Gestisce l'onmouseout sulle stellette per la votazione
 * @param	integer 	indexStar	indice della stelletta che ha causato l'evento
 */
function outStar(indexStar) {
  if (parseInt(document.getElementById('evento_votato').value) > 0) {
	  return;
  }
  
  while (parseInt(indexStar) > 0) {
	var star = document.getElementById('stella_votazione_'+indexStar);
	  
	if (star != null) {
	  star.src = path_img + 'stellina_spenta.gif';
	}    
    indexStar--;
  }  
}

/**
 * Oggetto che incapsula la funzione di richiesta/invio dati al server e la logica 
 * della risposta. Utilizza le componenti della libreria YUI yahoo e connection.
 */
var VoteEventObject = {

	handleFailure:function(o){
          alert('Impossibile completare l\'operazione di voto: si prega di riprovare piu\' tardi.');
	},
		
	handleSuccess:function(o){
		var index = parseInt(o.responseText);
    	var count = index;
    	while (count >= 1) {
			var star = document.getElementById('stella_votazione_'+count);
			count--;
			if (star == null) {			  	
			  continue;
			}
			star.src = path_img + 'stellina_votata.gif';
    	}
    	
    	var divElem = document.getElementById('box_voto'); 
    	document.getElementById('evento_votato').value = o.responseText; 
  		
  		// Spengo le altre stelline
  		while (true) {
  		  index++;	
  		  var star = document.getElementById('stella_votazione_'+index);
  		  if (star == null) {
  		    return;
  		  }
  		  star.src = path_img + 'stellina_spenta.gif';
  		}		
	},

	startRequest:function(param) {
          var sUrl = document.location + '&voto='+param;
          var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
	}
};

/**
 * Oggetto di callback per la gestione delle risposte dal server. Definisce anche un 
 * timeout di 2 secondi.
 */
var callback =
{
	success:VoteEventObject.handleSuccess,
	failure:VoteEventObject.handleFailure,
	scope: VoteEventObject,
	timeout: 2000
};

/**
 * Effettua la richiesta al server per effettuare la votazione.
 * @param	integer 	indexStar	indice della stelletta che ha causato l'evento 
 */
function votaEvento(indexStar) {
    if (parseInt(document.getElementById('evento_votato').value) > 0) {
	    return;
	}
	VoteEventObject.startRequest(indexStar);
}
