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

canVote=0;

initialW=0;

Event.observe(window,"load",initialWidth);

function initialWidth(){
	if($('starUser'))
	initialW=$('starUser0').width;
	canVote=1;
}


function stst(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:stst(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:stst(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) {
	
if(!canVote) return;
	
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 {
	if(donotwidth==0){
	$S('starCur'+n).width=oX+'px';
	$S('starUser'+n).color='#111';
	$('starUser'+n).innerHTML=Math.round(oX/84*50/*5*10*/)/10//+'%';
	mwidth=oX;
	}
}
};
} };

donotwidth=0;
mwidth=0;
star.update=function(e,o) {
	
if(!canVote) return;
	
canVote=0;
var n=star.num, v=parseInt($('starUser'+n).innerHTML);

n=o.id.substr(4); $('starCur'+n).title=v;

new Ajax.Request( '/article/rate/format/json' ,
{
	method:'post',
	parameters: { rate :    v/5 ,   ar_id  :  $(  'ar_id'  ).value } ,
	onSuccess: function(transport){
		donotwidth=1;
		response = transport.responseJSON;
		if ( response.allow ) {  $("stardiv" + n  ).innerHTML = " Ati mai votat o data! "   ; setTimeout( function () {

			$( 'stardiv' + n  ).innerHTML = "" ; } , 2000 )

		}




		if ( response.noperm ) {  $("stardiv" + n).innerHTML = response.noperm   ; setTimeout( function () {

			$( 'stardiv' + n    ).innerHTML = "" ; } , 2000 ) } ;

			if ( response.rate_ok ) { $("stardiv" + n ).innerHTML = " Vot inregistrat! " ; setTimeout( function() {

				$( 'stardiv' + n ).innerHTML = ""  ; } , 2000 ) ;

			}
	},
	onFailure: function(){ alert('Something went wrong...') }
});

};

star.revert=function() { 

if(!canVote) return;
	
var n=star.num, v=parseInt($('starCur'+n).title);

$S('starCur'+n).width=initial_width//Math.round(mwidth)+'px';
$('starUser'+n).innerHTML="";//(v>0?Math.round(v)+'%':'');
$('starUser'+n).style.color='#888';

document.onmousemove='';

};

star.num=0;

