(function($) {
  
  if(typeof TRYA === 'undefined') {
      TRYA = {};
  }
  
  TRYA.reviewRating = {
    fadeIn: function(job) {
      $('#review_rating_fadeout_' + job).fadeOut('slow');
      $('#review_rating_fadeout_up_' + job).fadeOut('slow');
      $('#review_rating_fadeout_down_' + job).fadeOut('slow');
      TRYA.reviewRating.closeReasonForm(job);
      $('#item_review_' + job).bind("mouseover", function() {
        $('#review_rating_fadeout_' + job).hide();
        $('#review_rating_fadeout_up_' + job).hide();
        $('#review_rating_fadeout_down_' + job).hide();
      });
    },
    makeReadOnlyForIE: function() {
      if(jQuery.browser.msie) {
        $("#job_wrapper_<%= job.id %>").removeClass("review_rating_wrapper").addClass("review_rating_wrapper_readonly");
        $("#review_rating_edit_<%= job.id %>").addClass("hidden");
      }
    },
    openReasonForm: function(job) {
      $("#review_rating_reason_" + job).show();
      $("#item_review_" + job).addClass("opened_reason_form");
      $("#rating_reason_" + job).focus();
      $("#job_wrapper_" + job).addClass("review_rating_wrapper_opened");
      TRYA.reviewRating.showBackground(job);
    },
    closeReasonForm: function(job) {
      $("#item_review_" + job).removeClass("opened_reason_form");
      $("#review_rating_reason_" + job).hide();
      $("#job_wrapper_" + job).removeClass("review_rating_wrapper_opened");
      TRYA.reviewRating.hideBackground(job);
    },
    showBackground: function(job) {
      var width = $(document).width();
      var height = $(document).height();
      var target = $("#review_rating_background_" + job).show();
      if ($.browser.msie) {
        var element = $("#job_wrapper_" + job);
        var leftShift = Math.ceil(element.offset().left);
        var topShift = Math.ceil(element.offset().top);
        target.css("left", "-"+leftShift+"px").css("top", "-"+topShift+"px");
      }
      target.css("width", ""+width+"px").css("height", ""+height+"px").click(function() {
        TRYA.reviewRating.closeReasonForm(job);
      });
    },
    hideBackground: function(job) {
      $("#review_rating_background_" + job).hide();
    },
    formSubmitted: function(element, job) {
      form = $(element);
      $.ajax({
        type: "POST",
        url: form.attr('action'),
        success: function(data, status) {
          $("#item_review_" + job).html(data);
        },
        error: TRYA.reviewRating.ajaxCallbackError,
        data: form.serializeArray(),
        dataType: "script"
      });
      return false;
    },
    ajaxCallbackError: function(data, status) {
      $("#popup_box").html(data.responseText);
    },
    enableReasonValidator: function(job) {
      $("#rating_reason_" + job).keyup(function() {
        $("#rating_reason_submit_" + job).attr("disabled", !TRYA.reviewRating.isReasonValid($("#rating_reason_" + job).val()));
      }).keyup();
    },
    isReasonValid: function(value) {
      return(value.length >= 5);
    }
  }
  
})(jQuery);