// Star rating script from http://www.ajaxline.com/node/176
var Rating = Class.create();
Rating.prototype = {
 initialize: function(container,options){
    this.container = $(container);
    this.options = options;
    this.setOptions();
    this.stars = [];
    this.onSendLabel = document.createElement('text');
    this.onSendLabel.innerHTML = this.options.onSendText.length === 0 ? "" : this.options.onSendText+'<BR>';
    this.onSendLabel.style.display = 'none';
    this.container.appendChild( this.onSendLabel);
    this.createStars();
    
    this.dispalyLabel = document.createElement('text');
    this.dispalyLabel.innerHTML = '<BR>' + this.displayText;
    this.container.appendChild(this.dispalyLabel);
  },
  
  setOptions: function() {
    this.overallRating              = this.options.overallRating;
    this.displayText                = this.options.displayText;
    this.afterVoteText              = this.options.afterVoteText;
    this.starsCount                 = this.options.starsCount;
    this.hints                      = this.options.hints;
    this.value                      = this.options.value - 1;
    this.id                         = this.options.id;
    this.emptyImageUrl              = this.options.emptyImageUrl;
    this.hoverImageUrl              = this.options.hoverImageUrl;
    this.votedImageUrl              = this.options.votedImageUrl;
    this.votedHalfImageUrl          = this.options.votedHalfImageUrl;
    this.afterYouVotedImageUrl      = this.options.afterYouVotedImageUrl;
    this.afterYouVotedHalfImageUrl  = this.options.afterYouVotedHalfImageUrl;
    this.isEnabled                  = this.options.isEnabled;
    this.afterRatingCallback 				= this.options.afterRatingCallback;
  },
 
 createStars: function(){  
    for(var i = 0; i < this.starsCount; i++) {
        this.stars.push(new Star(this,i));
    } 
    this.setVoted(this.value);
 },

notifyMouseOver: function(star){
    this.currentValue = star.index;
    this.setHover(true,star.index);
},

notifyMouseOut: function(star){ 
    this.currentValue = star.index;
    setTimeout(this.setUnHover.bind(this), 100);
    
    if (this.currentValue != star.index)  {
      this.setHover(true,this.currentValue);
    }
},

setUnHover: function(){
    this.setHover(false, this.starsCount - 1);
    this.setVoted(  this.value );
},

notifyClick: function(star){
    this.setAfterYouVoted(star.index);
    this.isEnabled = false;
    this.onSendLabel.style.display = 'block';
    this.sendViaAjax(star.index + 1);
},

sendViaAjax:function(value){ 
		xajax_saveCocktailFeedback(this.id, value);
		var cocktailId = parseInt(this.id, 10);
		var arrForTracking = [cocktailId, value];
		registerUserAction("SUBMIT_COCKTAIL_FEEDBACK", arrForTracking.toJSON());
		this.afterRatingCallback(this.id, value);
},


setHover: function(isSetHover, index) {
    for(var i = 0; i < index + 1; i++){
        this.stars[i].setHover(isSetHover);
    }
},
  
setVoted: function(index) {
    for(var i = 0; i < index + 1; i++) {
        if (i - (index + 1) > -1) {
            this.stars[i].setVoted(this.votedHalfImageUrl); 
        } else {
            this.stars[i].setVoted(this.votedImageUrl);
        }
    }
},
  
  
setAfterYouVoted: function(index) {
    this.setHover(false, this.starsCount-1);
    for(var i = 0; i < index + 1; i++) {
         if (i - (index + 1) > -1) {
              this.stars[i].setVoted(this.afterYouVotedHalfImageUrl); 
          } else {
            this.stars[i].setVoted(this.afterYouVotedImageUrl);
          }
    } 
    this.dispalyLabel.innerHTML = this.afterVoteText.length === 0? "" : '<BR>'+this.afterVoteText+': '+ (index+1);
}
};

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);
    if (this.parent.hints) {
        this.img.title = this.parent.hints[this.index];
    }
    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.hoverImageUrl; 
},

setVoted: function(url){
    this.setImage(url);
    this.isVoted = true;
},

setAfterYouVoted: function(){
    this.img.src =this.parent.afterYouVotedImageUrl; 
    this.isVoted = true;
},

setImage: function(url){
    this.img.src = url;
},

onmouseover: function(e){ 
    if (this.parent.isEnabled) {
//    		console.log("onMouseOver", this.parent.id);
        this.img.style.cursor ='pointer';
        this.parent.notifyMouseOver(this);
    }
 },
 
onmouseout: function(e){ 
    if (this.parent.isEnabled) {
//    	console.log("onMouseOut", this.parent.id);
        this.img.style.cursor ='default'; 
        if(this.isNextElemStar(e)) {
            this.parent.notifyMouseOut(this);
        } else {  
              if (this.isVoted) {
                  this.setVoted(this.getVotedImageUrl()); 
              } else {
                  this.img.src = this.parent.emptyImageUrl;
              }
        }
    }
 },
 
 getVotedImageUrl:function(){
    return (this.parent.value-(this.index)>-1&&this.parent.value-(this.index)<0)? this.parent.votedHalfImageUrl:this.parent.votedImageUrl;
 },

isNextElemStar:function(ev){
    isIE = navigator.userAgent.toLowerCase().indexOf("ie")!= -1;
    
    if (isIE) {
    	var elem = ev.toElement;
    } else {
    	elem = ev.relatedTarget;
    }
    
	  if (elem && elem.tagName == 'IMG') {
//	  	console.log("related target: ", elem);
//	  	console.log("related target parent id:", elem.parentNode.id.substr(10));
//	  	console.log("my parent id:", this.parent.id);
	  	
	  	// This has to be the star of the SAME PARENT: different parent doesn't count, 
	  	// otherwise we'll have very bad bugs (timing issues with mouseout not firing
	  	// ... had lots of fun with that)
	  	if (elem.parentNode.id.substr(14) == this.parent.id) {
	  		return false;
	  	}
		}
		
		return true;
},

onclick: function(e){ 
    if (this.parent.isEnabled) {
        this.img.style.cursor ='default'; 
        this.parent.notifyClick(this);
    }
}
};
