// JavaScript Document
/*************************************************
Star Rating System
First Version: 21 November, 2006
Second Version: 17 May, 2007
Author: Ritesh Agrawal (http://php.scripts.psu.edu/rja171/widgets/rating.php)
Inspiration: Will Stuckey's star rating system (http://sandbox.wilstuckey.com/jquery-ratings/)
Half-Star Addition: Karl Swedberg
Demonstration: http://examples.learningjquery.com/rating/
Usage: $('#rating').rating('url-to-post.php', {maxvalue:5, curvalue:0});

arguments
url : required -- post changes to 
options
  increment : 1, // value to increment by
	maxvalue: number of stars
	curvalue: number of selected stars
	
Changes to set the stars with average rating
************************************************/

jQuery.fn.rating = function(url, options) {
	
	
	var settings = {
    url : url, // post changes to 
    increment : 1, // value to increment by
    maxvalue  : 5,   // max number of stars
    curvalue  : 0,    // number of selected stars
    actionId  : 0
  };
	
  if(options) {
    jQuery.extend(settings, options);
  };
  jQuery.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});
   
   
  var container = jQuery(this);
	container.empty();
	jQuery.extend(container, {
    averageRating: settings.curvalue,
    url: settings.URL
  });
  settings.increment = (settings.increment < .75) ? .5 : 1;
  var s = 0;
	for(var i= 1; i <= settings.maxvalue ; i++){
			var $star = $('<label for="rate'+i+'_'+settings.actionId+'" title="Give it '+i+'/'+settings.maxvalue+'">'+i+'</label>')
			 
      var $div = $('<div class="star"></div>')
        .append($star)
        .appendTo(container);
        if(i <= settings.curvalue){
      	$div.addClass('star_on');
      	}
      if (settings.increment == .5) {
        if (s%2) {
          $div.addClass('star-left');
        } else {
          $div.addClass('star-right');
        }
      }
     
    i=i-1+settings.increment;
    s++;
  }
	
	return(this);	

};
