/***********************************************
zRating jQuery Plugin - Shane Wolf - 12/03/2006

Tons of thanks to Wil Stuckey and Ritesh Agrawal for their work on their rating plugins.  
This file is draws largely from their work and just added featured I felt were important and removed ones I had no use for.
  Wil Stuckey: http://sandbox.wilstuckey.com/jquery-ratings/
  Ritesh Agrawal: http://php.scripts.psu.edu/rja171/widgets/rating.php

Usage Examples: 
  $("#rating_stars").zRating("rating");
  $("#rating_stars").zRating("rating", {maximumValue: 8, currentValue: 3});
  $("#rating_stars").zRating("rating", {maxvalue: 5, remote: false}, function(c){alert(c+", now that's a mighty fine choice!")});
  $("#rating_stars").zRating("rating", {maximumValue: 10, multiplier: 10});
  $("#rating_stars").zRating("remote_rating.php", {remote: true}, function(c){$("#rating_stars").html(c)});
  
Usage Instructions:
  http://blog.shanewolf.com/2006/12/2/javascript-rating-system-using-jquery
  
***********************************************/

$.fn.zRating = function(postTo, options, callback) {
	
	var zRSettings = {
	postTo          : postTo, // either the id of the hidden field or the location the AJAX post should be sent to
    maximumValue    : 5,      // max number of stars
    currentValue    : 0,      // default initial value
    remote          : false,  // whether or not the value will go into a hidden field sent as an AJAX request
    multiplier      : 1       // multiply the selected index by this number... useful for counting by 5 or 10
  };
  
  if(options)
    $.extend(zRSettings, options);
  
  var container = $(this);
  container.addClass("z_rating");
  // Build up the zRItem list based on maximumValue
	for(var i= 1; i <= zRSettings.maximumValue; i++){
		var size = zRSettings.maximumValue
    var div = "<div class='z_star'><a></a></div>";
		container.append(div);
	}
	
	if (!zRSettings.remote && $("#"+zRSettings.postTo).length == 0){
	  var input = "<input type='hidden' id='"+zRSettings.postTo+"' name='"+zRSettings.postTo+"'>";
		container.after(input);
	}
	
	var zRItem = container.children(".z_star");
	
	// Event Handlers
  zRItem.mouseover(function(){
    event.empty();
    event.highlight(this);
  });
  
  zRItem.mouseout(function(){
    event.empty();
    event.reset();
  });
  
  zRItem.focus(function(){
    event.empty();
    event.highlight(this)
  });
  
  zRItem.click(function(){
    event.setCurrentzRItem(this);
  });
  
  // Functions to manipulate ratingItems
  var event = {
		setCurrentzRItem: function(currentzRItem){
      zRSettings.currentValue = zRItem.index(currentzRItem) + 1;
      if (zRSettings.remote){
        $.post(zRSettings.postTo, 
          { "z_rating": (zRSettings.multiplier*zRSettings.currentValue) }, 
          function(data){ 
            if(callback)
              callback(data);
          }
        );
      }else{
        $("#"+zRSettings.postTo).val((zRSettings.multiplier*zRSettings.currentValue));
        if(callback)
          callback(zRSettings.currentValue);
      }
    },

  	highlight: function(currentzRItem){
  		zRItem.lt(zRItem.index(currentzRItem)+1).addClass('z_highlighted').end();
  	},

  	empty: function() {
  		zRItem.filter('.z_selected').removeClass('z_selected').end();
  	  zRItem.filter('.z_highlighted').removeClass('z_highlighted').end();
  	},

  	reset: function(){
  		zRItem.lt(zRSettings.currentValue).addClass('z_selected').end();
  	}
	}        
	event.reset();
	return(this);	
}