// JavaScript Document

/* AJAX Star Rating : v1.0.3 : 2008/05/06 */
/* http://www.nofunc.com/AJAX_Star_Rating/ */

function $(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:$(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.body.scrollLeft,'Y':event.clientY+document.body.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

star={};

function get_stelle(oX){
	return parseInt(oX/84.1 * 5) + 1;
}

var arr_giudizio = new Array(	
	"Sconsigliato", 
	"Niente di speciale", 
	"Degno di nota",
	"Consigliato",
	"Strepitoso"
);

function updateLock()
{
	document.getElementById("email").value = 
		arr_voti_bloccati[0] + " - " + arr_voti_bloccati[1] + " - " + arr_voti_bloccati[2] + " - " + arr_voti_bloccati[3];
}

var arr_voti_bloccati = new Array(false, false, false, false);

var n_prec = null;

star.mouse=function(e,o) 
{ 	
	n=o.id.substr(4);
	
	// caso in cui ho lasciato una stella senza aver votato viene azzerata!!
	if(n_prec != null && n!= n_prec && !arr_voti_bloccati[n_prec])
	{
		$S('starCur'+n_prec).width= '0px';
		$('starUser'+n_prec).innerHTML= 'clicca per votare';
	}
	n_prec = n;	

	if(star.stop || isNaN(star.stop)) 
	{ 
		star.stop=0;
		document.onmousemove = function(e) 
		{ 
			if(arr_voti_bloccati[n] == false)
			{
				var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);
				//document.getElementById("email").value = 
				//	"n:" + n + " o(" + oX + ", " + oY + ") - p(" + p.X + ", " + p.Y + ") m(" + x.X + ", " + x.Y + ")";
		
				if(oX<1 || oX>84)// || oY<0 || oY>19) 
				{ 
					star.stop=1; 
					star.revert(); 
				}
				else {			
					var nStelle = get_stelle(oX);
					oX = parseInt(nStelle * 84.0 /5) ;
					$S('starCur'+n).width=oX+'px';
					$('starUser'+n).innerHTML= arr_giudizio[nStelle - 1]; // Math.round(oX/84*100)+'%';
				}
			}
		};
	}
};

star.update=function(e,o) 
{ 
	var n=o.id.substr(4);
	arr_voti_bloccati[n] = true;
	
	var oX = ($S('starCur'+n).width);
	oX  = oX .substring(0, oX.length-2);
	var nStelle = get_stelle(oX);
	$('starCur'+n).title = arr_giudizio[nStelle - 1];;
	$('starUser'+n).innerHTML='Grazie per il tuo voto!!';

	document.getElementById("iVoto" + n).value = nStelle;
};

star.revert=function() { 
	var n=star.num
	//var n=o.id.substr(4);

	if(arr_voti_bloccati[n] == false)
	{
		v=parseInt($('starCur'+n).title);
		$('starCur'+n).width=Math.round(v*84/100)+'px';
		$('starUser'+n).innerHTML= 'clicca per votare';
		//$('starUser'+n).style.color='#888';
	}
	document.onmousemove='';

};

star.num=0;

