window.addEvent('domready', function() {
	var moostarwidth= 84; // width of Star Rating container
	var moostarnum = 5 // number of stars
	var inpercent = false; // Set this flag to true , if you require percentage values to be displayed
	var isFractional = false // Set this to true, if you want fractional values like 1.24, 1.25, 4.56  rather than 1,2 ...5
	var moostar = $$('.moostar');
	
	// Behavior for each star rating
	moostar.each(function(el,i){
		el.clickable = true;
		el.revert =true;
		var id_article = el.getChildren()[0].get('id_article');
		var v = parseInt(el.getChildren()[0].title);
		if(inpercent){
			w = (parseInt(el.getChildren()[0].title)/100) * moostarwidth;
			}else{
				w = Math.round(el.getChildren()[0].title * (moostarwidth/moostarnum)*10)/10;
			}
		el.getChildren()[0].setStyles({'width': w});
		if(el.clickable){
		el.setStyle('cursor', 'pointer');
		el.addEvents({
			'mouseenter': function(){},
			'mousemove': function(event){
				w = event.client.x - el.getPosition().x;
						//status=event.client.x; //For test purpose only
						el.getChildren()[0].setStyles({'width': w});
						
					
			},
			'click': function(event){ 
				if(!el.clickable){
					return true;
				}
					el.revert = false;
					w = event.client.x - el.getPosition().x;
					var x = (w/moostarwidth) * moostarnum;
					if(x<=5 || x >=0) v = formatNumber(x,2);
					new Request({url:path2+'articles/vote.html?id='+id_article+'&value='+v, onComplete:evalRep}).send();
			},
			'mouseleave': function(){
				if(!el.clickable){
					return true;
				}
					//status ="left"; //For test purpose only
					if(el.revert){
						w = Math.round(el.getChildren()[0].title * (moostarwidth/moostarnum)*10)/10;
						el.getChildren()[0].setStyles({'width': w});
					}
					el.revert = true;
			}
		});
		}
	});
});
function formatNumber(myNum, numOfDec) { var decimal = 1; for(i=1; i<=numOfDec;i++)decimal = decimal *10
  var myFormattedNum = (Math.round(myNum * decimal)/decimal).toFixed(numOfDec)
  return myFormattedNum;
} 

function updateRating(id, rating) {
	//alert(id + " , " +rating );
} 
