var ajax_comment_error = false;
var mySlide = null;

function ajax_comments_loading(on) { 
    $('comment').getParent().toggleClass('cloading');
    $$('#commentform input', '#comment').each(function(el){el.setProperty('disabled', on)});
}

function ajax_comments_error(on, text) {
  if(on) {
    if (ajax_comment_error) {
      $('ajax_comments_error').setHTML(text);
      var fx = $('ajax_comments_error').effect('background-color', {transition: Fx.Transitions.Quart.easeOut});
      fx.start('#FF0000').chain(function() {this.start.delay(1000, this, '#FF9980');});
    } else {
      ajax_comment_error = true;
      create_error_message();
      $('ajax_comments_error').setHTML(text);
      $('ajax_comments_error').addClass('ajax_comments_error');      
      mySlide.toggle();
    }
  } else {
    if (ajax_comment_error)
      mySlide.toggle();
    ajax_comment_error = false;
  }
}

function find_commentlist() {
  var e = $('comments');
  if(e != null) return e;
  var comments = new Element('ol', {
      'class': 'commentlist',
      'id': 'comments'
  });
  comments.injectTop('commentscontainer');
  return $('comments');
}

function rotate_auth_image() {
  if (!$('mycaptcha')) return;
  var url = '';  
  $$('p#mycaptcha img').each(function(image) { url = image.getProperty('src'); image.setProperty('src', url+'rmd='+Math.random());});
  $('securitycode').setProperty('value', '');
}

function destroy_error_message() {
  if ($('ajax_comments_error') && !ajax_comment_error) {
      $('ajax_comments_error').getParent().remove();
      mySlide = null;
  }  
}

function create_error_message() {
  if (!$('ajax_comments_error')) {
    var errcont = new Element('div', {
         'id': 'ajax_comments_error'
    });
    errcont.injectBefore('sendbuttons');
    mySlide = new Fx.Slide('ajax_comments_error', {onComplete:destroy_error_message});
    mySlide.hide();
  }
}

function ajax_comments_submit(e) {
  e = new Event(e);
  var close_txt = ' <a href="#" onclick="ajax_comments_error(false, \'\'); return false;">'+langClose+'</a>';
  var ol = find_commentlist();
  new Ajax(urlToFile+'?submit', {
    method: 'post',
    async: true,
    autoCancel: true,
    data: $('commentform').toQueryString(),
    onRequest: function() {
      ajax_comments_loading(true);
    },
    onFailure: function(request) {
      var response = request.responseText;
      if(response == '')
        response = langDefaultError;
      ajax_comments_error(true, (request.status!=406? langError+request.status+' : '+request.statusText+'\n' : '')+response+close_txt);
      ajax_comments_loading(false);
      rotate_auth_image();
    },
    onSuccess: function(response) {
      ajax_comments_error(false, '');
      ajax_comments_loading(false);
      rotate_auth_image();
      $('comment').setProperty('value', '');
      var lastComment = ol.getLast();
      var newLi = new Element('li');
      var newDiv = new Element('div');
	  if (lastComment == null) {
		  newLi.injectInside(ol);
	  } else {
		  newLi.injectAfter(lastComment);
	  }
      newDiv.injectInside(newLi);
      newDiv.setHTML(response);
      var mySlide = new Fx.Slide(newDiv);
      mySlide.hide();
      mySlide.slideIn();
    }
  }).request();
  e.stop();
}

function ajax_comments_init() {
  var form = $('commentform');
  if (form) {
    form.addEvent('submit', ajax_comments_submit.bindWithEvent(form));  
    $('comment').addEvent('keypress', function (e) {
      if (((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)) 
          ajax_comments_submit(e);
    });
  }
}