
/* manage the review rating events */
$(document).ready(function() {
  //events for stars
  $('#divStarRating').ratings(5).bind('ratingchanged', function(event, data) {
    $('#rating').attr('value', data.rating);
  });
  
  //show/hide events for add review form
  $('#addReviewLnk').click(function() {
    $('#reviewDiv').slideToggle('slow');
  });
  
  //add review button click
  $('#addReviewButton').click(function() {
    try {
      var addParams = new Object();//used for building JSON object
      //validation
      var product_id = parseInt($('#product_id').val());
      if(isNaN(product_id))
        throw 'Malformed Product ID\nPlease refresh this page and try again';
      else
        addParams.product_id = product_id;
      
      var rating = parseInt($('#rating').val());
      if(isNaN(rating) || rating < 1 || rating > 5) {
        throw 'You MUST select a Product Rating of at least 1 and no greater than 5';
      } else {
        addParams.rating = rating;
      }
      
      var customer_name = $('#customer_name').val();
      if(customer_name.toString() == '' || customer_name.toString().length < 1) {
        $('#customer_name').focus();
        throw 'You MUST enter Your Name';
      } else {
        addParams.customer_name = customer_name;
      }
      
      var title = $('#title').val();
      if(title.toString() == '' || title.toString().length < 1) {
        $('#title').focus();
        throw 'You MUST enter a Review Title';
      } else {
        addParams.title = title;
      }
      
      var body = $('#body').val();
      if(body.toString() == '' || body.toString().length < 1) {
        $('#body').focus();
        throw 'You MUST enter some Review Content';
      } else {
        addParams.body = body;
      }
      
      //OK, all should be fine so AJAX submit the form!
      var addString = $.toJSON(addParams);
      $.post('/page/add-product-review', {data: addString}, function(res) {
        $('#reviewDiv').html(res);//replace form with response
        $('#addReviewLnk').attr('style', 'display: none;');//hide add link
        return false;
      });
    } catch(err) {
      window.alert('Error Adding Review:\nMessage: '+err.toString()+'\n');
      return false;
    }
  });
});

/* main function to handle the review rating events */
jQuery.fn.ratings = function(stars, initialRating) {

  //Save  the jQuery object for later use.
  var elements = this;
  
  //Go through each object in the selector and create a ratings control.
  return this.each(function() {
  
    //Make sure intialRating is set.
    if(!initialRating)
      initialRating = 0;
      
    //Save the current element for later use.
    var containerElement = this;
    
    //grab the jQuery object for the current container div
    var container = jQuery(this);
    
    //Create an array of stars so they can be referenced again.
    var starsCollection = Array();
    
    //Save the initial rating.
    containerElement.rating = initialRating;
    
    //Set the container div's overflow to auto.  This ensure it will grow to
    //hold all of its children.
    container.css('overflow', 'auto');
    
    //create each star
    for(var starIdx = 0; starIdx < stars; starIdx++) {
      
      //Create a div to hold the star.
      var starElement = document.createElement('div');
      
      //Get a jQuery object for this star.
      var star = jQuery(starElement);
      
      //Store the rating that represents this star.
      starElement.rating = starIdx + 1;
      
      //Add the style.
      star.addClass('jquery-ratings-star');
      
      //Add the full css class if the star is beneath the initial rating.
      if(starIdx < initialRating) {
        star.addClass('jquery-ratings-full');
      }
      
      //add the star to the container
      container.append(star);
      starsCollection.push(star);
      
      //hook up the click event
      star.click(function() {
        //When clicked, fire the 'ratingchanged' event handler.  Pass the rating through as the data argument.
        elements.triggerHandler("ratingchanged", {rating: this.rating});
        containerElement.rating = this.rating;
      });
      
      star.mouseenter(function() {
        //Highlight selected stars.
        for(var index = 0; index < this.rating; index++) {
          starsCollection[index].addClass('jquery-ratings-full');
        }
        //Unhighlight unselected stars.
        for(var index = this.rating; index < stars; index++) {
          starsCollection[index].removeClass('jquery-ratings-full');
        }
      });
      
      container.mouseleave(function() {
        //Highlight selected stars.
        for(var index = 0; index < containerElement.rating; index++) {
          starsCollection[index].addClass('jquery-ratings-full');
        }
        //Unhighlight unselected stars.
        for(var index = containerElement.rating; index < stars ; index++) {
          starsCollection[index].removeClass('jquery-ratings-full');
        }
      });
    }
  });
};
