var symantec = symantec || {};

symantec.forums = {
    solution_link_selector: 'a.solution-link',
    solved_button_selector: 'div.solved-button',
    comment_reply_selector: '.comment_reply a',
    ajax_endpoint_solution_set: 'admin/forums/solution-set',
    ajax_endpoint_solution_clear: 'admin/forums/solution-clear'
};

$(document).ready(function() {
    symantec.forums.setup_mark_as_solution();
    symantec.forums.setup_quoting();
    symantec.forums.setup_solution_clear();
    $('.solved-button', '#content-inner').parent().addClass('current-solution');
});

var solved_flag = false; // used to send data to Omniture only once

/**
 * Clears the current solved marker, replacing it with a mark as solved link
 */
symantec.forums.clear_solution_markup = function(current_nid) {
  // clear the solved marker on any pre-existing solution chosen during this page load
  var $current_solution = $('.current-solution');
  if ($current_solution.length) {
    $current_solution.empty().append($('#solution-mark-stub').html());
    var parent_comment_id_parts = $current_solution.parents('.comment-container').prev().attr('id').split('-');
    var parent_comment_id = parent_comment_id_parts[1];
    $('a', $current_solution).attr('id', 'mark-as-solution-' + current_nid + '-' + parent_comment_id);
    $current_solution.removeClass('current-solution');
    $(symantec.forums.solved_button_selector).show();
  }
}


/**
 *  Setup for the mark as solution feature
 */
symantec.forums.setup_mark_as_solution = function() {
  // add nodes with template markup for a solution or mark as solved link
  $('body').append('<div id="solution-solved-stub" style="display:none;"><div class="solved-button">SOLUTION<span class="solved-clear">(<a href="#">clear</a>)</span></div></div>');
  $('body').append('<div id="solution-mark-stub" style="display:none;"><a class="solution-link" >Mark as solution</a></div>');

  // event binding for mark as solution links
  $(this.solution_link_selector).unbind('click').click(function() {
    $(this).html('saving...');
    var clicked_link = $(this);

    var item_id = $(this).attr('id');
    var item_id_parts = item_id.split('-');
    var solved_nid = item_id_parts[3];
    var solved_cid = item_id_parts[4];
    var post_data = {nid: solved_nid, cid: solved_cid};

    symantec.forums.clear_solution_markup(solved_nid);

    // clear the solved class on any previously-solved comment on the page
    $('.comment-solved').removeClass('comment-solved');

    $.post(Drupal.settings.basePath + symantec.forums.ajax_endpoint_solution_set, post_data, function(data, textResponse) {
      $(symantec.forums.solved_button_selector).each(function() {
          $(this).fadeOut(); // snarky
      })

      // used to track mark-as-solved as an Omniture event
      if(!solved_flag) {
        solved_flag = true;
        if(s) {
          s.events = 'event54';
          s.linkTrackVars="channel,events,products,prop1,prop2,prop3,prop25,prop27,prop30,prop33,prop41,prop42,prop43,prop44,prop45,prop46,prop47,prop48,prop49,eVar26,eVar27,eVar28,eVar29,eVar32,eVar41,eVar42,eVar43,eVar50";
          s.linkTrackEvents="event54";
          s.tl(true, 'o', 'connect: solved_button_selector');
        }
      }

      clicked_link.parents('.comment-container').addClass('comment-solved');
      clicked_link.parent('.solution-wrapper').empty().append($('#solution-solved-stub').html()).addClass('current-solution');
      symantec.forums.setup_solution_clear(solved_nid);
      symantec.forums.setup_mark_as_solution();
    });

    return false;
  });
}

/**
 * Setup bindings for clearing current solution
 * @param int solved_nid
 *  The node whose solution should be cleared when a clear link is checked
 */
symantec.forums.setup_solution_clear = function(solved_nid) {
  $('.solved-clear a').click(function() {

    if (!solved_nid) {
      var item_id_parts = $(this).attr('id').split('-');
      solved_nid = item_id_parts[2];
    }

    var post_data = {
      nid: solved_nid
    };

    var clicked_link = $(this);

    $.post(Drupal.settings.basePath + symantec.forums.ajax_endpoint_solution_clear, post_data, function(data, textResponse) {
      var response = eval('(' + data + ')');
      if (response.status) {
        clicked_link.parents('.solution-wrapper').empty().append('<div class="solved-button">' + response.data + '</div>');
        $('.comment-solved').removeClass('comment-solved');
      } else {
        alert(response.data);
      }
    });

    return false;
  });
}

/**
 * Setup for the quoting feature in forums
 */
symantec.forums.setup_quoting = function() {
    $(this.comment_reply_selector + 'extra').click(function() {
       var $container = $(this).parents('.comment-container');
       var comment_text = $('div.content', $container).html();
       FCKeditorAPI.GetInstance('oFCK_1').SetHTML(comment_text);
       var target_offset = $('#edit-subject').offset().top;
       $('html, body').animate({scrollTop: target_offset}, 500);
       return false;
    });
}