/* 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={};

star.mouse=function(e,o) { if(star.stop || isNaN(star.stop)) { star.stop=0;

	document.onmousemove=function(e) { var n=star.num;
	
		var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);

		if(oX<1 || oX>84 || oY<0 || oY>19) { star.stop=1; star.revert(); }
		
		else {

			$S('starCur'+n).width=oX+'px';
			$S('starUser'+n).color='#111';
			$('starUser'+n).innerHTML=Math.round(oX/84*100)+'%';
		}
	};
} };

star.update=function(e,o,id,llave,ip) { var n=star.num, v=parseInt($('starUser'+n).innerHTML);
	//alert(llave);
	//alert(v);
	//console.warn(v);
	//alert(id);
	if(isNaN(v))
	{
		alert("Solo se puede calificar una vez");
		return 0;
	}
	n=o.id.substr(4); 
	$('starCur'+n).title=v;  //actualiza el color de las estrellas
	//alert(n);
	
	//llave = '<? echo $llave;  ?>';
	//ip = '<? echo $_SERVER["REMOTE_ADDR"]; ?>';
	//alert(ip);
	//alert(llave);
	dojo.xhrGet(
	{
		url:"rate.php?llave="+llave+"&tipo=califica&id="+id+"&calif="+v+"&ip="+ip,
			handleAs:"text",
			load: function(data,args)
			{
				//alert(data);
				if (data == "")
					return 0;
				datos = data.split('|');
				//$('starCur'+n).title=10;
				document.getElementById("starCur0").title =datos[1]; //10;
				puntos = datos[1] * 84 / 100;
				
				puntosC = puntos+'px';
				//alert(puntosC);	
				document.getElementById("starCur0").style.width = puntosC;
				document.getElementById("estado").innerHTML = "Promedio:"+datos[1]+"% "+datos[0]+" votos";
				//despues de votar deshabilita el control
				document.getElementById("star0").disabled = true;
				document.getElementById("star0").onmousemove = "";
				document.getElementById("star0").onmousedown = "";
				
			},
			error: function(error,args)
			{
				console.warn("error:",error);
			}
		});
	
	
	//req=new XMLHttpRequest(); req.open('GET','test.php?vote='+(v/100),false); req.send(null);    

};

star.revert=function() { var n=star.num, v=parseInt($('starCur'+n).title);

	$S('starCur'+n).width=Math.round(v*84/100)+'px';
	$('starUser'+n).innerHTML=(v>0?Math.round(v)+'%':'');
	$('starUser'+n).style.color='#888';
	
	document.onmousemove='';

};

star.ini=function(ip,registroId,llave) {
	//llave = '<? echo $llave;  ?>';
	//ip = "2.2.2.2"; //'<? echo $ip_votante; ?>';
	
	dojo.xhrGet(
	{
		url:"rate.php?llave="+llave+"&tipo=ini&id="+registroId+"&ip="+ip,
			handleAs:"text",
			load: function(data,args)
			{
				//alert(data);
				datos = data.split('|');
				//$('starCur'+n).title=10;
				document.getElementById("starCur0").title =datos[1]; //10;
				puntos = datos[1] * 84 / 100;
				
				puntosC = puntos+'px';
				//alert(puntosC);	
				document.getElementById("starCur0").style.width = puntosC;
				document.getElementById("starUser0").innerHTML = "";
				document.getElementById("estado").innerHTML = "Promedio:"+datos[1]+"% "+datos[0]+" votos";
				if (datos.length > 2 && datos[2] == "yavoto")
				{
					//document.getElementById("star0").disabled = true;
					document.getElementById("star0").onmousemove = "";
					//document.getElementById("star0").onmousedown = "";
				}
			},
			error: function(error,args)
			{
				console.warn("error:",error);
			}
		});
};


star.num=0;