$(document).ready(function(){
  //Initialize
  performerComment.init();
});

var performerComment = {
  /**
   * Run this function once for initializing and binding
   */
  init: function(){
    $("#commentSubmit").live('click', function(ev){
      ev.preventDefault();
      performerComment.addComment($(this));
    });

    //Check if this page has box for comments
    if(typeof($("#commentBox").attr('id')) !== 'undefined'){
      //Load comments
     performerComment.getComments();
    }
  },

  /**
   * Add a comment to item (most likely an article)
   * @param $this jQuery-object of trigger (most likely a button)
   */
  addComment: function(){
    var commentHolder = new Object();
        commentHolder.form = $("#makeComment");

    //Get parameters and values for comment
    commentHolder.sourceID = commentHolder.form.find('input[name="sourceID"]').val();
    if(typeof($("#commentName").attr('id')) === 'undefined'){
      commentHolder.commentatorType = 'site';
    } else {
      commentHolder.commentatorType = 'anonymous';
      if($("#commentName").val().length > 0){
        commentHolder.commentatorName = $("#commentName").val();
      } else {
        alert("Du har inte angivet ditt namn, fyll i det för att fortsätta.");
        return false;
      }
    }
    commentHolder.comment = commentHolder.form.find('#commentContent').val();
    if(commentHolder.comment.length > 0){
      performerComment.performAddComment(commentHolder);
    } else {
      alert("Du har inte skrivit någon kommentar, fyll i meddelande-fältet för att fortsätta.");
    }
  },

  performAddComment: function(commentHolder){
    //Validate before sending comment
    if(performerComment.validate(commentHolder)){
      //Comment is validated
    } else {
      alert("Någonting i formuläret stämmer inte, kontrollera dina uppgifter och försök igen.");
      return false;
    }

    var data = new Object();
        data.action = 'addComment';
        data.sourceID = commentHolder.sourceID;
        data.commentatorType = commentHolder.commentatorType;
        data.comment = commentHolder.comment;
        if(commentHolder.commentatorName){
          data.commentatorName = commentHolder.commentatorName;
        }

        var dataString = $.toJSON(data);

        $.post('ajax/comment.php', {data: dataString}, function(res){
            var obj = $.evalJSON(res);
            if(obj.success == 1){
              //Comment added succesfully
              alert("Tack, din kommentar har sparats.");
              performerComment.getComments();
            } else {
              //Error
              alert("Ett fel inträffade när din kommentar skulle sparas, vänligen försök igen.");
            }
        });
  },

  getComments: function(){
    var articleID = $('#sourceID').val();
    var data = new Object();
        data.action = 'getComments';
        data.articleID = articleID;

        var dataString = $.toJSON(data);

        $.post('ajax/comment.php', {data: dataString}, function(res){
            var obj = $.evalJSON(res);
            if(obj.success == 1){
              //Comment-template successfully loaded
              $("#commentItemHolder").html(obj.tpl);
              //Re-init wrShadow (performer gathers all necessary instances)
            } else {
              //Could not get the comments

            }
        });
  },

  /**
   * Validate a comment
   * @param $this jQuery-object of trigger (most likely a link)
   */
  validate: function(commentHolder){
    var isPassed = true;
    if(commentHolder.comment.length == 0){
      isPassed = false;
    }
    if(commentHolder.sourceID.length == 0){
      isPassed = false;
    }

    return isPassed;
  }
}
