/*
*  By Diego A. - www.fyneworks.com
*  This is a modified version of the star rating plugin from:
*  http://www.phpletter.com/Demo/Jquery-Star-Rating-Plugin/
*  
*  For full details and history, see:
*  http://www.fyneworks.com/jquery/star-rating/
*
* ORIGINAL COMMENTS:
*
*  This is hacked version of star rating created by <a href="http://php.scripts.psu.edu/rja171/widgets/rating.php">Ritesh Agrawal</a>
*  It thansform a set of radio type input elements to star rating type and remain the radio element name and value,
*  so could be integrated with your form. It acts as a normal radio button.
*  modified by : Logan Cai (cailongqun[at]yahoo.com.cn)
*  website:www.phpletter.com
*
*  convert a set of radio buttons to star rating type of question
*/

jQuery.fn.rating = function(settings) {
  if(typeof(groups) == "undefined") {
    var groups = {};
    var event = {
      // fill to the current mouse position.
      fill: function(n, el) {
        var stars = groups[n].valueElem.siblings('.' + settings.styleMain);
        var index = stars.index(el) + 1;
        stars
          .removeClass(settings.styleOn + ' ' + settings.styleHover)
          .slice(0,index).addClass(settings.styleHover);
      },
      
      // Reset the stars to the default index.
      reset: function(n, el){
        groups[n].valueElem
          .siblings('.' + settings.styleMain).removeClass(settings.styleOn + ' ' + settings.styleHover)
          .slice(0,groups[n].index).addClass(settings.styleOn);
      }
    };
  }
  
  $(this).each(function() {
    
    if(!$(this).attr("id")) {
      return;
    } else {
      var groupId = $(this).attr("id");
    }
    if(typeof(groups[groupId]) != "undefined") {
      return;
    }

    settings = jQuery.extend({
      enabled: true,
      cancel: false,
      cancelTip: 'Cancel Rating',
      styleMain: 'star',
      styleHover: 'star_hover',
      styleOn: 'star_on',
      click: function() { return true; },
      submit: function() { return true; },
      over: function() { return true; },
      out: function() { return true; }
    }, settings);
   
    // multiple star ratings on one page
    $(this).find('input[@type=radio].' + settings.styleMain).each(function(i) {
      var n = groupId;
      if(!groups[n]) groups[n] = {count:0, value: 0, index: 0};
      i = groups[n].count;
      groups[n].count = i+1;
      
      //prepend cancel option at the begining
      if(i == 0) {
        groups[n].valueElem = jQuery('<input type="hidden" name="' + this.name + '" value="" >');
        jQuery(this).before(groups[n].valueElem);
        
        if(settings.cancel) {
          var cancelElement = jQuery('<div class="cancel"><a href="#" title="' + settings.cancelTip + '">' + settings.cancelTip + '</a></div>');
          jQuery(this).before(cancelElement);
          if(settings.enabled) {
            jQuery(cancelElement)
              .mouseover(function(){
                jQuery(this).addClass(settings.styleOn);
              })
              .mouseout(function(){
                event.reset(n);
                jQuery(this).removeClass(settings.styleOn);
              })
              .click(function(){
                groups[n].value = 0;
                groups[n].index = 0;
                jQuery(groups[n].valueElem).val(groups[n].value);
                event.reset(n);
                return false;
              });
          }
        }
      };
      
      //insert rating option right after preview element
      var element = jQuery('<div class="' + this.className + '"><a href="javascript:;" title="' + this.value + '">' + this.value + '</a></div>');
      jQuery(this).after(element);
      if(settings.enabled) {
        jQuery(element)
          .mouseover(function(){
            if(settings.over(groups[n].valueElem)) {
              event.fill(n, this);
            }
          })
          .mouseout(function(){
            if(settings.out(groups[n].valueElem)) {
              event.reset(n);
            }
          })
          .click(function(){
            if(settings.click(groups[n].valueElem)) {
              groups[n].index = i+1;
              groups[n].value = jQuery(this).children('a').attr('title');
              jQuery(groups[n].valueElem).val(groups[n].value);
              settings.submit(groups[n].valueElem);
            }
          });
      }
      
      if(this.checked) {
        groups[n].index = i+1;
        groups[n].value = $(this).val();
        $(groups[n].valueElem).val(groups[n].value);
      }
      
      //remove this checkbox
      $(this).remove();
    });
    
    if(groups[groupId])
      event.reset(groupId);
  });
  return this;
};
