/*
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.
*/


var rc_ratings = new Class({

	Implements: Options,
	
	options: {
	   moostarwidth:80, // width of Star Rating container
	   moostarnum:5, // number of stars
	   inpercent:false, // Set this flag to true , if you require percentage values to be displayed
	   isFractional:false, // Set this to true, if you want fractional values like 1.24, 1.25, 4.56  rather than 1,2 ...5
	   moostar:'',
	   moostartval:'',
	   moostartval2:'',
	   msid:'',
	   cookie:'',
	   sid:'',
	   url:0,
	   url1:0
	},
	
	initialize: function(options) {
		
		    this.setOptions(options);
		    this.el=this.options.moostar;
			this.el.revert =true;
			this.el.addEvents({
				'mousemove': this.events.mouseover.bind(this),
				'click': this.events.onclick.bind(this),
				'mouseleave':this.events.mouseout.bind(this)
			});
			
			this.acc=0;
			var id=this.options.msid.get('html');
			if(Cookie.read(this.options.cookie+''+id)!=null)
			this.acc=1;
			
		
	},
    formatNumber:function(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;
   }, 
   updateRating:function(id,rating) {
	  
		            
			        if(Cookie.read(this.options.cookie+''+id)==null && this.options.url!=0)
					{					   
					    this.acc=1;
						var self = this;
						var req = new Request({
						method: 'post',
						url: this.options.url,
						data: {'id':id,'rating':rating},
						onComplete: function(data) 
						{
						  Cookie.write(self.options.cookie+''+id,this.options.sid, {duration:1,path:'/'});
						  
								var req = new Request({
								method: 'post',
								url: self.options.url1,
								data: {'id':id},
								onComplete: function(data) 
								{
								  var r=data.split(',')
								  self.el.getChildren()[0].setStyles({'width': (16*self.formatNumber(r[0],1))});
								  self.options.moostartval.set('html',self.formatNumber(r[0],1)); 
								  self.options.moostartval2.set('html',r[1]);
								}
								}).send();
								
						}
						}).send();
					}
		 
				   
				   
   },
   events: {
   mouseout:function(){
						if(this.acc==0)
						{
						if(this.el.revert){
							var v = parseFloat(this.el.getChildren()[0].title);
							if(this.options.inpercent){
								w = (parseInt(this.el.getChildren()[0].title)/100) * this.options.moostarwidth;
								//this.options.moostartval.set('html', v +'%');
								}else{
								w = parseInt(this.el.getChildren()[0].title) * (this.options.moostarwidth/this.options.moostarnum);
								//this.options.moostartval.set('html',v);
							}
							this.el.getChildren()[0].setStyles({'width': w});
						}
						this.el.revert = true;
						}
	},
    mouseover:function(event){
	                     if(this.acc==0)
						 {
							w = event.client.x - this.el.getPosition().x;
							//status=event.client.x; //For test purpose only
							this.el.getChildren()[0].setStyles({'width': w});
							var x = (w/this.options.moostarwidth) * this.options.moostarnum;
							if(this.options.inpercent){
								var v = Math.round(w/84*100);
								//if(v <101) this.options.moostartval.set('html', Math.round(w/84*100)+'%');
							}else{
								//if(this.options.isFractional){if(x<=5 || x >=0) this.options.moostartval.set('html', this.formatNumber(x,1));}
								//else{this.options.moostartval.set('html', Math.round(x));}
						}
						}
	},
    onclick:function(event){ 
	        w = event.client.x - this.el.getPosition().x;
			var x = (w/this.options.moostarwidth) * this.options.moostarnum;
			
			if(this.updateRating(this.options.msid.get('html'),Math.round(x))==1)
			{
			 this.el.getChildren()[0].title = parseFloat(this.options.moostartval.get('html'));
	         this.el.revert = false;
			} 
						
	}	
   }	
});