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');
	var moostartval = $$('.moostartval');

	// Behavior for each star rating
	moostar.each(function(el,i){
		el.revert =true;

		//do not bind event if element has no ID, Leave it for readyonly (Lock) mode [display purpose only].
		if (el.id != '')
		{
			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});
							var x = (w/moostarwidth) * moostarnum;
							if(inpercent){
								var v = Math.round(w/moostarwidth*100);
								if(v <101) moostartval[i].innerHTML= Math.round(w/moostarwidth*100)+'%';
							}else{
								if(isFractional){if(x<=moostarnum || x >=0) moostartval[i].innerHTML= formatNumber(x,2);}
								else{moostartval[i].innerHTML= Math.round(x);
							}
						}
				},
				'click': function(){ 
						//alert(moostartval[i].id);
						updateRating(i,moostartval[i].innerHTML);
						el.getChildren()[0].title = parseFloat(moostartval[i].innerHTML);
						el.revert = false;
				},
				'mouseleave': function(){
						//status ="left"; //For test purpose only
						if(el.revert){
							var v = parseInt(el.getChildren()[0].title);
							if(inpercent){
								w = (parseInt(el.getChildren()[0].title)/100) * moostarwidth;
								moostartval[i].innerHTML = v +'%';
								}else{
								w = parseInt(el.getChildren()[0].title) * (moostarwidth/moostarnum);
								moostartval[i].innerHTML = v;
							}
							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 );
}
