// Comment Javascript for the video page. See app/views/video/_commentstg.html.erb.
function setup_comment_handlers() {
  // Reply link
  $j('#comment-listing .reply-link').click(function() {
    // Remove any existing reply form
    $j('#comment-listing .comment-reply-form').remove();
    // Add new form below the Reply link
    var item = $j(this).parent().parent();
    item.append($j('#reply-form-template').html());

    var at_reply_username = $j(this).attr('data-username');
    if (at_reply_username.length > 0) {
      $j('textarea[name=message]', item).val("@" + at_reply_username + " ");
    }
    
    // Copy audit_id to the form
    item.find('.audit-id').val($j(this).attr('data-audit-id'));

    item.find('.comment-reply-form').submit(function() {
      post_comment($j(this).find('.comment-reply-button'), $j(this).serialize(), item.find('.audit-id').val());
      return false;
    });

    return false;
  });

  // Flag link
  $j('#comment-listing .flag-link').click(function() {
    var theLI = $j(this);
    $j.post('/video/vote_down/' + theLI.attr('data-audit-id'), function(result) {
      theLI.html('This comment is flagged to be hidden');
    }, 'text');

    return false;
  });

  // Next and Prev links
  $j('.prev_comments').click(function() {
    $j('#comment-listing').css({opacity:0.5});
    $j('#comments-holder').load('/video/get_activity/' + $vodpod_video_id,
      {page: (parseInt($j('#current_comments_page').val())-1), new_format:true },
      function() {
        setup_comment_handlers();
      }
    );
    return false;
  });

  $j('.next_comments').click(function() {
    $j('#comment-listing').css({opacity:0.5});
    $j('#comments-holder').load('/video/get_activity/' + $vodpod_video_id,
      {page: (parseInt($j('#current_comments_page').val())+1), new_format:true },
      function() {
        setup_comment_handlers();
      }
    );
    return false;
  });

  // video page highlight reply, flag and hidden links

  $j("#pod-comments .topic-element, #pod-comments .hidden-message").hover(function(){
    $j(this).addClass("active");
  }, function(){
    $j(this).removeClass("active");
  });

  // show hidden comment
  $j("#pod-comments .hidden-message a.view-hidden").click(function(){
    $j(this).parent(".hidden-message").hide().next(".topic-element").show();
    return false;
  });

  // video page comment reply
  $j("#pod-comments .reply-link").click(function(){
    $j(this).closest(".info").find("form").show();
    return false;
  });


};

function post_comment(thebutton, post_data, target_audit_id) {
  $j.ajax({
    url: '/video/comment/' + $vodpod_video_id,
    data: post_data,
    dataType: 'html',
    type: 'post',
    success: function(content) {
      $j(thebutton).restore_saver();
      $j('textarea[name="message"]').val('')
      $j('#comments-holder').html(content);
      target_item = $j('#comment-listing li:first');
      if  (target_audit_id) {
        // Find a particular comment thread to highlight
        target_item = $j('#comment_' + target_audit_id).parent();
      }
      target_item.css({backgroundColor:'#fdffef'}).hide().fadeIn(1200, function() {$j(this).css({background:'none'})});
      setup_comment_handlers();
    },
    error: function(request, status, error) {
      $j(thebutton).restore_saver();
      alert(request.responseText);
    }
  });
}

$j(function() {
  setup_comment_handlers();

  // setup new comment form handler
  $j("#add_comment_form").submit(function(e) {
    e.preventDefault();

    var theform = $j(this);
    var thebutton = theform.find('.button-saver');

    // Valdiate guest comment fields
    if (theform.find('textarea').val() == '') {
      alert("Please enter a comment");
      thebutton.restore_saver();
      return false;
    }
    if (theform.find('[name="name"]').val() == '') {
      alert("Please enter your name");
      thebutton.restore_saver();
      return false;
    }
    if (theform.find('[name="gopher"]').val() == '') {
      alert("Please enter your email address");
      thebutton.restore_saver();
      return false;
    }
    post_comment(thebutton, theform.serialize());
  });
});
