text: // Generated by CoffeeScript 1.4.0
  var raty;
  
  $(function() {
    $(window).scroll(function() {
      if ($(this).scrollTop() !== 0) {
        return $("#toTop").fadeIn();
      } else {
        return $("#toTop").fadeOut();
      }
    });
    return $("#toTop").click(function() {
      return $("body,html").animate({
        scrollTop: 0
      }, 800);
    });
  });
  
  raty = {
    template: JST['company/comment'],
    init: function() {
      this.detect_elements();
      return this.bind_events();
    },
    detect_elements: function() {
      this.timeliness = $("#timeliness");
      this.comfort = $("#comfort");
      this.wifi = $("#wifi");
      this.empty_seating = $("#empty-seating");
      this.cleanliness = $("#cleanliness");
      this.form = $(".comment-form");
      this.comments = $(".comments-block");
      this.company = $("#company");
      this.avg_rate = $(".avg_rate");
      this.rating = $(".rating");
      return this.options = {
        success: this.showResponse
      };
    },
    bind_events: function() {
      this.init_avg_rate();
      this.create_rate();
      this.init_form_submit();
      this.get_comments();
      return this.init_votes();
    },
    init_votes: function() {
      return $(".btn-vote").live("click", function() {
        var element, sign, value,
          _this = this;
        element = $(this);
        value = parseInt(element.parent().find('h1.votes').html());
        sign = '+';
        if (element.hasClass('minus')) {
          sign = '-';
        }
        return $.ajax({
          url: SYS.baseUrl + 'company/vote',
          data: $.param({
            comment_id: element.parent().next().data('comment'),
            user_id: $('#user_id').val(),
            sign: sign
          }),
          type: 'POST',
          dataType: 'json',
          success: function(res) {
            if (res.text === "success") {
              if (element.hasClass('plus')) {
                element.next().html(value + 1);
              } else {
                element.prev().html(value - 1);
              }
            } else {
              console.log(res.data);
            }
            return element.parent().find('.btn-vote').attr("disabled", "disabled");
          }
        });
      });
    },
    init_avg_rate: function() {
      var company_id, me,
        _this = this;
      me = this;
      company_id = this.avg_rate.data("company");
      return $.ajax({
        url: SYS.baseUrl + 'compare/get_avg_rating',
        data: $.param({
          id: company_id
        }),
        type: 'POST',
        dataType: 'json',
        success: function(res) {
          if (res.text === "success") {
            me.avg_rate.raty({
              half: true,
              size: 24,
              starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
              starOff: SYS.baseUrl + "img/stars/star-off-big.png",
              starOn: SYS.baseUrl + "img/stars/star-on-big.png",
              readOnly: true,
              noRatedMsg: "Be the first to leave a review",
              hints: ['bad', 'poor', 'average', 'good', 'excellent'],
              score: res.data.rating
            });
            if (res.data.count) {
              return me.avg_rate.next().html("(" + res.data.count + " customer reviews)");
            }
          }
        }
      });
    },
    get_comments: function() {
      var me,
        _this = this;
      me = this;
      return $.ajax({
        url: SYS.baseUrl + 'company/get_comments',
        data: $.param({
          id: me.company.val()
        }),
        type: 'POST',
        dataType: 'json',
        success: function(res) {
          if (res.text === "success") {
            _.each(res.data, function(comment) {
              me.comments.append(me.template({
                comment: comment
              }));
              return me.rate_comment(comment);
            });
            if (!$('#user_id').val()) {
              return $('.btn-vote').attr("disabled", "disabled");
            }
          }
        }
      });
    },
    showResponse: function(responseText, statusText, xhr, $form) {
      var obj;
      obj = jQuery.parseJSON(responseText);
      if (obj.text === "success") {
        obj.data.date = "1 second ago";
        obj.data.votes = "0";
        raty.comments.prepend(raty.template({
          comment: obj.data
        }));
        raty.rate_comment(obj.data);
        return raty.form.remove();
      } else {
        return console.log(responseText);
      }
    },
    rate_comment: function(comment) {
      $("#rating" + comment.id).raty({
        half: true,
        size: 16,
        readOnly: true,
        starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
        starOff: SYS.baseUrl + "img/stars/star-off-big.png",
        starOn: SYS.baseUrl + "img/stars/star-on-big.png",
        score: comment.rating,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      $("#timeliness" + comment.id).raty({
        half: true,
        size: 16,
        readOnly: true,
        starHalf: SYS.baseUrl + "img/stars/star-half.png",
        starOff: SYS.baseUrl + "img/stars/star-off.png",
        starOn: SYS.baseUrl + "img/stars/star-on.png",
        score: comment.timeliness,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      $("#comfort" + comment.id).raty({
        half: true,
        size: 16,
        readOnly: true,
        starHalf: SYS.baseUrl + "img/stars/star-half.png",
        starOff: SYS.baseUrl + "img/stars/star-off.png",
        starOn: SYS.baseUrl + "img/stars/star-on.png",
        score: comment.comfort,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      $("#wifi" + comment.id).raty({
        half: true,
        size: 16,
        readOnly: true,
        starHalf: SYS.baseUrl + "img/stars/star-half.png",
        starOff: SYS.baseUrl + "img/stars/star-off.png",
        starOn: SYS.baseUrl + "img/stars/star-on.png",
        score: comment.wifi,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      $("#empty-seating" + comment.id).raty({
        half: true,
        size: 16,
        readOnly: true,
        starHalf: SYS.baseUrl + "img/stars/star-half.png",
        starOff: SYS.baseUrl + "img/stars/star-off.png",
        starOn: SYS.baseUrl + "img/stars/star-on.png",
        score: comment.empty_seating,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      return $("#cleanliness" + comment.id).raty({
        half: true,
        size: 16,
        readOnly: true,
        starHalf: SYS.baseUrl + "img/stars/star-half.png",
        starOff: SYS.baseUrl + "img/stars/star-off.png",
        starOn: SYS.baseUrl + "img/stars/star-on.png",
        score: comment.cleanliness,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
    },
    init_form_submit: function() {
      var _this = this;
      this.form.submit(function(e) {
        var el;
        el = $(e.currentTarget);
        if (_this.form.valid()) {
          el.ajaxSubmit(_this.options);
        }
        return false;
      });
      return this.form.validate({
        rules: {
          "name": {
            minlength: 2,
            required: true
          },
          "message": {
            minlength: 40,
            required: true
          },
          "title": {
            minlength: 3,
            required: true
          }
        }
      });
    },
    create_rate: function() {
      this.rating.raty({
        half: true,
        size: 24,
        starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
        starOff: SYS.baseUrl + "img/stars/star-off-big.png",
        starOn: SYS.baseUrl + "img/stars/star-on-big.png",
        score: 0,
        target: '#rating',
        targetType: 'number',
        targetKeep: true,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      this.timeliness.raty({
        half: true,
        size: 24,
        starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
        starOff: SYS.baseUrl + "img/stars/star-off-big.png",
        starOn: SYS.baseUrl + "img/stars/star-on-big.png",
        score: 0,
        target: '#timeliness-input',
        targetType: 'number',
        targetKeep: true,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      this.comfort.raty({
        half: true,
        size: 24,
        starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
        starOff: SYS.baseUrl + "img/stars/star-off-big.png",
        starOn: SYS.baseUrl + "img/stars/star-on-big.png",
        score: 0,
        target: '#comfort-input',
        targetType: 'number',
        targetKeep: true,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      this.wifi.raty({
        half: true,
        size: 24,
        starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
        starOff: SYS.baseUrl + "img/stars/star-off-big.png",
        starOn: SYS.baseUrl + "img/stars/star-on-big.png",
        score: 0,
        target: '#wifi-input',
        targetType: 'number',
        targetKeep: true,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      this.empty_seating.raty({
        half: true,
        size: 24,
        starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
        starOff: SYS.baseUrl + "img/stars/star-off-big.png",
        starOn: SYS.baseUrl + "img/stars/star-on-big.png",
        score: 0,
        target: '#empty-seating-input',
        targetType: 'number',
        targetKeep: true,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
      return this.cleanliness.raty({
        half: true,
        size: 24,
        starHalf: SYS.baseUrl + "img/stars/star-half-big.png",
        starOff: SYS.baseUrl + "img/stars/star-off-big.png",
        starOn: SYS.baseUrl + "img/stars/star-on-big.png",
        score: 0,
        target: '#cleanliness-input',
        targetType: 'number',
        targetKeep: true,
        noRatedMsg: "Be the first to leave a review",
        hints: ['bad', 'poor', 'average', 'good', 'excellent']
      });
    }
  };
  
  $(document).ready(function() {
    return raty.init();
  });
  