function $D(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:$D(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$D(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); }

function gettext(rate) {
   if (rate >= 80)
        return 'Best';
   else if (rate < 80 && rate > 60) 
        return 'Good';
   else if (rate <= 60 && rate >= 50) 
        return 'OK';
   else if (rate <= 50 && rate > 20) 
        return 'Not bad';
   else if (rate <= 20) 
        return 'Poor';
        
}

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($D('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';
            rate = Math.round(oX/84*100);
            $D('starU'+n).innerHTML=rate +'%' ;
			$D('starUser'+n).innerHTML=gettext(rate);
            
		}
	};
} };

star.update=function(e,o,d) { var n=star.num, v=parseInt($D('starU'+n).innerHTML);

	n=o.id.substr(4); $D('starCur'+n).title=v;
   
    req=new XMLHttpRequest();
    req.onreadystatechange = function() {
   
	    if (req.readyState == 4) {

	        if (req.status == 200) {
                if (req.responseText == 'OK') {        
                    $D('starUser'+n).innerHTML= 'Thank for voting' ;
                }

	        } else {
	            //alert("Error :\n" + req.statusText);
                $D('starUser'+n).innerHTML= 'Sorry! Error' ;
	        }
	    }
		}
    
     
	 req.open('GET','/mod/video/tool.php?act=rate&id=' + d + '&r=' +(v/100),true); 
     req.send(null);    

};

star.revert=function() { var n=star.num, v=parseInt($D('starCur'+n).title);

	$S('starCur'+n).width=Math.round(v*84/100)+'px';
	$D('starU'+n).innerHTML=(v>0?Math.round(v)+'%':'');
	$D('starUser'+n).style.color='#888';
	
	document.onmousemove='';

};

star.num=0;
