SK.Comments = {
  View: {
    hideForms: function() {
      $('#reply-comment').hide().appendTo($('#invisible_section'));
    },

    showForm: function(id) {
      SK.Comments.View.hideError();
      if (id == "comment-0") {
      }
      else {
        $('#reply-comment').show().appendTo($('#'+id+' > .comment_form_target'));
        if ($('#reply-comment').attr('action') == "") {
          action = $('#reply-comment').attr('rel')
        } else {
          action = $('#reply-comment').attr('action')
        }
          
        $('#reply-comment').attr('action', action.replace(/\/\//, '/'+id.split("-")[1]+'/'))
        $('#reply-comment').removeAttr('rel')
      }
    },

    showSpinner: function(id) {
      SK.Comments.spinner.prependTo($('#'+id)).show();
    },

    hideSpinner: function() {
      SK.Comments.spinner.appendTo($('#invisible_section')).hide();
    },

    displayError: function(id, html) {
      $('#'+id+' > .comment_form_target .error').html(html).show();
    },

    hideError: function() {
      $('.comment_form_target .error').hide();
    },

    addChild: function(id, html) {
      if ($('#'+id+' > ol.comments').length == 0) {
        $('#'+id).append('<ol class="comments"></ol>');
      }
      $('#'+id+' > ol.comments').prepend(html);
      $('#'+id+' > ol.comments > .comment:first > .footer .reply_comment_link').click(stopAnd(SK.Comments.Controller.handleReplyClick));
    },

    getElements: function() {
      SK.Comments.spinner = $('#comment_spinner');
      SK.Comments.newCommentForm = $('#new-comment');
      SK.Comments.newCommentLink = $('#new-comment_link');
    },

    form: function(id) {
      if (id == "comment-0") {
        return $('#new-comment');
      }
      else {
        return $('#reply-comment');
      }
    }
  },

  Controller: {
    handleReplyClick: function(e) {
      e.preventDefault();
      
      var id = $(e.target).parents('.comment').attr('id');
      SK.Comments.View.showForm(id);
      $('#reply_comment_content').val("")
    },

    handleSuccessFor: function(id) {
      return function(json_resp) {
        if (json_resp["status"] == "ok") {
          SK.Comments.View.addChild(id, json_resp["body"]);
          SK.Comments.View.hideSpinner();
          SK.Comments.View.hideError();
        }
        else {
          SK.Comments.View.displayError(id, json_resp["message"]);
          SK.Comments.View.hideSpinner();
        }
      };
    },

    handleErrorFor: function(id) {
      return function(req) {
        SK.Comments.View.displayError(id, "Sorry, there was an unknown error.");
        SK.Comments.View.hideSpinner();
      };
    },

    handleSubmitForm: function(e) {
      var id;
      if (e.target.id == "new-comment") {
        id = "comment-0";
      } else {
        id = e.target.parentNode.parentNode.id;
      }
      SK.Comments.Controller.submitForm(id);

      if (id = "comment-0") {
        $('#new_comment_content').val("")
        $('#rating').val("")
      	if (SK.rating != undefined) {
          SK.rating.setRating(null);
      	}
      }
    },

    submitForm: function(id) {
      SK.Comments.View.hideForms();
      SK.Comments.View.showSpinner(id);
      var form = SK.Comments.View.form(id);
      
      $.ajax({
        type:     'POST',
        url:      form.attr("action") + "?format=js",
        data:     form.serialize(),
        success:  (SK.Comments.Controller.handleSuccessFor(id)),
        error:    (SK.Comments.Controller.handleErrorFor(id)),
        dataType: "json"
      });
    }
  },

  init:function() {
    $(document).ready(function() {
      SK.Comments.View.hideForms();
      SK.Comments.View.getElements();

      $('.reply_comment_link').click(SK.Comments.Controller.handleReplyClick);
      $('#cancel_reply_comment_link').click(stopAnd(SK.Comments.View.hideForms));

      $('#new-comment').submit(stopAnd(SK.Comments.Controller.handleSubmitForm));
      $('#reply-comment').submit(stopAnd(SK.Comments.Controller.handleSubmitForm));
    });
  }
};

SK.Comments.init();
