/**
  *
  *  Copyright 2005 www.AjaxLine.com - NaikonSoft
  *  Author Igor Kononuchenko
  *
  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  *  file except in compliance with the License. You may obtain a copy of the License at
  *
  *         http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software distributed under the
  *  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  *  either express or implied. See the License for the specific language governing permissions
  *  and limitations under the License.
  **/



  Rating = function(container, options) {this.initialize(container, options);};

  Rating.prototype = {
  initialize: function(container, options){
  this.container = $("#"+container);
  this.options = options;
  this.setOptions();
  this.stars = new Array();

   this.createStars();

  },
  setOptions:function(){

    this.urlToSend =   this.options.urlToSend;
    this.overallRating = this.options.overallRating;
    this.displayText = "";
    this.afterVoteText="";
    this.starsCount = 5;
    this.hints = "";
    this.name      = this.options.name;
    this.valname   = this.options.valname;
    this.value     = this.options.value-1;
    this.votetype  = this.options.votetype;
    this.id        = this.options.id;



    this.emptyImageUrl 			   = BASEURL + 'images/starbig_empty.jpg',
    this.hoverImageUrl 			   = BASEURL + 'images/starbig1.jpg',
    this.votedImageUrl 			   = BASEURL + 'images/starbig.jpg',
    this.afterYouVotedImageUrl     = BASEURL + 'images/starbig.jpg',
    this.votedHalfImageUrl         = BASEURL + 'images/starbig_half.jpg',
    this.afterYouVotedHalfImageUrl = BASEURL + 'images/starbig_half.jpg',

    this.isEnabled = this.options.isEnabled;
  },
 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.name+".setUnHover()",300);

 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.value =  star.index;
  this.setVoted(this.value);
  this.sendViaAjax(star.index+1);
},
sendViaAjax:function(value){
     $("#"+this.valname).val(value);
      var url = BASEURL + '/ajax.php?id=' +this.id+'&value=' +value;
      //createCookie('bm'+this.id, 1, 365);

      $.ajax({
	   type: "GET",
	   url: url,
	   success: function(param){
	        if (param != "") {
               this.value =  param - 1;
               this.setVoted(this.value);
               this.setAfterYouVoted(this.value);
               $("#"+this.valname).val(param);
               this.isEnabled = false;
           }
	   },
	   error: function(val1, val2, val3){

	   }
	 });


},
  onComplete:function(originalRequest)
	{
	      this.value = eval(originalRequest.responseText)-1;
	     if  (this.overallRating)
          this.overallRating.setAfterYouVoted(this.value);
         else
          this.setAfterYouVoted(this.value);
         setTimeout(this.sendTextClose.bind(this),300);

	},

  sendTextClose:function()
  {

  },


setHover:function(isSetHover,index){
 for(var i=0;i<index+1;i++)
  {
    this.stars[i].setHover(isSetHover);
  }
},

setNewRate:function(){
 for(var i=0;i<this.stars.length;i++)
  {
    this.stars[i].isVoted = false;
  }
  this.setUnHover();
},

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<this.starsCount-1;i++)
  {
     this.stars[i].isVoted = false;
   }

  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 = '<BR>'+this.afterVoteText+': '+(index+1);
}

};



 Star = function(parent,index) {this.initialize(parent,index);};
 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.align = "absmiddle";
  this.img.src =this.parent.emptyImageUrl; //this.parent.isEnabled?this.parent.emptyImageUrl:this.parent.emptyImageUrl;
  this.img.star = this;
  this.parent.container.append(this.img);
  if (this.parent.hints)
     this.img.title       = this.parent.hints[this.index];
  this.img.onclick     = this.onclick;
  this.img.onmouseover = this.onmouseover;
  this.img.onmouseout  = this.onmouseout;
 },
 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.star.parent.isEnabled)
 {
   this.star.img.style.cursor ='pointer';
   this.star.parent.notifyMouseOver(this.star);

 }
 },
 onmouseout:function(e){
  if (this.star.parent.isEnabled)
 {


  this.star.img.style.cursor ='default';

 if(this.star.isNextElemStar(e))
      this.star.parent.notifyMouseOut(this.star)
 else
 {
   if (this.star.parent.stars[this.star.index].isVoted)
       this.star.setVoted(this.star.getVotedImageUrl());
    else
       this.star.img.src =this.star.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(e){   e = e || window.event;
  isIE = navigator.userAgent.toLowerCase().indexOf("ie")!= -1;
  if(isIE && e != null && e.toElement != null)
   return e.toElement.tagName!='IMG';
  else if(e == null || e.relatedTarget == null)
  return false;
  else
  return e.relatedTarget.tagName!='IMG';
},
onclick:function(e){
  if (this.star.parent.isEnabled)
   {
    this.star.img.style.cursor ='default';
   this.star.parent.notifyClick(this.star);
   }
 }


};


function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

