/***** star rank ******/

/****
* HTML Usage: <span id="id0123456789" class="starrank" rank="1.76" params="{'pizza':'good'}"></span>
* id : unique idenitifier for ranking
* class: starrank by default. This can be changed via the options parameter; 
* rank: rank value
* params (optional): any extra parameters you want to pass to the handler
*
* Java Ussage: var starRank = new StarRank();
*              starRank.init( handler, options );
* handler (optional): the function to call when a ranking is registered. 
*          the handler Function should be written as function( rank , id, params );
* options (optional): values rankonce => [true,false], Default: true
*                            classname => name of class, Default: starrank
*                            readonly => only show rank, dont allow voting
*
* Note: If rankonce is set to true, the user will only be allowed to rank once. The handler function will 
*       only be called for the first ranking. If rankonce is set to false, the user will be allowed to rank
*       as many times as they choose, the handler function will be called each time.
******/

var StarRank = new Class({
  initialize: function(handler, options){
    this.handler = handler;
    this.classname = 'starrank';
    this.cookieduration = 365;
    this.rankonce = true;
    this.readonly = false;
    if( options && typeof(options.rankonce) != "undefined" ) this.rankonce = options.rankonce;
    if( options && typeof(options.classname) != "undefined" ) this.classname = options.classname;
    if( options && typeof(options.readonly) != "undefined" ) this.readonly = options.readonly;
    if( options && typeof(options.cookieduration) != "undefined" ) this.cookieduration = options.cookieduration;

    if(document.images)
    {
      preload_image_object = new Image();
      image_url = new Array(16);
      image_url[0] = statichost+'/images/stars/star_1_sel.png';
      image_url[1] = statichost+'/images/stars/star_2_sel.png';
      image_url[2] = statichost+'/images/stars/star_3_sel.png';
      image_url[3] = statichost+'/images/stars/star_4_sel.png';
      image_url[4] = statichost+'/images/stars/star_5_sel.png';
      image_url[5] = statichost+'/images/stars/star_0.png';
      image_url[6] = statichost+'/images/stars/star_0.5.png';
      image_url[7] = statichost+'/images/stars/star_1.png';
      image_url[8] = statichost+'/images/stars/star_1.5.png';
      image_url[9] = statichost+'/images/stars/star_2.png';
      image_url[10] = statichost+'/images/stars/star_2.5.png';
      image_url[11] = statichost+'/images/stars/star_3.png';
      image_url[12] = statichost+'/images/stars/star_3.5.png';
      image_url[13] = statichost+'/images/stars/star_4.png';
      image_url[14] = statichost+'/images/stars/star_4.5.png';
      image_url[15] = statichost+'/images/stars/star_5.png';

      for( var i = 0; i < image_url.length; i++){
         preload_image_object.src = image_url[i];
      }
    }
  },
  init: function(){

    this.stars = $$('.'+this.classname);

    this.stars.each(function(item) {
      var listedrank = parseFloat( item.getAttribute('rank') );
      var rank = this.convert( listedrank );
      item.setAttribute("title", "Average: " + rank + " out of 5" );
      item.top = item.getTop();
      item.ranked = false;
      var id = this.getId( item );

      var cookie = new Hash.Cookie('starrank', {duration:this.cookieduration} );
      var _rank = cookie.get(id);
      if( _rank ){
         rank = _rank;
         item.ranked = true;
      }
      this.changeImage( item, rank, item.ranked );
      item.rank = rank;

      if( !this.readonly ){
        item.addEvent( 'click', function(event){
          if( this.rankonce == true && item.ranked == true ) return;
          var e = new Event(event);
          var rank = this.calcMouseRank( e, item );
          var id = this.getId( item );
          var cookie = new Hash.Cookie('starrank', {duration:this.cookieduration} );
          cookie.set(id, rank);
          item.ranked = true;
          item.rank = rank;
          this.changeImage( item, rank, true );
          if( this.handler ) this.handler( rank, id, item.getAttribute("params") );
        }.bind(this) );

        item.addEvent( 'mousemove', function(event){
          var e = new Event(event);
          var rank = this.calcMouseRank( e, item );
          if( item.mouseoverrank && item.mouseoverrank == rank ) return;
          item.mouseoverrank = rank;
          this.changeImage( item, rank );
        }.bind(this) );

        item.addEvent( 'mouseout', function(event){
          var e = new Event(event);
          var rank = item.rank;
          item.mouseoverrank = 0;
          this.changeImage( item, rank, item.ranked );
        }.bind(this) );
      }
    }.bind(this) );
  },
  calcMouseRank: function( e, item ){
    var left = item.getLeft();
    var width = item.getSize().x;
    var mousex =  e.page.x;
    var perc = (mousex - left) / width;
    if( isNaN( perc ) ) perc = 0.0;
    else if( !isFinite(perc) ) perc = 1;
    return Math.ceil( 5*perc );
  },
  changeImage: function( item, rank, userselected ){
    if( userselected == true ){
      item.innerHTML = '<img src="'+statichost+'/images/stars/star_'+rank+'_sel.png">';
    }else{
      item.innerHTML = '<img src="'+statichost+'/images/stars/star_'+rank+'.png">';
    }
  },
  getId: function( item ){
    var id = item.getAttribute('id');
    if( id && id.length > 0 ){
      return id;
    }else{
      id = this.generateId();
      item.setAttribute( 'id', id );
      return id;
    }          
  },
  generateId: function(){
    var id = "";
    var alphaNum = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var length = parseInt(5+Math.random()*4);
    for(var a = 0; a < 16; a++){
      id = id + alphaNum.charAt(parseInt(Math.random()*alphaNum.length));
    }
    return id;
  },
  convert: function( listedrank ){
      var rank = "0";
      if( 0 <= listedrank < 0.5 ) rank = "0";
      else if( 0.5 <= listedrank && listedrank < 1 )   rank = "0.5";
      else if( 1 <= listedrank && listedrank < 1.5 )   rank = "1";
      else if( 1.5 <= listedrank && listedrank < 2 )   rank = "1.5";
      else if( 2 <= listedrank && listedrank < 2.5 )   rank = "2";
      else if( 2.5 <= listedrank && listedrank < 3 )   rank = "2.5";
      else if( 3 <= listedrank && listedrank < 3.5 )   rank = "3";
      else if( 3.5 <= listedrank && listedrank < 4 )   rank = "3.5";
      else if( 4 <= listedrank && listedrank < 4.5 )   rank = "4";
      else if( 4.5 <= listedrank && listedrank < 5 )   rank = "4.5";
      else if( 5 <= listedrank )                       rank = "5";
      return rank;
  }

});