var Rating = Class.create();
Rating.prototype = {
 initialize:function(container,options){
  this.container = $(container);
  this.options = options;
  this.setOptions();
  this.stars = new Array();
   this.onSendLabel =  document.createElement('text');
 this.onSendLabel.innerHTML = this.options.onSendText+'<BR>';
 this.onSendLabel.style.display = 'none';
   this.container.appendChild( this.onSendLabel);
  this.createStars();
this.title =document.createElement('text'); 
   this.setTitle();
   this.container.appendChild(this.title);
   if (this.value) this.setHover(true,this.value);
  },
  setTitle:function(){
   var byUser= (this.byUsers)?' by '+this.byUsers+' users':'';
   this.title.innerHTML = '<BR>'+this.displayText+byUser;
  },
  setOptions:function(){
    
   this.byUsers =  this.options.byUsers;
    this.value =  this.options.value-1;
   this.gameid = this.options.gameid;
   this.urlToSend =   this.options.urlToSend; 
    this.overallRating = this.options.overallRating;
    this.displayText = this.options.displayText;
    this.displayText = this.options.displayText;
    this.starsCount = this.options.starsCount;
    this.emptyImageUrl = this.options.emptyImageUrl;
    this.hoverImgUrl = this.options.hoverImgUrl;
    this.isEnabled = this.options.isEnabled;
  },
 createStars:function(){  
  for(var i=0;i<this.starsCount;i++)
  {
    this.stars.push(new Star(this,i));
  }  
 },

notifyMouseOver:function(star){

 this.currentValue = star.index;
 this.setHover(true,star.index);
},
notifyMouseOut:function(star){
 
 this.currentValue = star.index;
 setTimeout(this.setUnHover.bind(this),300);
 if (this.currentValue!=star.index)  this.setHover(true,this.currentValue);
},
setUnHover:function(){
 this.setHover(false,this.starsCount-1);
},
notifyClick:function(star){
 this.setHover(true,star.index);
 this.isEnabled = false;
  this.onSendLabel.style.display = 'block';
  this.sendViaAjax(star.index+1);
},
sendViaAjax:function(value){ 

		var pars = 'rating=' + value+'&gid='+this.gameid;	
		this.request = new Ajax.Request(
			this.urlToSend, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: this.onComplete.bind(this)
			});

},
  onComplete:function(originalRequest)
	{	
     //   alert(originalRequest.responseText);

         this.overallRating.setHover(true,eval(originalRequest.responseText)-1);   
           this.overallRating.byUsers++;
           this.overallRating.setTitle(); 
         setTimeout(this.sendTextClose.bind(this),300);
	},

  sendTextClose:function()
  {
       this.onSendLabel.style.display = 'none';
  },


setHover:function(isSetHover,index){
 for(var i=0;i<index+1;i++)
  {
    this.stars[i].setHover(isSetHover);
  } 
}
 
};
var Star = Class.create();
Star.prototype = {
initialize:function(parent,index){
   this.container = parent.container;
   this.parent = parent;
   this.img =null;
   this.index =index; 
   this.CreateDOM();
},
 CreateDOM:function(){
  this.img = document.createElement('img');
  this.img.src =this.parent.emptyImageUrl; //this.parent.isEnabled?this.parent.emptyImageUrl:this.parent.emptyImageUrl;   
  this.parent.container.appendChild(this.img);
  this.img.onclick = this.onclick.bindAsEventListener(this);
  this.img.onmouseover = this.onmouseover.bindAsEventListener(this);
    this.img.onmouseout = this.onmouseout.bindAsEventListener(this);
 }, 
 setHover:function(isSetHover){
  this.img.src =!isSetHover?this.parent.emptyImageUrl:this.parent.hoverImgUrl; 
 },
 onmouseover:function(e){ 
 if (this.parent.isEnabled) 
 {
   this.img.style.cursor ='pointer';
   this.parent.notifyMouseOver(this);
 
 }
 },
 onmouseout:function(e){ 
  if (this.parent.isEnabled) 
 {

 
  this.img.style.cursor ='default'; 
 
  if(this.isNextElemStar(e))  
 this.parent.notifyMouseOut(this) 
 else
  this.img.src =this.parent.emptyImageUrl;   
 }
 },
isNextElemStar:function(e){
  isIE = navigator.userAgent.toLowerCase().indexOf("ie")!= -1;
  if(isIE)
   return e.toElement.tagName!='IMG';
  else
  return e.relatedTarget.tagName!='IMG';
},
onclick:function(e){ 
  if (this.parent.isEnabled) 
   {
    this.img.style.cursor ='default'; 
   this.parent.notifyClick(this);
   }
 }


};