var ajax_comment_loading = false;

function ajax_comments_loading(on) { 
// 20070220 ge : comment out this whole function - just makes the screen look jumpy
//	  if(on) {
//	  ajax_comment_loading = true;
//	  
//	  var f = $('commentform');
//	  new Insertion.Before(f, '<div id="ajax_comments_loading">Processing...</div>'); 
//	  f.submit.disabled = true; // disable submit
//	 // create loading
//	
//	  var l = $('ajax_comments_loading');
//	  new Effect.Appear(l, { beforeStart: function() { with(l.style) {
//		display = 'block';
//		margin = '0 auto';
//		width = '100px';
//		font = 'normal 12px Arial';
//		background = 'url(http://sonomalibrary.org/news/wp-content/plugins/ajax-comments/loading.gif) no-repeat 0 50%';
//		padding = '0';
//	  }}});
//	} else {
//	   new Effect.Fade('ajax_comments_loading', { afterFinish: function() { // hide loading
//		Element.remove('ajax_comments_loading'); 
//	  }});
//	
//	  ajax_comment_loading = false;
//	}
}

// 20070221 ge NOTE - special error handling revised from original plugin
// *** will not work unless you add these items from the tweaked aquafluid themes.
//  (1) comments template add <div id="errmsg" class="invisibility"></div>
//  (2) stylesheet add styles for these classes:
//		textarea.textbox, input.err_highlight, textarea.err_highlight
//		.visibility , .invisibility
// also NOTE - additional error detection/spam control added to wp-comments-post

// 20070215 ge add special error handling function
// very clunky switch statement - needs fixing
// if I can figure out how to set a flag value on error in the first place
function ajax_comments_error(errtext) {
  
  var highlight_field;
  var errormessage = document.getElementById("errmsg");
  errormessage.innerHTML = errtext;
  errormessage.className = "visibility";
	  
    switch (errtext) { 
	case 'Please fill in your name.':
		highlight_field = document.getElementById("author");
		break;
	case 'Please fill in your e-mail address.':
		highlight_field = document.getElementById("email");
		break;
	case 'The address you entered does not look like a valid e-mail address.  Please re-type.':
		highlight_field = document.getElementById("email");
		break;
	case 'Please type your feedback in the comments area.':
		highlight_field = document.getElementById("comment");
		break;
    	}
	
	if (highlight_field) {
		highlight_field.className = 'err_highlight';
		highlight_field.focus();
		}

  }

function rotate_auth_image() {
  var img = $('auth-image'), input = $('code');
  if(img) img.src += '?'+Math.random(); // Change AuthImage
  if(input) input.value = ''; // Reset Code
}

function find_commentlist() {
// 20070222 ge : comment out looking for existing comments
// as we are not displaying feedback received
//  var e = $('commentlist');
//  if(e == null) {
//    var e = document.getElementsByTagName('ol');
//    for(var i=0; i<e.length; i++)
//      if(e[i].className=='commentlist')
//        return e[i];
//  } else return e;
// 
// 20070222 also add class=commentlist to the ol created in this function.
// The stylesheets we are using operate on the class, not the id.

  /* commentslist doesn't exist (no posts yet)
  so create it above the commentform and return it */
  var f = $('commentform');
  new Insertion.Before(f, '<ol id="commentlist" class="commentlist"></ol>'); // create commentform
  return $('commentlist');
}


function ajax_comments_submit() {
  if(ajax_comment_loading) return false;

  ajax_comments_loading(true);
  var f = $('commentform'), ol = find_commentlist();
  
  // clear all effects in case this is the second time through the form
  var errormessage = document.getElementById("errmsg");
  errormessage.className = "invisibility";
  f.author.className = 'textbox';
  f.email.className = 'textbox';
  f.comment.className = 'textbox';
  
  new Ajax.Request('http://sonomalibrary.org/news/wp-content/plugins/ajax-comments/ajax-comments.scl?submit', {
    method: 'post',
    asynchronous: true,
    postBody: Form.serialize(f),
    onLoading: function(request) {
      request['timeout_ID'] = window.setTimeout(function() {
        switch (request.readyState) {
        case 1: case 2: case 3:
          request.abort();
          alert('Comment Error: Timeout\nThe server is taking a long time to respond. Try again in a few minutes.');
          break;
        }
      }, 25000);
    },
    onFailure: function(request) {

 	  // 20070215 ge : inline error message to replace alert()
     //alert((request.status!=406? 'Comment Error '+request.status+' : '+request.statusText+'\n' : '')+request.responseText);

	ajax_comments_error(request.responseText);
  	f.submit.disabled = false; // re-enable submit button
	  // end 20070215
    },
    onComplete: function(request) { ajax_comments_loading(false);
      window.clearTimeout(request['timeout_ID']);
//      rotate_auth_image(); // AuthImage
      if(request.status!=200) return;

      f.comment.value=''; // Reset comment

      new Insertion.Bottom(ol, request.responseText);
      var li = ol.lastChild, className = li.className, style = li.style;
 // 20070222 ge : remove afterFinish as both FF and IE complain about JavaScript error
 // ("setting a property that only has a getter")
 	new Effect.BlindDown(li);
 //     new Effect.BlindDown(li, {
 //       afterFinish: function() { li.className = className; li.style = style; }
 //     });
    }
  });
  return false;
}
