/*
Title 	: Simple Star Rating Script Using Mootools
Author 	: Nikhil Kunder (nik1409@gmail.com)
Date 	: 2008/01/12
Version : 1.0
    moostar_v1.0.js  is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 	Lesser General Public License for more details.
*/
window.addEvent('domready', function() {
	var moostarwidth= 85; // 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.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<=5 || x >=0) moostartval[i].innerHTML= formatNumber(x,2);}
					else{moostartval[i].innerHTML= Math.round(x) +'/5';
					}
				}
			},
			'click': function(){ 
				updateRating(el.id,moostartval[i].innerHTML);
				el.getChildren()[0].title = parseFloat(moostartval[i].innerHTML);
			},
			'mouseleave': function(){
				//status ="left"; //For test purpose only
				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 +'/5';
				}
				el.getChildren()[0].setStyles({'width': w});
			}
		});
	});
});
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 );
	document.getElementById(id+'Value').value = rating;
} 