
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();


SK.Pagination = {
  
  View: {
    displaySuccess: function(target, content){
      $(target).replaceWith(content)
    },
    
    displayError: function(target, content){
      $(target).replaceWith(content)
    }
  },

  handleSuccessFor: function(id) {
    return function(json_resp) {
      if (json_resp["status"] == "ok") {
        SK.Pagination.View.displaySuccess(id, json_resp["body"]);
      }
      else {
        SK.Pagination.View.displayError(id, json_resp["body"]);
      }
    };
  },
  
  handleErrorFor: function(id) {
    return function(req) {
      SK.Pagination.View.displayError(id, "<div id='tweets' class='module'><h2>Twitter comments</h2><p class='js_error'>Sorry, there was an unknown error.</p></div>");
    };
  },
  
  // SK.Pagination.init(target:'#someWrapper')
  init: function(options) {
    id = options["target"]
    $(id).find("div.pagination").children("a").each(function(i,a){
      $(a).bind("click", function(e){
        SK.core.ajaxifyActionAttribute($(a), "href");
        $(id).find(".ajax-indicator").show();
        
        $.ajax({
          type:     'GET',
          url:      $(a).attr("href"),
          success:  (SK.Pagination.handleSuccessFor(id)),
          error:    (SK.Pagination.handleErrorFor(id)),
          dataType: "json"
        });
        return false;
      })
    })
  }
  
}