/*
 * WNCOutdoors.info UI

 * This script is free and unencumbered software released into the
 * public domain. It comes without any restrictions on what you may
 * do with it, and with no warranty whatsoever. Assume nothing works,
 * and you may be pleasantly surprised; and when it breaks, you get
 * to keep both pieces.
 * @requires: prototype.js
 * @requires: livePipe UI
*/
( function( WNCO, $, undefined ) {
  
  // Public object
  WNCO.Ratings = {
    
    params: {}, // This gets filled by calling page.
    
    submitRating: function() {
      var url = [ 'http:/', WNCO.cfg.hostname, 'data', WNCO.Ratings.params.type, WNCO.Ratings.params.id, 'ratings' ].join( '/' );
      new Ajax.Request( url, {
        method: 'POST',
        parameters: { value: WNCO.Ratings.params.value },
        onSuccess: function( r ){
          var ratingData = r.responseJSON;
          ['onestar','twostar','threestar','fourstar','fivestar'].each( function( cls, pos ){
            if($("ratings").hasClassName( cls )){ $("ratings").removeClassName( cls ); }
          });
          $("ratings").addClassName( ratingData.text );
          $('ratings').stopObserving();
          $("visitor-rating").update("<strong>Your Rating:</strong> " + ratingData["rating"]);
          $("avg-rating").update("<strong>Average rating:</strong> " + ratingData["avg"] + " (rated " + ratingData["count"] + " times)");
          cookieName = "WNCO-rating-" + WNCO.Ratings.params.type + "-" + WNCO.Ratings.params.id;
          WNCO.setCookie( cookieName, ratingData["rating"] );
        },
        onException: function( r, e ){ }
      });
    }
  }; // Public
}( window.WNCO = window.WNCO || {}, $ ) );

