// Ajax Comment Posting
// WordPress plugin
// version 1.3
// author: regua
// http://regua.biz

jQuery(document).ready(function($){

  jQuery.noConflict();

// If you want to use Google Analytics with ACP, fill in your
// GA tracker code in between the quotation marks below.
// The code should look like UA-XXXXXX-XX, with Xs being numbers.
// Make sure the line ends with a semi-colon.
  var google_analytics = "";
// To disable the use of Google Analytics with ACP, either remove the line
// above or change its value to "".

// -------------------------------------
// DO NOT EDIT BELOW THIS LINE IF YOU DON'T KNOW WHAT YOU'RE DOING
// -------------------------------------


// Google Analytics code
  if(google_analytics && google_analytics != "") {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");  
    jQuery.getScript(gaJsHost + "google-analytics.com/ga.js", function(){  
      var pageTracker = _gat._getTracker(google_analytics);  
      pageTracker._initData();  
      pageTracker._trackPageview();  
    });  
  }

	jQuery('form').each(function(){
		var idArray = jQuery(this).attr('id').split('-');
		var postId = idArray[1];
		var formId = jQuery(this).attr('id');
		if ( idArray[0] == 'commentform' && postId && postId != '') {
			jQuery(this).after('<div id="comment-error-'+postId+'"></div>');
			//jQuery('#submit-'+postId).after('<img src="wp-content/plugins/ajax-comment-posting/loading.gif" id="loading-'+postId+'" alt="'+loading+'" />');
			jQuery('#loading-'+postId).hide();
			var form = jQuery(this);
			var err = jQuery('#comment-error-'+postId);
			// WP Ajax Edit Comments hook
			if (window.AjaxEditComments) {
			AjaxEditComments.init();
			} // end if
			
			form.submit(function(evt) { 
			
		  if(form.find('#author-'+postId)[0]) {
			  if(form.find('#author-'+postId).val() == '') {
				   err.html('<span class="error">'+enter_name+'</span>');
				   return false;
			   } // end if
				if(form.find('#email-'+postId).val() == '') {
					err.html('<span class="error">'+enter_email+'</span>');
					return false;
				} // end if
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(!filter.test(form.find('#email-'+postId).val())) {
					err.html('<span class="error">'+enter_valid+'</span>');
					if (evt.preventDefault) {evt.preventDefault();}
			  return false;
				} // end if
			} // end if
			
			if(form.find('#comment-'+postId).val() == '') {
				err.html('<span class="error">'+enter_comment+'</span>');
				return false;
			} // end if
			
			jQuery(this).ajaxSubmit({
								   
				beforeSubmit: function() {
					jQuery('#loading-'+postId).show();
					jQuery('#submit-'+postId).attr('disabled','disabled');
				}, // end beforeSubmit

				error: function(request){
						err.empty();
						if (request.responseText.search(/<title>WordPress &rsaquo; Error<\/title>/) != -1) {
							var data = request.responseText.match(/<p>(.*)<\/p>/);
							err.html('<span class="error">'+ data[1] +'</span>');
						} else {
							var data = request.responseText;
							err.html('<span class="error">'+ data[1] +'</span>');
						}
						jQuery('#loading-'+postId).hide();
						jQuery('#submit-'+postId).removeAttr("disabled");
						return false;
				}, // end error()

				success: function(data) {
				   try {
						var response = jQuery("<ol>").html(data); 
							 if (jQuery(document).find('.commentlist-'+postId)[0]) {
									jQuery('.commentlist-'+postId).prepend(response.find('.commentlist-'+postId+' li:first'));
							 } else {
									jQuery('.respond-'+postId).before(response.find('.commentlist-'+postId));
							 } // end if
							 if (jQuery(document).find('#comments')[0]) {
									jQuery('#comments').html(response.find('#comments'));
							 } else {
									jQuery('.commentlist-'+postId).before(response.find('#comments'));
							 } // end if
							 var commentsCountArray = jQuery('.comment-dropdown-link-'+postId+' a').text().split(' ');
							 var commentText = '';
							 if ( commentsCountArray[0] == 'No' ) {commentText = '1 Comment';}
							 else commentText = (parseInt(commentsCountArray[0])+1)+' Comments';
							 jQuery('.comment-dropdown-link-'+postId+' a').text(commentText);
							 
							 err.empty();
							 //form.remove(); // REMOVE THIS IF YOU DON'T WANT THE FORM TO DISAPPEAR
							 jQuery('#comment-'+postId).val('');
							 //jQuery('.respond-'+postId).hide();
							 err.html('<span class="success">Your comment has been added.</span>');
							 jQuery('#submit-'+postId).removeAttr("disabled");
							jQuery('#loading-'+postId).hide();	
						
					} catch (e) {
						jQuery('#loading-'+postId).hide();
								jQuery('#submit-'+postId).removeAttr("disabled");
						 alert(error+'\n\n'+e);
					} // end try
					
					// WP Ajax Edit Comments hook
						if (window.AjaxEditComments) {
						AjaxEditComments.init();
						} // end if
								   
					} // end success()
					
				}); // end ajaxSubmit()
				
				return false; 
				
			}); // end form.submit()
		}
	});
	
	
	
	
}); // end document.ready()
										

