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

var initial_width=0;
var timer_message = 3000;

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

function initialW(){
	has_voted=0;
	if($('starUser0'))
	initial_width=$S('starCur0').width;
}

var _g_ = 0;
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(has_voted) return;
	
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 {
	
	if(has_voted==1)return;
	$S('starCur'+n).width=oX+'px';
	//alert(oX);
	$S('starUser'+n).color='white';
	$('starUser'+n).innerHTML=Math.round(oX/84*5*10)/10 ;
	_g_ = Math.round(oX/84*100)/100;
}
};
} };

has_voted=1;

star.update=function(e,o) {
	
	if(has_voted==1){
		return;
	}
	
	has_voted=1
	var n=star.num, v=_g_;
	n=o.id.substr(4); $('starCur'+n).title=v*100;


	if( $( 'ar_id' ) ) {



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

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

				}




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

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

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

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

					}



			},
			onFailure: function(){
				window.location = "/"; 
			}
		});
	}
	else if( $( 'jar_id' ) ) {



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

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

				}




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

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

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

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

					}



			},
			onFailure: function(){
				window.location = "/"; 
			}
		});
	}
	else {
		_id = parseInt( n ) ;
		new Ajax.Request( '/recipe/rate/format/json' ,
		{
			method:'post',
			parameters: { rate :    v ,   ar_id  :  _id } ,
			onSuccess: function(transport){
				response = transport.responseJSON;
				if ( response.allow ) {  $("stardiv" + n).innerHTML = " Ati mai votat o data! "   ; setTimeout( function () {

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

				}

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

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

				}




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

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

				}
			},
			onFailure: function(){
				window.location = "/"; 
			}
		});

	}

};

star.revert=function() { 
	
	if(has_voted) return;
	var n=star.num, v=$('starCur'+n).title;
	if( v<=1 ) { v*= 10 };
	$S('starCur'+n).width=initial_width//Math.round(v*82/100)+'px';
	$('starUser'+n).innerHTML=""//(v>0?Math.round(v*5)/100     :'');
	//$('starUser'+n).style.color='white';

document.onmousemove='' ;

};

star.num=0;






function rate() {
	
	if ( $('letmerate').value == 1 ) {

		$('star0').onmousedown    =       star.update( event , this )       ;
		$('star0').onmousemove    =       star.mouse ( event , this )       ;

	}



}

