/*
  comments engine
*/

// uses ajax call to load comments into div 
function load_comments(post_id, scroll){
  var container = $('comment_container_' + post_id);
  // $$('.comment_loader_' + post_id)[0].show();
  new Ajax.Updater(container, '/blog/get_comments',
  {
    onComplete:function(){
      // $$('.comment_loader_' + post_id)[0].hide();
      setup_comments(container,scroll);
    }, 
    onFailure:load_comments_failure,
    parameters: { 'post_id': post_id }
  });
}


// setup the comments
function setup_comments(container,scroll) {
  container.show();
  attachQuoteEvents(container);
  
  var frm = container.select('form')[0];
  if (!frm.hasClassName('form-self-manage')) {
    // use ajax to send the form?
    if (frm.hasClassName('form-ajax')) {
	    ajaxOnSubmitEvent(frm);
    } else {
	    standardOnSubmitEvent(frm);
    }
  }
  
  if(scroll)
    Effect.ScrollTo(container.id);
}

function attachQuoteEvents(container) {
  
  // attach click events to all of the quote comment links
  container.select('.quote_this_comment').each(function(elm){
    elm.observe('click',function(){
      
      // find the comment text in the dom
      var comment_text = elm.up().next().down('p').innerHTML.replace(/<br>/gi,'\n').replace(/(<([^>]+)>)/ig,"");

      // copy into quote box, show and scroll
      var quote_hidden = container.down('.quote_hidden');
      quote_hidden.value = comment_text;
      
      var quote_selector = container.down('.quote_selector');
      var quote_box = quote_selector.down('.quote_box textarea');
      quote_box.value = comment_text;
      quote_selector.show();
      
      var quote_display = quote_selector.down('.quote_display');
      
      Effect.ScrollTo(quote_box.up('.add_comment').id);
      
      // try and resize the textarea so that the quoted text does not have a scroll bar
      var strtocount = quote_box.value;
      var hard_lines = 1;
      var last = 0;
      while ( true ) {
        last = strtocount.indexOf("\n", last+1);
        hard_lines ++;
        if ( last == -1 ) break;
      }
      var soft_lines = Math.round(strtocount.length / (quote_box.cols-1));
      var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
      if ( hard ) soft_lines = hard_lines;
      quote_box.rows = soft_lines;
      
      // attach event so that when text is selected it creates a nice little quote box
      quote_box.observe('mouseup',function(){
        if(document.selection) {
          var selText = document.selection.createRange().text;          
        }
        else {
          var selText = (quote_box.value).substring(quote_box.selectionStart,quote_box.selectionEnd);          
        }
        quote_hidden.value = selText;
        quote_display.down().innerHTML = selText.replace(/\n/g,'<br/>');
        quote_display.show();
      });
      
      quote_box.observe('keydown',function(e){
        e.stop();
      });
      
      // attach close event to the small x
      quote_display.select('.close')[0].observe('click',function(){
        quote_hidden.value = '';
        quote_display.down().innerHTML = '';
        quote_display.hide();
      });
      
      // change the message box to say respond to this comment
      container.down('.add_message').down().value = 'Respond to this comment';
    })
  });
}

// if loading the comments fail
function load_comments_failure(){
  display_notification('There was a problem retrieving the comments for this post.');
}



function post_comment_onSubmit(frm,errors,custom_errors) {

  frm.select('.ajax-loader')[0].show();
}



// if posting a new comment is successful
function post_comment_onSuccess(t,frm) {
  var result = t.responseJSON;

  if (result.success)
  {
    if (result.is_spam) {
      display_notification('Your comment has been marked as spam and cannot be displayed. Our moderators have been notified and will check the content of the comment to confirm.');
      frm.select('.ajax-loader')[0].hide();      
    }
    else
      load_comments(result.post_id, false);
  }
  else
  {
    frm.select('.ajax-loader')[0].hide();
    
    if(result.error_code == 'already_posted')
    {
      display_notification('You have already posted in the last 3 minutes, please wait before posting another comment.');
    }
    else if(result.error_code == 'banned')
    {
      display_notification('You have been banned from posting content to this site, contact the site administrator if you feel you should not be banned.');
    }
    else
    {
      display_errors(result.error_code);
    }
    
  }
}

// if posting a new comment fails
function post_comment_onFailure(t,frm) {
  display_notification('Sorry, there was a problem posting your comment.');
  frm.select('.ajax-loader')[0].hide();
}
