var RatingManager = {
  cache: new Cache(),
  options: {
    url: "/rate.sv",
    parameters: ""
  },
  save: function(B, A, E, C) {
    RatingManager.cache.put(A, E);
    if (B != null && B != "") {
      var D = "rateAction=doRateScore&rateMovie=" + A + "&rateScore=" + E + "&rateSource=" + C + RatingManager.options.parameters;
      new Ajax.Request(RatingManager.options.url, {
        parameters: D
      });
    }
  },
  find: function(A) {
    return RatingManager.cache.get(A);
  }
};
var ReviewManager = {
  cache: new Cache(),
  options: {
    url: "/rate.sv",
    parameters: ""
  },
  save: function(C, B, A, D) {
    ReviewManager.cache.put(B, A);
    if (C != null && C != "") {
      var E = "rateAction=doRateReview&rateMovie=" + B + "&rateComment=" + encodeURIComponent(A) + "&rateSource=" + D + RatingManager.options.parameters;
      new Ajax.Request(ReviewManager.options.url, {
        parameters: E
      });
    }
  },
  find: function(A) {
    return ReviewManager.cache.get(A);
  }
};
var RatingWidget = Class.create();
RatingWidget.prototype = {
  initialize: function(B, A, D, C) {
    this.user = B;
    this.movie = A;
    this.score = D;
    this.source = C;
    this.stars = new RatingStars(this);
    this.buttons = new RatingButtons(this);
    if ($("clear" + this.movie) != null) {
      Event.observe($("clear" + this.movie), "click", this.onClear.bind(this));
    }
  },
  onRate: function(A) {
    if (this.score == A) {
      this.score = 0;
    } else {
      this.score = A;
    }
    RatingManager.save(this.user, this.movie, this.score, this.source);
    this.stars.paint(this.score);
    this.buttons.paint(this.score);
  },
  onClear: function() {
    this.score = 0;
    RatingManager.save(this.user, this.movie, this.score, this.source);
    this.stars.paint(this.score);
    this.buttons.paint(this.score);
  }
};
var RatingStars = Class.create();
RatingStars.prototype = {
  initialize: function(A) {
    this.widget = A;
    Event.observe("stars" + this.widget.movie, "mousemove", this.onMousemove.bindAsEventListener(this));
    Event.observe("stars" + this.widget.movie, "mouseout", this.onMouseout.bindAsEventListener(this));
    Event.observe("stars" + this.widget.movie, "click", this.onClick.bindAsEventListener(this));
  },
  onMousemove: function(A) {
    var B = this.map(A);
    this.paint(B);
  },
  onMouseout: function(A) {
    this.paint(this.widget.score);
  },
  onClick: function(A) {
    var B = this.map(A);
    this.widget.onRate(B);
  },
  map: function(E) {
    var D = Position.cumulativeOffset(Event.element(E));
    var C = Event.element(E);
    var A = 0;
    if (E.offsetX) {
      A = E.offsetX;
    } else {
      if (C.offsetX != null) {
        var B = C.offsetX;
        A = E.layerX - B;
      } else {
        if (E.layerX) {
          var B = Position.cumulativeOffset(C)[0];
          A = E.layerX - B;
          C.offsetX = B;
        }
      }
    }
    if ($R(0, 7).include(A)) {
      return 6;
    } else {
      if ($R(8, 14).include(A)) {
        return 1;
      } else {
        if ($R(15, 22).include(A)) {
          return 7;
        } else {
          if ($R(23, 29).include(A)) {
            return 2;
          } else {
            if ($R(30, 37).include(A)) {
              return 8;
            } else {
              if ($R(38, 44).include(A)) {
                return 3;
              } else {
                if ($R(45, 52).include(A)) {
                  return 9;
                } else {
                  if ($R(53, 59).include(A)) {
                    return 4;
                  } else {
                    if ($R(60, 66).include(A)) {
                      return 10;
                    } else {
                      if ($R(67, 74).include(A)) {
                        return 5;
                      } else {
                        return - 1;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  paint: function(A) {
    switch (A) {
    case 1:
      $("stars" + this.widget.movie).src = "/static/images/rating/1.0.gif";
      break;
    case 2:
      $("stars" + this.widget.movie).src = "/static/images/rating/2.0.gif";
      break;
    case 3:
      $("stars" + this.widget.movie).src = "/static/images/rating/3.0.gif";
      break;
    case 4:
      $("stars" + this.widget.movie).src = "/static/images/rating/4.0.gif";
      break;
    case 5:
      $("stars" + this.widget.movie).src = "/static/images/rating/5.0.gif";
      break;
    case 6:
      $("stars" + this.widget.movie).src = "/static/images/rating/0.5.gif";
      break;
    case 7:
      $("stars" + this.widget.movie).src = "/static/images/rating/1.5.gif";
      break;
    case 8:
      $("stars" + this.widget.movie).src = "/static/images/rating/2.5.gif";
      break;
    case 9:
      $("stars" + this.widget.movie).src = "/static/images/rating/3.5.gif";
      break;
    case 10:
      $("stars" + this.widget.movie).src = "/static/images/rating/4.5.gif";
      break;
    default:
      $("stars" + this.widget.movie).src = "/static/images/rating/0.0.gif";
    }
  }
};
var RatingButtons = Class.create();
RatingButtons.prototype = {
  initialize: function(A) {
    this.widget = A;
    if ($("wts" + this.widget.movie)) {
      Event.observe("wts" + this.widget.movie, "click", this.onWtsClick.bindAsEventListener(this));
    }
    if ($("ni" + this.widget.movie)) {
      Event.observe("ni" + this.widget.movie, "click", this.onNiClick.bindAsEventListener(this));
    }
  },
  onWtsClick: function(A) {
    this.widget.onRate(12);
  },
  onNiClick: function(A) {
    this.widget.onRate(11);
  },
  paint: function(A) {
    switch (A) {
    case 11:
      if ($("ni" + this.widget.movie)) {
        $("ni" + this.widget.movie).src = "/static/images/rating/ni.on.gif";
      }
      if ($("wts" + this.widget.movie)) {
        $("wts" + this.widget.movie).src = "/static/images/rating/ws.off.gif";
      }
      break;
    case 12:
      if ($("ni" + this.widget.movie)) {
        $("ni" + this.widget.movie).src = "/static/images/rating/ni.off.gif";
      }
      if ($("wts" + this.widget.movie)) {
        $("wts" + this.widget.movie).src = "/static/images/rating/ws.on.gif";
      }
      break;
    default:
      if ($("ni" + this.widget.movie)) {
        $("ni" + this.widget.movie).src = "/static/images/rating/ni.off.gif";
      }
      if ($("wts" + this.widget.movie)) {
        $("wts" + this.widget.movie).src = "/static/images/rating/ws.off.gif";
      }
    }
  }
};
var ReviewWidget = Class.create();
ReviewWidget.prototype = {
  initialize: function(B, A, C) {
    this.user = B;
    this.movie = A;
    this.source = C;
    Event.observe($("comment" + this.movie), "focus", this.onFocus.bindAsEventListener(this));
    if ($("save" + this.movie) != null) {
      Event.observe($("save" + this.movie), "click", this.onSave.bindAsEventListener(this));
    } else {
      Event.observe($("comment" + this.movie), "blur", this.onSave.bindAsEventListener(this));
    }
    if ($("clear" + this.movie) != null) {
      Event.observe($("clear" + this.movie), "click", this.onClear.bindAsEventListener(this));
    }
  },
  onFocus: function(A) {
    if ($("save" + this.movie) != null) {
      $("save" + this.movie).src = "/static/images/bt_save_ani.gif";
    }
  },
  onSave: function(A) {
    if ($("save" + this.movie) != null) {
      $("save" + this.movie).src = "/static/images/bt_saved.gif";
    }
    ReviewManager.save(this.user, this.movie, $F("comment" + this.movie), this.source);
  },
  onClear: function(A) {
    $("comment" + this.movie).value = "";
    ReviewManager.save(this.user, this.movie, $F("comment" + this.movie), this.source);
  }
};
var Images = {
  cache: function() {
    var J = new Image();
    J.src = "/static/images/rating/0.0.gif";
    var I = new Image();
    I.src = "/static/images/rating/1.0.gif";
    var H = new Image();
    H.src = "/static/images/rating/2.0.gif";
    var G = new Image();
    G.src = "/static/images/rating/3.0.gif";
    var F = new Image();
    F.src = "/static/images/rating/4.0.gif";
    var E = new Image();
    E.src = "/static/images/rating/5.0.gif";
    var D = new Image();
    D.src = "/static/images/rating/0.5.gif";
    var C = new Image();
    C.src = "/static/images/rating/1.5.gif";
    var B = new Image();
    B.src = "/static/images/rating/2.5.gif";
    var A = new Image();
    A.src = "/static/images/rating/3.5.gif";
    var M = new Image();
    M.src = "/static/images/rating/4.5.gif";
    var L = new Image();
    L.src = "/static/images/rating/ni.on.gif";
    var K = new Image();
    K.src = "/static/images/rating/ws.on.gif";
  }
};
Event.observe(window, "load", Images.cache);
