/*
 * This file is copyright by Indymedia linksunten
 * Based on a comment moderation module by Kyle Cunningham
 * It is licensed under the GNU GPL v2.
 * See LICENSE.txt for further licensing details.
 */

var comments = [];
var lastcomment = null;


if (Drupal.jsEnabled) {
  $(function() {
    unfoldComment();
  });
  $(function() {
    foldComment();
  });
}


function unfoldComment() {
  $('.comment-folded .comment-folded-subject a').click(function(){

    lastcomment = this;

    // String processing to get the comment ID
    var path = [];
    path = this.href.split('/')

    // Retrieve the last section of the path
    var pathstring = path[path.length - 1];

    // path[0] will now contain the comment ID
    path = pathstring.split('#');

    // Do the JSON call to the correct path
    $.getJSON(Drupal.settings.basePath + 'view-comment/unfold/' + path[0], {}, ratingDisplayCommentUnfold);

    return false;
  });
}


function ratingDisplayCommentUnfold(data, textStatus) {
  $(lastcomment).parents('.comment-folded').hide().replaceWith(data.comment);
  setRatingEvents(); // refresh events for rating comments
  foldComment(); // refresh fold event
  Drupal.behaviors.flagLink(lastcomment);
}


function foldComment() {
  $('.comment-unfolded .comment-unfolded-subject a').click(function(){

    lastcomment = this;

    // String processing to get the comment ID
    var path = [];
    path = this.href.split('/')

    // Retrieve the last section of the path
    var pathstring = path[path.length - 1];

    // path[0] will now contain the comment ID
    path = pathstring.split('#');

    // Do the JSON call to the correct path
    $.getJSON(Drupal.settings.basePath + 'view-comment/fold/' + path[0], {}, ratingDisplayCommentFold);

    return false;
  });
}


function ratingDisplayCommentFold(data, textStatus) {
  $(lastcomment).parents('.comment-unfolded').hide().replaceWith(data.comment);
  unfoldComment(); // refresh unfold event
}