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

var post

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('#commentform')
		//.after('<div id="error"></div>')
		.find('#submit')
			.after('<img style="display: none;" src="/wp-content/plugins/ajax-comment-posting/loading.gif" id="loading_comment" alt="'+loading+'" />');
	//jQuery('#loading').hide();
	
	var form = jQuery('#commentform');
	//console.info(post.id);
	
	
	var err = jQuery('#error');
	//var post = form.find('#comment_post_ID').val()
	
	// WP Ajax Edit Comments hook
	if (window.AjaxEditComments) {
   		AjaxEditComments.init();
	} // end if
	
    form.submit(function(evt) 
	{
		form.post = form.parents('.post')[0]
		
  		if(form.find('#author')[0]) 
		{
			if(form.find('#author').val() == '') 
			{
				err.html('<span class="error">'+enter_name+'</span>');
				return false;
			} // end if
			if(form.find('#email').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').val()))
			{
				err.html('<span class="error">'+enter_valid+'</span>');
				if (evt.preventDefault) {evt.preventDefault();}
				return false;
			} 
		}
	
		if(form.find('#comment').val() == '') {
			err.html('<span class="error">'+enter_comment+'</span>');
			return false;
		} // end if
		
		jQuery(this).ajaxSubmit(
		{	   
			beforeSubmit: function() {
				jQuery('#loading_comment').show();
				jQuery('#submit').attr('disabled','disabled');
			}, // end beforeSubmit

			error: function(request)
			{	
				var text = request.responseText;
				var response = jQuery("<div>").html(text);
				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 = response.find('p').text();
					err.html('<span class="error">'+ data +'</span>');
				}
				jQuery('#loading_comment').hide();
				jQuery('#submit').removeAttr("disabled");
				return false;
			}, // end error()
			
			success: function(data) {
				try {		
					var temp = data.substring(0, data.length - 7);
					temp = temp.replace(/\r|\n|\r\n|\t/g, "");
					temp = temp.replace(/<!DOCTYPE(.*)<\/head>/g, "");
					
					var response = jQuery(temp);
					
					//console.info(response)
					//console.info('test')
					//console.info(data)
					//console.info(form.post)
					//console.info()
					//console.info(response.find('ol li.comment:last').html())
					//console.info(response.find('#' + form.post.id))
					//console.info(response.find('#' + form.post.id).find('.commentlist'))
					//console.info(response.find('#' + form.post.id).find('.commentlist').find('li:last'))
					//if(jQuery(post))
					//{	
						
						var new_comment = response.find('#blog ol.commentlist li:last').hide();
						jQuery('#commentform textarea, #commentform p.input input').val('')
						jQuery(form.post).find('.commentlist').show()
						jQuery(form.post).find('.commentlist').append(new_comment);
						new_comment.fadeIn(1000);

						//jQuery(form.post).find('.commentlist li.comment:last').append(response.find('#' + form.post.id).find('.commentlist').find('li.comment:last'))
					//}
	
	
					/*
						 if (jQuery(document).find('.commentlist')[0]) {
						if (jQuery(document).find('div.comments')[0]) {
							jQuery('#comments').html(response.find('div.comments'));
						 } else {
					 		jQuery('.commentlist').before(response.find('div.comments'));
						 } // end if
						jQuery('.commentlist').append(response.find('.commentlist li:last'));							
					} else {
						jQuery('#respond').before(response.find('.commentlist'));						
					} // end if
				
				
					err.empty();
					//form.remove(); // REMOVE THIS IF YOU DON'T WANT THE FORM TO DISAPPEAR 
					//jQuery('#respond').hide();
					*/
					//form.hide('fast')
					//err.html('<span class="success">' + comment_added + '</span>');
					jQuery('#submit').removeAttr("disabled");
	           		jQuery('#loading_comment').hide();
					
	            } catch (e) {
					jQuery('#loading_comment').hide();
					jQuery('#submit').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()
										

